【问题标题】:Tikz: horizontal centering of group of nodesTikz:节点组的水平居中
【发布时间】:2010-05-31 01:11:21
【问题描述】:

我需要将图表的每一行对齐到中心。我正在尝试使用 xshift 来做到这一点。代码如下:

    \begin{tikzpicture}[node distance=1.5cm, auto, text centered]
    \tikzstyle{every node}=[draw,ball];
    \begin{scope}[xshift=1.5cm]
        \node (A) {A};
        \node [right of=A] (B) {B};
        \node [right of=B] (C) {C};
        \node [right of=C] (D) {D};
    \end{scope}
    \begin{scope}[yshift=-1.5cm]
        \node (AB) {AB};
        \node [right of=AB] (AC) {AC};
        \node [right of=AC] (AD) {AD};
        \node [right of=AD] (BC) {BC};
        \node [right of=BC] (BD) {BD};
        \node [right of=BD] (CD) {CD};
    \end{scope}
    \begin{scope}[yshift=-3cm,node distance=2cm,xshift=1cm]
        \node (ABC) {ABC};
        \node [right of=ABC] (ABD) {ABD};
        \node [right of=ABD] (ACD) {ACD};
        \node [right of=ACD] (BCD) {BCD};
    \end{scope}
    \begin{scope}[xshift=4cm, yshift=-4.5cm, node distance=2cm]
        \node (ABCD) {ABCD};
    \end{scope}
\end{tikzpicture}

还有其他方法吗?不喜欢每次都改变 xshift 值。

【问题讨论】:

  • 作为一种解决方法,您能否为每个scope 制作自己的图片?然后,您可以将整堆图片居中。
  • 我想在不同行的节点之间绘制链接(正确对齐后)。所以,我应该在每个 tikzpicture 中使用“叠加”/“记住图片”,这有点复杂。我想也许有一些命令可以将默认节点对齐方式从左侧更改为中心...
  • 我一直在努力解决同样的(-ish)问题,到目前为止还没有解决方案。 +1 有趣的问题。
  • 你的申请是什么?如果您正在绘制一棵树(看起来),您可以使用更具描述性的相对位置信息,并让 Tikz 很好地处理位置。
  • 这种图顺便称为哈斯图。

标签: latex tikz centering pgf


【解决方案1】:

您可以将每一行设为自己的矩阵,从而有效地将一组节点组合为一个。

你的例子

\begin{tikzpicture}[auto]
    \begin{scope}[]
        \matrix[nodes={draw,ball}, column sep=1cm]{
            \node (A) {A}; &
            \node (B) {B}; &
            \node (C) {C}; &
            \node (D) {D}; \\
            };
    \end{scope}
    \begin{scope}[yshift=-1.5cm]
        \matrix[nodes={draw,ball}, column sep=1cm]{
            \node (AB) {AB}; &
            \node (AC) {AC}; &
            \node (AD) {AD}; &
            \node (BC) {BC}; &
            \node (BD) {BD}; &
            \node (CD) {CD}; \\
        };
     \end{scope}
     \begin{scope}[yshift=-3cm]
        \matrix[nodes={draw,ball}, column sep=1cm]{
            \node (ABC) {ABC}; &
            \node (ABD) {ABD}; &
            \node (ACD) {ACD}; &
            \node (BCD) {BCD}; \\
        };
    \end{scope}
    \begin{scope}[yshift=-4.5cm]
        \matrix[nodes={draw,ball}, column sep=1cm]{
            \node (ABCD) {ABCD}; \\
        };
    \end{scope}
\end{tikzpicture}

注意:一定要\usetikzlibrary{matrix}

结果

我自己编了ball风格。

【讨论】:

  • 哦。我刚刚意识到您发表了一条评论,指的是matrix。无论如何,我希望这很有用。
  • 我想制作一个大矩阵,而不是每个范围一个矩阵,谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-30
  • 2015-01-14
  • 2011-01-17
相关资源
最近更新 更多