4

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

  • 1
    Welcome to TeX.SX! The simplest thing is to use \begin{tabular}{@{}c@{}}\includegraphics{image1}\end{tabular} – egreg Aug 10 '15 at 14:09
  • 1
    Use \usepackage[export]{adjustbox} and then \includegraphics[valign=m,width=3cm]{image1} etc. – user11232 Aug 10 '15 at 14:11
  • 2
    here is something similar: tex.stackexchange.com/a/17102 – LaRiFaRi Aug 10 '15 at 14:12
  • @egreg: Wouldn't \raisebox{-.5\height}{\includegraphics{image1}} be even simpler? – campa Aug 10 '15 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 '15 at 14:45
5

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 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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.