如何避免 tikzpicture 的嵌套?

如何避免 tikzpicture 的嵌套?

我必须在一个图中以及在多个图中多次绘制以下 MWE 中的概率分布函数 (PDF)。我已阅读以下问题:

并且我认为如果可能的话,我希望避免嵌套。这是一个简单的 MWE,对于这个特定问题,我可以使用所有元素的绝对定位。但这很麻烦。另一个选择是准备一个 PDF 文件并将图像包含在节点中。还有更好的选择吗?

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
  \begin{tikzpicture}[node distance=2em]
    \node(pdf){
      \begin{tikzpicture}
          \draw [step=0.25cm,lightgray,very thin] (0,0) grid (2.5,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (0,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (2.5,0);
          \draw [fill=blue!20!white,ultra thick,overlay] (0.2,0) ..controls
          (0.5,2.2) and (1,0.1) .. (2,0);
      \end{tikzpicture}
    };  
    \node [rectangle,draw, right=of pdf] (cfd) {Nonlinear Flow Solution};
    \path[draw,->] (pdf) -- (cfd); 
  \end{tikzpicture}
\end{document}

答案1

我认为最简单的方法是将内部排版tikzpicture在另一个外部的保存框中tikzpicture,然后使用节点内的框。这样tikzpicture就不会拾取父级的设置,因为内部的设置已经渲染了。

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newsavebox\mybox

\begin{document}

  \begin{lrbox}{\mybox}
      \begin{tikzpicture}
          \draw [step=0.25cm,lightgray,very thin] (0,0) grid (2.5,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (0,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (2.5,0);
          \draw [fill=blue!20!white,ultra thick,overlay] (0.2,0) ..controls
          (0.5,2.2) and (1,0.1) .. (2,0);
      \end{tikzpicture}
  \end{lrbox}
  \begin{tikzpicture}[node distance=2em]
    \node(pdf){\usebox\mybox};
    \node [rectangle,draw, right=of pdf] (cfd) {Nonlinear Flow Solution};
    \path[draw,->] (pdf) -- (cfd); 
  \end{tikzpicture}
\end{document}

如果您想使用宏执行此操作:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newsavebox\mybox
\begin{lrbox}{\mybox}
      \normalfont% to ensure that the font is fully set up
      \begin{tikzpicture}
          \draw [step=0.25cm,lightgray,very thin] (0,0) grid (2.5,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (0,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (2.5,0);
          \draw [fill=blue!20!white,ultra thick,overlay] (0.2,0) ..controls
          (0.5,2.2) and (1,0.1) .. (2,0);
      \end{tikzpicture}
\end{lrbox}
\newcommand\mypdfimage{\usebox\mybox}


\begin{document}

  \begin{tikzpicture}[node distance=2em]
    \node(pdf){\mypdfimage};
    \node [rectangle,draw, right=of pdf] (cfd) {Nonlinear Flow Solution};
    \path[draw,->] (pdf) -- (cfd); 
  \end{tikzpicture}

And again:

  \begin{tikzpicture}[node distance=2em]
    \node(pdf){\mypdfimage};
    % [..]
  \end{tikzpicture}
\end{document}

不过这里的宏只是一个快捷方式,\usebox\mybox如果你不介意的话可以直接使用。

答案2

如果你的情况是指定了子图片第一的然后,您可以将该库与在子图片周围放置一个节点fit一起使用。current bounding box

\documentclass{report}
%\url{http://tex.stackexchange.com/q/89264/86}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}

\begin{document}
  \begin{tikzpicture}[node distance=2em]
          \draw [step=0.25cm,lightgray,very thin] (0,0) grid (2.5,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (0,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (2.5,0);
          \draw [fill=blue!20!white,ultra thick,overlay] (0.2,0) ..controls
          (0.5,2.2) and (1,0.1) .. (2,0);
\node[fit=(current bounding box)] (pdf) {};
    \node [rectangle,draw, right=of pdf] (cfd) {Nonlinear Flow Solution};
    \path[draw,->] (pdf) -- (cfd); 
  \end{tikzpicture}
\end{document}

tikz 子图片使用 fit

如果没有,还有一个同样简单的替代方案(我刚刚在代码中发现),它允许您围绕给定范围定义一个节点。只需将键放在local bounding box=<name>范围上,它就会围绕该范围定义一个矩形节点。

\documentclass[border=10]{standalone}
%\url{http://tex.stackexchange.com/q/89264/86}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
  \begin{tikzpicture}[node distance=2em]
\begin{scope}[local bounding box=pdf]
          \draw [step=0.25cm,lightgray,very thin] (0,0) grid (2.5,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (0,1.5);
          \draw [draw,->,ultra thick] (0,0.0) -- (2.5,0);
          \draw [fill=blue!20!white,ultra thick,overlay] (0.2,0) ..controls
          (0.5,2.2) and (1,0.1) .. (2,0);
\end{scope}
    \node [rectangle,draw, right=of pdf] (cfd) {Nonlinear Flow Solution};
    \path[draw,->] (pdf) -- (cfd); 
  \end{tikzpicture}
\end{document}

一个区别是,范围指定的节点围绕其内容“紧密”:

具有范围的 TikZ 子节点

相关内容