Vertically centering symbols and graphics inside math formulas

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


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


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