[Tex/LaTex] How to save the bounding box of a TikZpicture and use in other TikZpicture

tikz-pgfvertical alignment

I need 2 different TikZpictures with the same bounding box (the one of the first TikZpicture) because I need a perfect vertical alignment of the subfigures side by side.

Please, how can I handle this?

Best Answer

You can use the \useasboundingbox command in the second picture to set the bounding box. Should be the first command in the picture.

\useasboundingbox (0,0) rectangle (<width of first picture>,<height of first picture>);

If you don't know the dimensions of the first picture you can get them from the current bounding box node. Using remember picture you can then access this information in the second node.

The code below will set the accept same bounding box for the second picture.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\fbox{%
\begin{tikzpicture}
   \draw (-1,-1) -- (5,5);
   % more drawing commands ...
   \coordinate (FIRST NE) at (current bounding box.north east);
   \coordinate (FIRST SW) at (current bounding box.south west);
\end{tikzpicture}
}
\fbox{%
\begin{tikzpicture}
   \useasboundingbox (FIRST SW) rectangle (FIRST NE);
   \draw (0,0) -- (1,1);
\end{tikzpicture}
}
\end{document}

This works if both pictures use only positive coordinates. Adjustment must be made if this isn't the case.

The \fbox commands are only to display the bounding box and are not really required.

Related Question