在 tikz 中使用 \\ 換行的問題

在 tikz 中使用 \\ 換行的問題

這是我的程式碼,由 pdflatex 編譯。

\documentclass[11pt]{article}
\usepackage[a5paper,margin=5pt]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{tikz-qtree}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.6]\Tree
[.(0,0)
    [.(0,1)
        [.(1,1)
            [.(1,2)\\$\downarrow$\\(2,0)
                [.(3,0)
                    [.(4,0)\\$\downarrow$\\\textcolor{olive}{(1,3)} ]
                    [.\textcolor{olive}{(3,1)} ]
                ]
                [.\textcolor{cyan}{(2,1)} ]
            ]
            [.\textcolor{cyan}{(2,1)} ]
        ]
        [.(0,2)
            [.(0,3)
                [.(1,3)\\$\downarrow$\\\textcolor{purple}{(4,0)} ]
                [.\textcolor{purple}{(0,4)} ]
            ]
            [.(1,2)\\$\downarrow$\\(2,0)
                [.(3,0)
                    [.(4,0)\\$\downarrow$\\\textcolor{olive}{(1,3)} ]
                    [.\textcolor{olive}{(3,1)} ]
                ]
                [.\textcolor{cyan}{(2,1)} ]
            ]
        ]
    ]
    [.(1,0)
        [.(2,0)\\$\downarrow$\\(1,2)
            [.(1,3)\\$\downarrow$\\\textcolor{purple}{(4,0)} ]
            [.\textcolor{blue}{(2,2)} ]
        ]
        [.(1,1)
            [.(1,2)\\$\downarrow$\\(2,0)
                [.(3,0)
                    [.(4,0)\\$\downarrow$\\\textcolor{olive}{(1,3)} ]
                    [.\textcolor{olive}{(3,1)} ]
                ]
                [.\textcolor{cyan}{(2,1)} ]
            ]
            [.\textcolor{cyan}{(2,1)} ]
        ]
    ]
]
\end{tikzpicture}
\end{center}
\end{document}

根據tikz-qtree套件的文檔,可以在節點中使用\\換行符。但上面的程式碼無法正確編譯。如果我改變包含它的零件\\就會起作用。

為什麼會這樣呢?如何在節點中換行?

答案1

看起來您還沒有添加

\tikzset{every tree node/.style={align=center,anchor=north}}

\begin{tikzpicture}[scale=.6]

打電話之前\Tree。另外以防萬一檢查\pgfversion它是否> 2.1,如文件所述

\documentclass[11pt]{article}
\usepackage[a5paper,margin=5pt]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{tikz-qtree}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.6]
\tikzset{every tree node/.style={align=center,anchor=north}}
\Tree
[.(0,0)
    [.(0,1)
        [.(1,1)
            [.(1,2)\\$\downarrow$\\(2,0)
                [.(3,0)
                    [.(4,0)\\$\downarrow$\\\textcolor{olive}{(1,3)} ]
                    [.\textcolor{olive}{(3,1)} ]
                ]
                [.\textcolor{cyan}{(2,1)} ]
            ]
            [.\textcolor{cyan}{(2,1)} ]
        ]
        [.(0,2)
            [.(0,3)
                [.(1,3)\\$\downarrow$\\\textcolor{purple}{(4,0)} ]
                [.\textcolor{purple}{(0,4)} ]
            ]
            [.(1,2)\\$\downarrow$\\(2,0)
                [.(3,0)
                    [.(4,0)\\$\downarrow$\\\textcolor{olive}{(1,3)} ]
                    [.\textcolor{olive}{(3,1)} ]
                ]
                [.\textcolor{cyan}{(2,1)} ]
            ]
        ]
    ]
    [.(1,0)
        [.(2,0)\\$\downarrow$\\(1,2)
            [.(1,3)\\$\downarrow$\\\textcolor{purple}{(4,0)} ]
            [.\textcolor{blue}{(2,2)} ]
        ]
        [.(1,1)
            [.(1,2)\\$\downarrow$\\(2,0)
                [.(3,0)
                    [.(4,0)\\$\downarrow$\\\textcolor{olive}{(1,3)} ]
                    [.\textcolor{olive}{(3,1)} ]
                ]
                [.\textcolor{cyan}{(2,1)} ]
            ]
            [.\textcolor{cyan}{(2,1)} ]
        ]
    ]
]
\end{tikzpicture}
\end{center}
\end{document}

相關內容