23

I would like to use a small image in an equation, where a symbol like \alpha might otherwise be. Is there a nice way to do this?

lockstep
  • 250,273
Bill Cheatham
  • 2,323
  • 1
  • 20
  • 18
  • 1
    I have never tried \includegraphics in math mode, but I would expect it to work. Are you saying it doesn't? Or is it that you have some difficulty with vertical alignment (which I imagine could become problematic)? – Harald Hanche-Olsen Feb 14 '11 at 16:33

3 Answers3

19

It won't scale nicely with font size, but a simple approach is straightforward. The image should have a tight bounding box, which you can achieve with tools like pdfcrop.

\newcommand{\mysymbol}{\mathord{\includegraphics[height=1.6ex]{symbol}}}

\mathord is suitable for ordinary symbols, since you indicated that it would be used similarly as \alpha.

With the suggestions from the comments, I wrote a better solution.

\newcommand{\myfancysymbol}{
 {\mathchoice
  {\includegraphics[height=1.6ex]{symbol}}
  {\includegraphics[height=1.6ex]{symbol}}
  {\includegraphics[height=1.2ex]{symbol}}
  {\includegraphics[height=0.9ex]{symbol}}
 }
}

The symbol scales like a capital X for subscript and subsubscripts.

  • 4
    Use height=1em, then the picture can be auto scaled. – Leo Liu Feb 14 '11 at 16:42
  • @Leo Liu: It should, but it didn't work in my tests: $ \mysymbol_{\mysymbol_{\mysymbol}} $ gives 3x the same size with 1em or similar. – Martin Scharrer Feb 14 '11 at 16:57
  • Until someone suggest a nicer automatic scaling, I would still say a static 1em is nicer. Thanks Leo for the suggestion. – Mikael Öhman Feb 14 '11 at 16:59
  • 1
    @Martin: Use \mathchoice then. I used this trick in my own document years ago. – Leo Liu Feb 14 '11 at 17:01
  • 1
    I would suggest 1.6ex instead, which is about the height of an uppercase X. The 1em is actually the width of an uppercase M. – Martin Scharrer Feb 14 '11 at 17:01
  • I agree with 1.6ex or something similar. BTW, 1em is in fact the length of \quad. It used to be width of M, but now don't have to. – Leo Liu Feb 14 '11 at 17:07
  • 1
    I have incorporated all your comments to an improved solution. – Mikael Öhman Feb 14 '11 at 17:15
  • @Mikael: Don't use braces outside \mathord, it will be wrong for \mathbin etc. And there're \defaultscriptratio and \defaultscriptscriptratio for scriptstyle and scriptscriptstyle. Or we can read the value set by \DeclareMathSizes before. – Leo Liu Feb 14 '11 at 17:28
  • @Mikael, a tip: you can use backticks ``` to mark your inline code as I did in my edit. – Hendrik Vogt Feb 14 '11 at 18:25
  • @Leo, You are right about \mathord, however i opted to remove it, as just as you've seen for yourself, it's necessary to beeing able to use it conveniently X_\symbol. I choose not to use use \defaultscriptratio, as I could not get it to work without involving the calc package, which felt like a unnecessary complication at this point. I have learnt alot on this question and I thank you all for the helpful comments. – Mikael Öhman Feb 15 '11 at 00:25
  • @Mikael: If the symbol is \mathbin, we will use it as $X_{a \foo b}$. Mostly only ordinary symbols is used as $X_\foo$. – Leo Liu Feb 15 '11 at 05:27
  • @Leo, yes, and as far as i can see, he do wants an ordinary symbol, like \alpha – Mikael Öhman Feb 15 '11 at 12:08
  • Thanks, this is just what I wanted. I found that if I made the images 'bigger' than normal text, they would be vertically misplaced, so I used @TH's suggestion of \vcenter to get them better placed. – Bill Cheatham Feb 15 '11 at 16:12
  • Note that I needed to add an additional pair of {} around the whole command definition in order to be able to use it as subscript as in $x_\myfancysymbol$. – quazgar Aug 19 '13 at 16:06
7

Mikael and Leo both gave good answers so I won't repeat that information.

If you need the image centered with respect to binary operators, fractions, and the like, then you can use \vcenter{...} to perform that vertical centering.

TH.
  • 62,639
4

Yes, you can. There is nearly no difference between a image and a symbol. Just define a command for convenience. You can also use PSTricks or TikZ to draw such a symbol.

However, you may need to redefine the depth of the box, and refine the spacing using \mathbin etc.

A full example (suppose a logo.pdf exists):

\documentclass{article}
\usepackage{graphicx}
\def\xlogo#1{\includegraphics[width=#1em]{logo}}
\def\logo{{% mathord
  \mathchoice
    {\xlogo1}%
    {\xlogo1}%
    {\xlogo\defaultscriptratio}%
    {\xlogo\defaultscriptscriptratio}}}
\begin{document}
$\logo_{\logo_{\logo_\logo}}$
\end{document}
Leo Liu
  • 77,365