32

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?

lockstep
  • 250,273
ala
  • 423
  • 1
  • 4
  • 6
  • 2
    Try height=1em. – Sigur Oct 16 '13 at 14:43
  • 2
    According to http://en.wikibooks.org/wiki/LaTeX/Lengths#Units em is roughly the width of an 'M' (uppercase) in the current font. So it is not what you want. Neither ex roughly the height of an 'x' in the current font. Also see the Examples here http://en.wikibooks.org/wiki/LaTeX/Lengths#Examples – Sigur Oct 16 '13 at 14:54

3 Answers3

43

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
}

and type your paragraph as

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
}
egreg
  • 1,121,712
  • 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
  • 3
    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 01:01
  • @VladimirS. I can't see the problem. Perhaps a new question is in order. – egreg Aug 30 '18 at 08:24
  • Just for the record @egreg , what does \fontcharht\font`\B mean ? – Etsaf Jan 14 '21 at 13:14
  • @Etsaf The height of a B in the current font. In the setting of my answer, the normal document font. – egreg Jan 14 '21 at 13:15
  • How do you have it so that you can substitute in any image? – ina Jun 28 '23 at 04:42
16

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}

enter image description here

2

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}

enter image description here

Sigur
  • 37,330