向東生長的樹不適合tikz圖片

向東生長的樹不適合tikz圖片

這段程式碼

\documentclass[a4paper]{article}

\usepackage{tikz}
    \usetikzlibrary{backgrounds}

\usepackage{forest}

\begin{document}
We are the knights who say "Ni!".
\begin{center}
\begin{tikzpicture}[framed]
    \begin{forest}
        [,for tree={grow'=east},shape=coordinate
            [
                [
                    []
                    []
                ]
                [
                    []
                    []
                ]
            ]
            [
                [
                    []
                    []
                ]
                [
                    []
                    []
                ]
            ]
        ]
        \end{forest}
\end{tikzpicture}
\end{center}
\end{document}

給我

上面程式碼的結果

顯然,樹超出了圖片的範圍,這使得它與上面的文字重疊。這是一個已知的錯誤還是我的錯誤,有解決方法嗎?

答案1

forest我認為將環境放入 a 中根本不是一個好主意,tikzpicture我強烈懷疑糟糕的事情可能會意外發生。一般來說,嵌套tikzpicture有時可以在簡單的情況下工作,但眾所周知是有問題的。 (也就是說,如果它有效,它就有效。如果無效,你就可以保留這兩部分。)記住 aforest是 a tikzpicture,所以你本質上是在說

\begin{tikzpicture}[framed]
  ...
  \begin{tikzpicture}
    ...

baseline對於您的範例,如果您適當調整圖片的大小,它確實有效。

  \begin{tikzpicture}[framed,baseline=(current bounding box.center)]

然而,我認為將框架繪製為forest.我認為你最終想要擁有比單純的盒子更精美的東西。如果沒有,還有更簡單的方法。但是可以輕鬆地添加複雜的背景作為forest規範本身的一部分。有幾種方法可以做到這一點。

\begin{forest}
  [
    ..
  ]
  \begin{scope}[on background layer]
    <drawing commands>
  \end{scope}
\end{forest}

或者可以將繪圖命令新增為樹規範的一部分或添加到樹序言中。例如,

  \begin{forest}
    [,for tree={grow'=east},shape=coordinate, tikz+={%
      \begin{scope}[on background layer]
        \node [fit=(current bounding box.north east) (current bounding box.south west), draw=blue!50!cyan, outer color=blue!50!cyan!25, inner color=blue!50!cyan!10, rounded corners, line width=1mm] {};
      \end{scope}
    }
        [
    ...
  \end{forest}

盒裝樹

完整範例程式碼:

\documentclass[a4paper]{article}
\usepackage{forest}
\usetikzlibrary{backgrounds,fit}

\begin{document}
We are the knights who say "Ni!".
\begin{center}
  \begin{tikzpicture}[framed,baseline=(current bounding box.center)]
    \begin{forest}
      [,for tree={grow'=east},shape=coordinate
          [
              [
                  []
                  []
              ]
              [
                  []
                  []
              ]
          ]
          [
              [
                  []
                  []
              ]
              [
                  []
                  []
              ]
          ]
      ]
    \end{forest}
  \end{tikzpicture}
\end{center}
We are the knights who say "Ni!".
\begin{center}
  \begin{forest}
    [,for tree={grow'=east},shape=coordinate, tikz+={%
      \begin{scope}[on background layer]
        \node [fit=(current bounding box.north east) (current bounding box.south west), draw=blue!50!cyan, outer color=blue!50!cyan!25, inner color=blue!50!cyan!10, rounded corners, line width=1mm] {};
      \end{scope}
    }
        [
            [
                []
                []
            ]
            [
                []
                []
            ]
        ]
        [
            [
                []
                []
            ]
            [
                []
                []
            ]
        ]
    ]
  \end{forest}
\end{center}
\end{document}

相關內容