# How to set an Inline figure matching the text height?

I want to put a very small figure inline with text. The figure must have the same height as the line it is positioned in. I have this:

Some text... \includegraphics[height=\baselineskip]{picture.png} ... some more text.


This, however, makes the figure slightly too large, filling the whole space up to the bottom of the line above. I want the figure to be only as high as the capital letters of my font. What's the right way to do this?

Tell TeX that the figure should be as high as a “B”:

Some text...
\includegraphics[height=\fontcharht\font\B]{picture.png}
... some more text.


You probably want to define a special command for this:

\newcommand{\mychar}{%
\begingroup\normalfont
\includegraphics[height=\fontcharht\font\B]{picture.png}%
\endgroup
}


some text \mychar{} some text


If you plan to use this also in titles or captions, it's better to say

\DeclareRobustCommand{\mychar}{%
\begingroup\normalfont
\includegraphics[height=\fontcharht\font\B]{picture.png}%
\endgroup
}

• this is exactly it. height=1em is not exactly what I wanted (as it is roughly the width of an 'M' as @Sigur posted) – ala Oct 16 '13 at 15:11
• You might possibly want to end the group begun, but +1 for the % at end of lines:-) – David Carlisle Oct 16 '13 at 15:14
• This is a good solution, but if I use this command in Beamer, an image is stretched horizontally in uncontrollable manner, so it is important to add a width parameter to avoid the issue. – Vladimir S. Aug 30 '18 at 1:01
• @VladimirS. I can't see the problem. Perhaps a new question is in order. – egreg Aug 30 '18 at 8:24
• Just for the record @egreg , what does \fontcharht\font\B mean ? – Etsaf Jan 14 at 13:14

This is what \scalerel* does... it scales its first argument to the same vertical footprint as its second argument, in this case the letter "B".

\documentclass{article}
\usepackage{scalerel}
\begin{document}
This is an inline \scalerel*{\includegraphics{SelfPortrait}}{B} photo.
\end{document}


edit: as pointed out by @egreg, the approach below works only for the normal font size. I'll keep it for historical reasons.

\documentclass{minimal}

\usepackage{graphicx}
\newlength{\myMheight}
% Create the reference text for measures
\settoheight{\myMheight}{M}

\begin{document}

foo\includegraphics[height=\myMheight]{foo.pdf}foo

\end{document}


• You don't need the box, just \settoheight{\myMheight}{M} – David Carlisle Oct 16 '13 at 15:06
• See You may like to have a look at tex.stackexchange.com/q/42114/4918 ;-) – Tobi Oct 16 '13 at 15:24
• This will always use the same height independently of the context: in a \Large context it wouldn't be correct. – egreg Oct 16 '13 at 15:32
• @Sigur Yes, it was. – egreg Oct 16 '13 at 16:27