15

I want to use some symbols from dingbat font Dingosaurs and graphics inside math formulas. However, they are aligned to the bottom, while I want them to be vertically centered.

enter image description here

How can I do it inside both inline and centered equations?

MWE

\documentclass{article}

\usepackage{mathtools}

\usepackage{unicode-math}

\usepackage{fontspec}

\setmainfont{Dingosaurs.ttf}

\usepackage[demo]{graphicx}

\setmathfont{STIX Two Math}

\begin{document}

    $1+\text{A}=2$

    $\displaystyle1+\text{A}=2$

    \[
        1+\includegraphics[width=2cm,height=2cm]{a.png}
    \]

\end{document}

enter image description here

egreg
  • 1,121,712
  • The adjustbox package has an export feature that adds its features to \includegraphics directly, perhaps the the valign=c option is all that is needed. (the option might be named differently, did not check) – daleif Feb 28 '18 at 20:59
  • @daleif I need the fonts more, than the image, so this is not the solution, unfortunately – Michael Fraiman Feb 28 '18 at 21:06
  • Ahh sorry, my bad did not notice that font, but the standalone \adjustbox macro still do what you want, and more. – daleif Feb 28 '18 at 21:08
  • I don't think this is a fontspec or unicode-math question, really. – Will Robertson Feb 28 '18 at 22:54

5 Answers5

15

In math-mode, you can easily vertically center a picture using \vcenter{\hbox{..}}.

\documentclass{article}
\usepackage{mathtools}
\usepackage{unicode-math}
\usepackage{fontspec}
%\setmainfont{Dingosaurs.ttf}
\usepackage[demo]{graphicx}
\setmathfont{STIX Two Math}

\begin{document}

    $1+\text{A}=2$

    $\displaystyle1+\text{A}=2$
    \[
        1+\vcenter{\hbox{\includegraphics[width=2cm]{a.png}}} + 1+\text{A}=2
    \]

\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
  • +1. Maybe create a macro called, say, \cbox, as \newcommand\cbox[1]{\vcenter{\hbox{#1}}} in the preamble, and then write 1+\cbox{\includegraphics[width=1cm,height=1cm]{a}} in the body of the document? (The .png extension isn't strictly necessary.) – Mico Feb 28 '18 at 21:01
7

The following macro \TextVCenter centers text vertically around the math axis. It respects the different math styles and also works in text mode.

\documentclass{article}
\usepackage{amstext}
\usepackage{fontspec}
\newfontfamily\dingo{Dingosaurs.ttf}

\newcommand*{\TextVCenter}[1]{%
  \text{$\vcenter{\hbox{#1}}$}%
}

\begin{document}

  1 + \TextVCenter{\dingo A} = 2

  $\displaystyle 1 + \TextVCenter{\dingo A} = 2$

  $\scriptstyle 1 + \TextVCenter{\dingo A} = 2$

  $\scriptscriptstyle 1 + \TextVCenter{\dingo A} = 2$

\end{document}

Result

Remarks:

  • \text from package amstext (or loaded by amsmath or mathtools) adapts the text sizes to the math style.

  • \vcenter is a math command for centering vertical box material vertically around the math axis.

  • \hbox in opposite to \mbox avoids that a new paragraph with width \hsize starts in the vertical box of \vcenter.

And with the tail sticking to the left:

\documentclass{article}
\usepackage{amstext}
\usepackage{fontspec}
\newfontfamily\dingo{Dingosaurs.ttf}

\newcommand*{\TextVCenter}[1]{%
  \text{$\vcenter{\hbox{#1}}$}%
}

\newcommand*{\DingoA}{%
  \ensuremath{%
    \mkern-9mu\relax
    \TextVCenter{\dingo A}%
  }%
}

\begin{document}

  1 + \DingoA = 2

  $\displaystyle 1 + \DingoA = 2$

  $\scriptstyle 1 + \DingoA = 2$

  $\scriptscriptstyle 1 + \DingoA = 2$

\end{document}

Result

Heiko Oberdiek
  • 271,626
3

The simplest way is to use (or abuse, perhaps) array:

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}

\begin{document}

\[
1+\begin{tabular}{@{}c@{}}\includegraphics[width=2cm]{example-image-a}\end{tabular}=2
\]

\end{document}

enter image description here

egreg
  • 1,121,712
3

A simple \raisebox can also do the trick:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

     \[
        1+\raisebox{-0.45\totalheight}{\includegraphics[scale = 0.5]{Pointe_du_jour}}
    \]

\end{document} 

enter image description here

Bernard
  • 271,350
  • Why -0.45\totalheight, not .5, is it a manual tweak? – Michael Fraiman Mar 01 '18 at 17:14
  • 2
    Yes, it is, to centre the image w.r.t. the math axis, not the baseline. Rigourously done, I should have written \raisebox{\dimexpr-0.5\totalheight+\fontdimen22\textfont2}, but I thought that for casual use, an approximation was enough. – Bernard Mar 01 '18 at 17:26
1

Edit after @barbara's comment (with a complicated equation to make visible the defference):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{mathtools}


\usepackage{fontspec}
\newsavebox{\helpbox}
\newlength{\myshift}
\newcommand\addpic[2][]{\savebox{\helpbox}{\vbox{$.\cdot$}}\setlength\myshift{0.5\ht\helpbox}\begin{tikzpicture}[baseline, outer sep=0,inner sep=0,yshift=\myshift]\node[outer sep=0,inner sep=0] {\includegraphics[#1]{#2}};\end{tikzpicture}}
\begin{document}


    \[
    \frac{1}{\frac{2}{3}}^{\displaystyle\int_{N=\dfrac{1}{3}}^{9^5}f(x)+4 dx}+\addpic[width=2cm,height=2cm]{example-image-a}=1
    \]

\end{document}

Output:

enter image description here

Old answer: A tikz solution:

\documentclass{article}
\usepackage{tikz}
\usepackage{mathtools}


\usepackage{fontspec}

\begin{document}


    \[
    \tikz[anchor=center, baseline] \node {$\frac{1}{\frac{2}{3}}-$};\tikz[anchor=center,baseline] \node {\includegraphics[width=2cm,height=2cm]{example-image-a}};\tikz[anchor=center,baseline]\node{$=1$};
    \]

\end{document}

Output:

enter image description here

And if we add inner sep=0 in tikz options:

enter image description here

koleygr
  • 20,105