東向きの木は tikzpicture に収まりません

東向きの木は tikzpicture に収まりません

このコード

\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、予期せぬ悪いことが起きるかもしれないと強く疑っています。一般的に、tikzpictures をネストすることは、単純なケースではうまくいくこともありますが、問題があることが知られています。(つまり、うまくいく場合はうまくいく。うまくいかない場合は、両方の部分を保持できます。) a は a なので、forest基本的に次のよう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}

関連情報