4

I'm trying to align text of different sizes and images in-line. I'd like the center of the text to be aligned, but I'm having trouble figuring out how to do that.

Here's a minimal example with some attempts:

enter image description here

Here's the code that generated that image:

\documentclass{article}                                                         
\usepackage{graphicx}                                                           
\usepackage{array} % for tabular m{...}                                         
                                                                                
\begin{document}                                                                
                                                                                
\begin{center}                                                                  
  \Huge LEFT \normalsize                                                        
  \includegraphics[width=4em]{example-image-a}                                  
  RIGHT                                                                         
\end{center}                                                                    
                                                                                
\begin{center}                                                                  
  \setlength{\tabcolsep}{1ex}                                                   
  \begin{tabular}[c]{r c l}                                                     
    \Huge LEFT &                                                                
    \includegraphics[width=4em]{example-image-b} &                              
    RIGHT                                                                       
  \end{tabular}                                                                 
\end{center}                                                                    
                                                                                
\begin{center}                                                                  
  \Huge LEFT \normalsize                                                        
  \raisebox{-0.75em}{\includegraphics[width=4em]{example-image-c}}              
  \raisebox{0.4em}{RIGHT}                                                       
\end{center}                                                                 
                                                                                
\begin{center}                                                                  
  \setlength{\tabcolsep}{1ex}                                                   
  \begin{tabular}{ m{0.3\textwidth} m{4em} m{0.3\textwidth} }                   
    \raggedleft \Huge LEFT &                                                    
    \includegraphics[width=4em]{example-image-a} &                              
    \raggedright RIGHT                                                          
  \end{tabular}                                                                 
\end{center}                                                                    
                                                                                
\end{document}                                                                     
1
  • I think the \raisebox{lift}[height][depth]{text} is the best way for short snippets like in your example. For longer text, perhaps \parbox[pos]{width}{text} would be better?
    – Roadbug
    Aug 9, 2023 at 18:11

2 Answers 2

4

You can write a text wrapper around the \vcenter primitive (which is basically what also adjustbox dos internally). A starred form ignores the depth of the content.

\documentclass{article}

\usepackage{graphicx}

\makeatletter
\newcommand*{\textvcenter}{\@ifstar{\@tempswatrue\text@vcenter}{\@tempswafalse\text@vcenter}}
\newcommand*{\text@vcenter}[1]{\mbox{$\m@th\vcenter{\setbox\z@=\hbox{#1}\if@tempswa\dp\z@\z@\fi\box\z@}$}}

% to show the axis height in the snapshot
\newcommand*{\showmathaxis}{%
   \setbox\z@=\hbox{$a$}%
   \@tempdima\fontdimen22\textfont2\relax
   \@tempdimb\@tempdima
   \advance\@tempdima.2\p@
   \advance\@tempdimb-.2\p@
   \leavevmode\rlap{\vrule height\@tempdima depth-\@tempdimb width10cm}%
}
\makeatother

\begin{document}

\begin{center}
\showmathaxis
\textvcenter{\Huge LEFT }%
\textvcenter{\includegraphics[width=4em]{example-image-a}}
\textvcenter{RIGHT}
\textvcenter{qypgjtl}
\textvcenter*{qypgjtl}
\end{center}
\end{document}

enter image description here

3
  • Also a nice solution since it needs no additional packages to be loaded. I also tried lowercase letters with your approach, like suggested by @JohnKormylo. They're centred including the length of the descenders for calculating the vertical middle of the line (option valign=m in my answer). Is there an easy solution for calculating the vertical alignment without taking the length of the descenders into account? If not, no problem, since this is not my question post ;)
    – lukeflo
    Aug 9, 2023 at 21:07
  • 1
    @lukeflo See edit.
    – campa
    Aug 10, 2023 at 8:11
  • looks great. If someone chooses your plain approach or my with the adjustbox solely depends on personal preferences. The latter may offer more options, the former has lesser conditions. For me personally, your approach was a very interesting example in plain TeX regarding a field I'm not very used to (depth of characters/letters). So at least in my case, something learned again. Thanks.
    – lukeflo
    Aug 10, 2023 at 8:25
4

You could use the adjustbox package:

\documentclass{article}  
\usepackage[showframe]{geometry}

\RequirePackage{atbegshi,picture}
\AtBeginShipout{%
    \AtBeginShipoutUpperLeft{%
        \put(\dimexpr 1in+\oddsidemargin+.5\textwidth\relax,-\paperheight\relax){%
            \line(0,1){\paperheight}%
        }%
    }%
}

\usepackage{graphicx}
\usepackage[export]{adjustbox}

\begin{document}
\vspace*{1em}
    \begin{center}
        \fbox{\adjustbox{valign=m}{\Huge LEFT} \includegraphics[width=4em,valign=m]{example-image-b} \adjustbox{valign=m}{RIGHT}}
    \end{center}
\end{document} 

I only used \vspace and \fbox to illustrate that the text is vertically and horizontally centered. Just leave it out in your document. Plus I added a vertical line in the middle of the text area for orientation.

But to be honest its a lot of typing just for centering three different words/images. Maybe you could wrap it into a new command.

Edit

As John Kamylo pointed out, there is a difference when you use lowercase letters with descenders like g, y or p. It is possible to substitute valign=m with valign=M. The former will include the depth of the descenders for calculating the centre, the latter will take the general baseline of the letters as reference. You can try out which fits your needs better.

I myself would prefer M option. Looks better in my eyes ;)

Hope it helps.

6
  • There is a subtle difference beteen [valign=m] and [valign=M], at least if there are descenders. Aug 9, 2023 at 19:54
  • @JohnKormylo That's true. I tested both options and in this particular case I can see no real difference. If you see a one, your eyes have to be much sharper than my ;)
    – lukeflo
    Aug 9, 2023 at 20:06
  • 1
    Try "pygmy" instead of "TEST".. Aug 9, 2023 at 20:40
  • Thanks for the hint. And you're right again. Maybe my answer was a little bit unprecise in this regard. But the OP offered only capital letters. If he wants/needs lowercase letters is unclear. And if yes, we still don't know, how he wants them to be vertically centered: descenders in- or excluded. Maybe he will tell us :)
    – lukeflo
    Aug 9, 2023 at 20:51
  • Just edited my answer to include your comment.
    – lukeflo
    Aug 9, 2023 at 20:57

You must log in to answer this question.

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