Tikz:节点组的水平居中

时间:2010-05-31 01:11:22

标签: latex tikz centering pgf

我需要将图形的每一行与中心对齐。我试图用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值。

1 个答案:

答案 0 :(得分:4)

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

您的示例

\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}

结果

alt text http://i45.tinypic.com/o89r46.jpg

我制作了自己的ball风格。