Images are added with a zero depth, i.e. they sit on the baseline. It depends very much on the situation (and personal taste) what the "ideal" placement of a particular image is. So no, there is no way to automatically get-it-just-right (TM).
Note that \raisebox
allows the use of \height
, \depth
, \width
and \totalheight
(=height+depth) which represent the original dimension of the to be raised box. As said, \depth
is zero and so \totalheight
is identical to \height
for images. So if you want all you images to be 10% below the baseline you can use \raisebox{-.1\height}{\includegraphics[...]{...}}
.
If you want the images to be as deep as the normal text you can use \dp\strutbox
as a reference. The \strutbox
contains the depth and height of a \strut
. Both together are equal to \baselineskip
so about 20% larger than the actual font size. Therefore you might want to use -.8\dp\strutbox
. Alternatively you can use a font size relative length in ex
. 1ex
is about the height of an x
.
As always you might want to make a macro for this if you need this more often.
For getting the horizontal alignment right, you could supply the trim left
option to the tikzpicture
s, 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}
Best Answer
Just to summarize what was mentioned in the comments above and provide some comparison and comments on accuracy and simplicity. Additionally, I suggest a fourth approach with
\parbox
es with high accuracy. All the approaches below use only LaTeX macros, but other primitive methods can be used.Method 1
I suggested
\parbox
es because they are naturally vertically aligned, so this property can be used for vertically aligning images. The accuracy here is the highest of all other methods (observe the alignment of the horizontal bar of + with the image center line), but one must explicitly provide awidth
argument to the\parbox
macro. Fortunately, we have thecalc
package, which can provide that length to\parbox
seamlessly. In this case,\widthof
command is used.Method 2
In the second method, two
tabular
s are used. This seems a simple method, but unfortunately, the alignment is not perfect out of the box. We observe that the images are a bit higher than the+
and the=
signs.Method 3
The third method uses the
\raisebox
macro fromgraphicx
package (note thatadjustbox
loadsgraphicx
). This also seems a simple method, but unfortunately, the alignment is again not perfect out of the box. We observe that the images are a bit lower than the+
and the=
signs.Method 4
The last method uses the
valign=m
command fromadjustbox
package to center the images vertically. The accuracy here is improved (better than the above two methods), but usingvalign=m
in conjunction withheight=<length>
scales the images unexpectedly.