# How do I center the two images coming from two different “tikzpicture” environments?

Consider the following code.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds,
calc,
patterns}

\begin{document}

\begin{tikzpicture}
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile};
\end{tikzpicture}
\begin{tikzpicture}%[scale=4, transform shape]
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile} (0, 3) pic {tile};
\end{tikzpicture}

\end{document}


It outputs the following:

I want the first of the two images, namely the rectangle, to be vertically centered. How do I achieve this?

• the adjustbox package is very useful for this – daleif May 22 at 14:55
• Why don't you put them inside only one tikzpicture?????? – user156344 May 22 at 14:57
• @JouleV I did that. But then the two images overlapped. Then I had to make them not overlap by using xshift. Was there a better way? – caffeinemachine May 22 at 15:59
• \begin{scope}[shift=(current bounding box.east)] should do it. Or \begin{tikzpicture}[baseline=(current bounding box.center)] or \raisebox{\dimexpr 0.5\depth-0.5\height}{...} – John Kormylo May 22 at 17:42

I placed each tikzpicture in its own respective $\vcenter{\hbox{...}}$.

On a standalone document, this is sufficient. In a regular document, know that \vcenter will center the content about the math axis, meaning that some of the image will likely extend below the baseline. Depending on the surrounding material, extra steps may be required to balance the vertical spacing with surrounding material.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds,
calc,
patterns}

\begin{document}

$\vcenter{\hbox{\begin{tikzpicture} \tikzset{ tile/.pic={ \draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle; } } \path (0,0) pic {tile}; \end{tikzpicture}}}$%
$\vcenter{\hbox{\begin{tikzpicture}%[scale=4, transform shape] \tikzset{ tile/.pic={ \draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle; } } \path (0,0) pic {tile} (0, 3) pic {tile}; \end{tikzpicture}}}$

\end{document}


Here's a way where everything stays above the baseline, using \raisebox and measuring the content height:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds,
calc,
patterns}
\begin{document}
\setbox0=\hbox{\begin{tikzpicture}
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile};
\end{tikzpicture}}%
\setbox2=\hbox{\begin{tikzpicture}%[scale=4, transform shape]
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile} (0, 3) pic {tile};
\end{tikzpicture}}%
\end{document}


I would not necessarily use separate tikzpictures.

\documentclass[tikz,border=5mm]{standalone}

\begin{document}

\begin{tikzpicture}
\begin{scope}[tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle;
}
]
\path (0,1.5) pic {tile};
\end{scope}
\begin{scope}[xshift=pi*1cm,tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
}]
\path (0,0) pic {tile} (0, 3) pic {tile};
\end{scope}

\end{tikzpicture}

\end{document}


• It seems that you have manually centered it by choosing the right y-coordinate, namely 1.5. Am I right or I missed something? – caffeinemachine May 22 at 15:45
• @caffeinemachine You are right. You can center it automatically, too, but this would require to rewrite the pics. The point here is that the OP can give the same name to different pics in the same tikzpicture using scopes. – user121799 May 22 at 15:47