[Tex/LaTex] Vertically center image with text midpoint

graphicslengthsvertical alignment

I want to vertically align the center of an image with the vertical center of the text on the same line using \raisebox. Using a macro is a must since it will be used many times (and multiple times on the same line). This implementation works, but requires the use of temporary lengths. I'd like to do the calculation directly without these, but am having trouble finding the right syntax. \raisebox{0.5\heightof{E}-0.25in} does not work.

The closest question seems to be the one here, but this approach doesn't align properly. I want the vertical image center to be aligned with the midpoint between the text baseline and the letter top (in this case "E").

MWE:

\documentclass{article}
\usepackage{calc}
\begin{document}

\newlength{\lenA}
\newlength{\lenB}
\setlength{\lenA}{\heightof{E}}
\setlength{\lenB}{0.25in}
Text \raisebox{0.5\lenA-0.5\lenB}{\rule{0.25in}{0.25in}} Testing

\end{document}

Best Answer

Three methods:

  1. With tabular

    \begin{tabular}{@{}c@{}}<material>\end{tabular}
    
  2. With \raisebox and measuring a capital letter

    \raisebox{.5\fontcharht\font`E-.5\height}{<material>}
    
  3. With \raisebox and1ex` (which is David's solution)

    \raisebox{1ex-0.5\height}{<material>}
    
\documentclass{article}
\usepackage{calc}
\begin{document}

T \begin{tabular}{@{}c@{}}\rule{0.25in}{0.25in}\end{tabular} T
\raisebox{.5\fontcharht\font`E-.5\height}{\rule{0.25in}{0.25in}} T
\raisebox{1ex-0.5\height}{\rule{0.25in}{0.25in}} T

\end{document}

enter image description here

I'd use the first method, but it depends on the real application. The centering, for the first method, is with respect to the math axis, where fraction lines would be drawn.

Related Question