6

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

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.

  • yes probably worth mentioning that these centre on the math axis, half the height of E and the x-height of the font respectively. – David Carlisle Aug 21 '13 at 21:20
  • Method 2 does exactly what I want. Where would I find documentation on the effect of the tick mark in .5\fontcharht\font`E-.5\height? – TomH57 Aug 21 '13 at 23:34
  • @TomH57 With "backquote-character" you simply specify the number corresponding to the ASCII code of the character. It's documented in the TeXbook and in TeX by Topic. – egreg Aug 22 '13 at 7:33
4

The following compares several "midpoints":

  1. 1ex (the unit name is derived from the lowercase letter x), see David's answer.

  2. The middle of the letter E (method from the question).

  3. Math axis, center axis of minus, equals, plus and others. This is also the method of egreg's tabular solution.

  4. Ratio of the strut box. LaTeX puts 30 % of \baselineskip below and 70 % above the base line.

The following example file defines macros \midraiseboxA up to \midraiseboxB and compares them:

\documentclass{article}
\usepackage{calc}

\newcommand*{\midraiseboxA}{%
  \raisebox{1ex-.5\height}%
}
\newcommand*{\midraiseboxB}{%
  \raisebox{(\heightof{E}-\height)/2}%
}
\newcommand*{\midraiseboxC}{%
  \raisebox{\heightof{$\vcenter{}$}-.5\height}%
}
\newcommand*{\midraiseboxD}{%
  \raisebox{.2\baselineskip-.5\height}%
}

\usepackage{color}
\newcommand*{\testI}{%
  \textcolor{red}{\rule{.5ex}{.25pt}}%
}
\newcommand*{\testII}{%
  \rule{\baselineskip}{\baselineskip}%
}

\setlength{\parskip}{2ex}

\begin{document}

\newcommand*{\test}[1]{%
  E#1{\testI}xe#1{\testI}- Test #1{\testII} Testing %
  \kern-.75\baselineskip
  \textcolor[gray]{.5}{%
    \rlap{\raisebox{-.5\height}%
                   {\rule{3\baselineskip}{.2pt}}}%
    \rlap{\raisebox{\heightof{E}-.5\height}%
                   {\rule{3\baselineskip}{.2pt}}}%
    \rlap{\raisebox{\depthof{g}*(-1)-.5\height}%
                   {\rule{3\baselineskip}{.2pt}}}%
  }%
  \kern.75\baselineskip
  #1{\testII} Eg%
  \par
}

\test\midraiseboxA
\test\midraiseboxB
\test\midraiseboxC
\test\midraiseboxD

\end{document}

Result

Remarks:

The dependency on package calc can be removed if needed:

  • The expression \heightof is calculated using \settoheight and
  • the calculations can be done with e-TeX's \dimexpr.

Example for \midraiseboxC:

\newdimen\midraisedimen
\newcommand*{\midraiseboxC}{%
  \settoheight\midraisedimen{$\vcenter{}$}%
  \raisebox{\dimexpr\midraisedimen-.5\height\relax}%
}
2

It's easier to use the x-height of the font (although you could measure E if you want)

enter image description here

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


Text \raisebox{1ex-0.5\height}{\rule{0.25in}{0.25in}} Testing

\end{document}

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.