4

I use this code to place an image next to a character while matching its size:

\documentclass{article}

\usepackage{mwe}
\newlength{\Height}
\newlength{\Depth}
\DeclareRobustCommand{\ScaleImage}[1]{%
    \def\temp{#1}\ifx\temp\empty
        \PackageWarning{Eu}{Empty character given}%
    \fi
    \raisebox{-\depth}{%
        \vphantom{#1}%
        \settoheight{\Height}{#1}%
        \settodepth{\Depth}{#1}%
        \addtolength{\Height}{\Depth}%
        \includegraphics[height=\Height]{example-image-a}%
}}

\begin{document}
    \dots \ScaleImage{!}!

    \dots) \ScaleImage{)}

    an \ScaleImage{a} anonymous \dots

    \dots fooling around, \ScaleImage{,}

    \ScaleImage{}
\end{document}

This is how this looks:

enter image description here

It works pretty well, but note, for example, the example with the exclamation mark, which exceeds the size of the red box. Am I missing something, or is there some discrepancy between how a character is measured and how it is printed?

bers
  • 5,404
  • 4
    I believe there is a discrepancy between the bounding box of a character and the space taken by the actual glyph. See e.g. http://tex.stackexchange.com/a/53223/586 and http://tex.stackexchange.com/a/58327/586 – Torbjørn T. Feb 03 '17 at 09:08
  • 4
    Yes, glyphs that have “rounded” top or bottom overshoot their bounding box. – egreg Feb 03 '17 at 10:39

0 Answers0