16

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?

lockstep
  • 250,273

4 Answers4

17

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.

Martin Scharrer
  • 262,582
  • Now I know how to use pgf to avoid \newbox :) but you need to compile twice. – Alain Matthes Mar 02 '11 at 16:05
  • @Martin: The result is incorrect for me, the second line is out of the second box ? – Alain Matthes Mar 02 '11 at 16:11
  • @Altermundus: You don't need to compile it twice. It works fine in the first run. Maybe you are confusing remember picture with overlay? – Martin Scharrer Mar 02 '11 at 16:15
  • @Altermundus (and Martin): It seems this solution only works correctly on the first compile. Compiling it again moves the short line down. Removing [remember picture] makes it work, though. Funny... – Jake Mar 02 '11 at 16:15
  • @Jake,Altermundus: I updated my example to the simple version I had before. It only works for positive coordinates, but shows that the principle works. – Martin Scharrer Mar 02 '11 at 16:18
  • @Martin: The pgfmanual says "You need to use a driver that supports picture remembering and you need to run TeX twice". Like for Jake, the problem appears on the second run. – Alain Matthes Mar 02 '11 at 16:20
  • @Martin: Your new example doesn't work for me at all because you're referencing a coordinate FIRST NE that you didn't define in the first picture. Just removing [remember picture] from your previous solution works fine, negative coordinates and all! – Jake Mar 02 '11 at 16:21
  • Thanks all. Martin's solution shows the commands I was looking for. It works great in my code. – Fernando Nadal Mar 02 '11 at 16:23
  • @Jake: Thanks twice. That error was a simple typo. Apparently I misunderstood the function of remember picture. Like you said it isn't required here, but actually hurts. The nodes seem to be defined global anyway! – Martin Scharrer Mar 02 '11 at 16:24
  • @Jake: you are right : remember picture is not necessary – Alain Matthes Mar 02 '11 at 16:25
  • @Martin: If you try \draw (0,0) -- (11,11); in the second picture, the picture is greater than the bounding box ! so perhaps it's necessary to clip the rectangle. – Alain Matthes Mar 02 '11 at 16:31
  • @Altermundus: No, \useasboundingbox fixes the bounding box which wont change afterwards. However it's still possible to draw outside the boundaries. Then you might consider adding clip, but it wont have any influence on the alignment, which is the main concern of the OP. – Martin Scharrer Mar 02 '11 at 16:35
  • @Martin: You are right. I corrected my comment before your last comment. The problem with a picture greater than the bounding box is to place other things around the pictures and to avoid surcharge but it's not the main concern of the OP, you are right. – Alain Matthes Mar 02 '11 at 16:44
  • @Fernando Nadal: Please do not forget to accept correct answers to all your questions. Thanks! – Martin Scharrer Mar 02 '11 at 16:46
4

Another way to do it is to use a single tikzpicture environment and one scope environment:

\begin{tikzpicture}
% TikZ code for first picture
\begin{scope}[xshift=5cm]
% TikZ code for second picture
\end{scope}
\end{tikzpicture}

Then the points plotted at (x,y) in each scope will be exactly 5cm apart.

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
2

The code of Martin is correct but if you want to know the width and the height of the picture, you can use

\newbox\mybox 
\setbox\mybox=\hbox{\begin{tikzpicture}
...
\end{tikzpicture}} 

Now you get the width and the height of picture with

\wd\mybox
\ht\mybox
\dp\mybox

The height is \dp + \ht. I don't know if pgf/TikZ gives the height and the width directly.

Hendrik Vogt
  • 37,935
Alain Matthes
  • 95,075
0

Perfect vertical alignment of two independent tikzpictures can be obtained with a tabular with m columns (from array package) or with a sidebyside tcolorbox. Following code shows both options:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{array}
\newtcolorbox{mysidebyside}[1][]{%
enhanced, sidebyside,
sidebyside align= center seam,
halign = center, halign lower = center,
#1
}

\begin{document}
\begin{mysidebyside}[]
\begin{tikzpicture}
\draw (0,0)--++(0:3cm)--++(60:3cm)--cycle;
\end{tikzpicture}
\tcblower
\begin{tikzpicture}
\draw (0,0)--++(0:1cm)--++(60:1cm)--cycle;
\end{tikzpicture}
\end{mysidebyside}

\begin{mysidebyside}[empty]
\begin{tikzpicture}
\draw (0,0)--++(0:3cm)--++(60:3cm)--cycle;
\end{tikzpicture}
\tcblower
\begin{tikzpicture}
\draw (0,0)--++(0:1cm)--++(60:1cm)--cycle;
\end{tikzpicture}
\end{mysidebyside}

\noindent\begin{tabular}{>{\centering\arraybackslash}m{.45\textwidth}>{\centering\arraybackslash}m{.45\textwidth}}
\begin{tikzpicture}
\draw (0,0)--++(0:3cm)--++(60:3cm)--cycle;
\end{tikzpicture}
&
\begin{tikzpicture}
\draw (0,0)--++(0:1cm)--++(60:1cm)--cycle;
\end{tikzpicture}
\end{tabular}

\end{document}

enter image description here

Ignasi
  • 136,588