[Tex/LaTex] How to avoid nesting of tikzpicture

best practicestikz-pgf

I have to draw the probability distribution function (PDF) in the following MWE multiple times in a figure as well as across multiple figures. I have read the following questions:

and think that I would like to avoid nesting if possible. This is a simple MWE and for this specific problem, I could use absolute positioning of all the elements. But it is cumbersome. Another option is prepare a PDF file and include the image in a node. Is there a better option?

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

Best Answer

The simplest way IMHO is to typeset the inner tikzpicture in a savebox outside the other tikzpicture and then use the box inside the node. This way no settings of the parent tikzpicture will be picked up, because the inner one is already rendered.

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

If you want to do this using a macro:

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

But here the macro is just a short-cut, you can use \usebox\mybox directly if you don't mind.