6

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

  • 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
6

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

  • +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
5

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

4

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

  • Original and very beautiful you code +1. – Sebastiano Feb 28 '18 at 20:51
  • The faint horizontal line through example-image-a indicates that the object isn't centered on the math axis. Is there a way to adjust the (vertical) centering? – Mico Feb 28 '18 at 20:57
  • Perhaps easier with adjustbox – daleif Feb 28 '18 at 21:00
  • Btw wouldn't the plain matrix env do the same as this tabular? – daleif Feb 28 '18 at 21:11
  • @daleif Yes, but this exits from math mode. – egreg Feb 28 '18 at 21:30
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

  • Why -0.45\totalheight, not .5, is it a manual tweak? – Michael Fraiman Mar 1 '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 1 '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

  • the minus sign on the left doesn't seem to be aligned exactly with the equals sign on the right, judging from the centerline on the graphic. – barbara beeton Feb 28 '18 at 21:26
  • @barbarabeeton Thanks... You are right... If you change with \dfrac you will find out it isn't aligned at all. – koleygr Feb 28 '18 at 21:43
  • @barbarabeeton... Think that fixed the prioblem! Thanks... – koleygr Feb 28 '18 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.