[Tex/LaTex] tikzpicture alignment and centering

horizontal alignmenttikz-pgfvertical alignment

I have several very similar images except some have some extra graphics that enlarge them slightly. This causes some pictures to be offset and not aligned visually when it's clear they should be.

How can I align several tikzpictures along the the same origin they were created and center vertically and horizontally properly?

  • First, I want to align the 3 large boxes regardless of the what the small boxes are doing

  • Second, I want to be able to center them all without adding additional spaces the \begin{center} seems to do

  • Third, I want to be able to reproduce the exact same results but everything in landscape mode without using the landscape package if possible. (as you can see, the problem is that TeX is putting one rectangle at a time which is not what I want)

  • The methods should be easy and quick. I have many rectangles stacked and some need to be grouped arbitrarily. What these means is I want the methods to sort of be "smart" in that they can figure out themselves so I don't have to go change a lot of stuff each time I have to change the grouping. Basically I would like the first rectangle on the page to define the alignment location and the others on the same page to use it unless I tell them not to.

I've tried creating a custom bounding box as shown in How does TiKZ calculate positioning of picture on page? but it didn't do anything unless I make it too large which then causes other problems. (I can end up doing the alignment but it seems more complex than necessary)

\documentclass[10pt]{book}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw[line width=30pt] (0,0) -- (7,0);
\end{tikzpicture}

\begin{tikzpicture}
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
\end{tikzpicture}

\begin{tikzpicture}
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
    \draw[line width=20pt] (-0.8, 0) -- (-0.5, 0);
\end{tikzpicture}



\begin{center}%
\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (7,0);
\end{tikzpicture}%
\end{center}%

\begin{center}%
\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
\end{tikzpicture}%
\end{center}%

\begin{center}%
\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
    \draw[line width=20pt] (-0.8, 0) -- (-0.5, 0);
\end{tikzpicture}%
\end{center}%




\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (0,7);
\end{tikzpicture}%


\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
\end{tikzpicture}%

\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
    \draw[line width=20pt] (0,-0.8) -- (0,-0.5);
\end{tikzpicture}%



\begin{center}%
\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (0,7);
\end{tikzpicture}%
\end{center}


\begin{center}%
\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
\end{tikzpicture}%
\end{center}%


\begin{center}%
\begin{tikzpicture}%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
    \draw[line width=20pt] (0,-0.8) -- (0,-0.5);
\end{tikzpicture}%
\end{center}%


\end{document}

Best Answer

For getting the horizontal alignment right, you could supply the trim left option to the tikzpictures, which sets the bounding box so it starts at x=0. There's no option like that for the vertical alignment, however, so in that case you'll have to make sure the bounding boxes between the pictures match by setting them manually using \pgfresetboundingbox and then issuing a \path command that has the right dimensions.

You can automate this by defining a style like

master/.style={
    execute at end picture={
        \coordinate (lower right) at (current bounding box.south east);
        \coordinate (upper left) at (current bounding box.north west);
    }
}

which you supply to the first picture of a group to save the necessary bounding box information, and

slave/.style={
    execute at end picture={
        \pgfresetboundingbox
        \path (upper left) rectangle (lower right);
    }
}

which you supply to the other pictures in the group to set their bounding box to be equal to that of the first picture.

You have to be a bit careful with manually adjusted bounding boxes, as the pictures could protrude into the page margins or into the surrounding text.

\documentclass[10pt]{book}
\usepackage{tikz}

\tikzset{
    master/.style={
        execute at end picture={
            \coordinate (lower right) at (current bounding box.south east);
            \coordinate (upper left) at (current bounding box.north west);
        }
    },
    slave/.style={
        execute at end picture={
            \pgfresetboundingbox
            \path (upper left) rectangle (lower right);
        }
    }
}


\begin{document}
\begin{tikzpicture}[master]
    \draw[line width=30pt] (0,0) -- (7,0);
\end{tikzpicture}

\begin{tikzpicture}[slave]
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
\end{tikzpicture}

\begin{tikzpicture}[slave]
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
    \draw[line width=20pt] (-0.8, 0) -- (-0.5, 0);
\end{tikzpicture}



\begin{center}%
\begin{tikzpicture}[master]%
    \draw[line width=30pt] (0,0) -- (7,0);
\end{tikzpicture}%
\end{center}%

\begin{center}%
\begin{tikzpicture}[slave]%
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
\end{tikzpicture}%
\end{center}%

\begin{center}%
\begin{tikzpicture}[slave]%
    \draw[line width=30pt] (0,0) -- (7,0);
    \draw[line width=20pt] (-0.4, 0) -- (-0.1, 0);
    \draw[line width=20pt] (-0.8, 0) -- (-0.5, 0);
\end{tikzpicture}%
\end{center}%




\begin{tikzpicture}[master]%
    \draw[line width=30pt] (0,0) -- (0,7);

\end{tikzpicture}%
\hspace{1em}
\begin{tikzpicture}[slave]%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
\end{tikzpicture}%
\hspace{1em}
\begin{tikzpicture}[slave]%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
    \draw[line width=20pt] (0,-0.8) -- (0,-0.5);

\end{tikzpicture}%



\begin{center}%
\begin{tikzpicture}[master]%
    \draw[line width=30pt] (0,0) -- (0,7);
\end{tikzpicture}%
\hspace{1em}
\begin{tikzpicture}[slave]%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
\end{tikzpicture}%
\hspace{1em}
\begin{tikzpicture}[slave]%
    \draw[line width=30pt] (0,0) -- (0,7);
    \draw[line width=20pt] (0,-0.4) -- (0,-0.1);
    \draw[line width=20pt] (0,-0.8) -- (0,-0.5);
\end{tikzpicture}%
\end{center}%


\end{document}