7

I need to center (vertically) some text between two images of different size in Latex and would like to ask whether there is a simple way (similar to the \centering for horizontal alignment) to achieve this? Under "simple" I mean without minipage's and other onerous methods.

PS The images are inserted via:

\begin{figure}[ht]
\centering
\includegraphics{image1} text1 \includegraphics{image2} text2
\end{figure}

and currently all of them are aligned so to touch the bottom of the row:

enter image description here

7
  • 1
    Welcome to TeX.SX! The simplest thing is to use \begin{tabular}{@{}c@{}}\includegraphics{image1}\end{tabular}
    – egreg
    Aug 10, 2015 at 14:09
  • 1
    Use \usepackage[export]{adjustbox} and then \includegraphics[valign=m,width=3cm]{image1} etc.
    – user11232
    Aug 10, 2015 at 14:11
  • 2
    here is something similar: tex.stackexchange.com/a/17102
    – LaRiFaRi
    Aug 10, 2015 at 14:12
  • @egreg: Wouldn't \raisebox{-.5\height}{\includegraphics{image1}} be even simpler?
    – campa
    Aug 10, 2015 at 14:42
  • 1
    @campa Not really: with the tabular the centering will be right with respect to the horizontal bar of +.
    – egreg
    Aug 10, 2015 at 14:45

4 Answers 4

9

enter image description here

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 \parboxes with high accuracy. All the approaches below use only LaTeX macros, but other primitive methods can be used.

\documentclass{article}
\setlength{\parindent}{0in}
\usepackage{calc}
\usepackage[export]{adjustbox}
\begin{document}

\newcommand{\picA}{\includegraphics[height=1.25in,width=1.7in]{example-image-A.pdf}}
\newcommand{\picB}{\includegraphics[height=.5in,width=1in]{example-image-B.pdf}}

\parbox{\widthof{\picA}}{\picA} + another one
\parbox{\widthof{\picB}}{\picB} =

\bigskip

\begin{tabular}{@{}c@{}}\picA\end{tabular} + another one
\begin{tabular}{@{}c@{}}\picB\end{tabular} =

\bigskip

\raisebox{-.5\height}{\picA} + another one
\raisebox{-.5\height}{\picB} =

\bigskip

\includegraphics[valign=m,width=1.7in]{example-image-A.pdf} + another one
\includegraphics[valign=m,width=1in]{example-image-B.pdf} =

\end{document}

Method 1

I suggested \parboxes 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 a width argument to the \parbox macro. Fortunately, we have the calc package, which can provide that length to \parbox seamlessly. In this case, \widthof command is used.

Method 2

In the second method, two tabulars 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 from graphicx package (note that adjustbox loads graphicx). 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 from adjustbox package to center the images vertically. The accuracy here is improved (better than the above two methods), but using valign=m in conjunction with height=<length> scales the images unexpectedly.

1
  • Method 4 works superb....
    – MadyYuvi
    Jul 21, 2021 at 13:59
1

Method 5

Using \vcenter primitive in math mode:

$$\vcenter{\picA} \hbox{ text} + \vcenter{\picB}$$

Method 6

Using \valign primitive:

\valign{\vfil\hbox{#}\vfil\cr \picA\cr \ text + \cr \picB \cr}
0

I'm surprised nobody mentioned using tikz for this. I believe it is overkill to load the tikz package for this purpose only, but many of my documents use it anyway, so it's not an extra burden.

A simple example would be:

\begin{tikzpicture}
\node at (-2,0) {\includegraphics{graphic1}};
\node at (0,0) {$\implies$};
\node at (2,0) {\includegraphics{graphic2}};
\end{tikzpicture}

There are also clever ways to invoke tikz inline and handle the vertical alignment with the surrounding text. See for instance Inline TikZ - vertical centering

0

Another solution with a TikZ matrix:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\matrix[matrix of nodes, nodes={anchor=center}]{
\includegraphics[height=4cm]{example-image-a} & text1 & \includegraphics[height=1cm]{example-image-b} & text2\\};
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .