8

I wish to insert a graphic in an equation, and have it horizontally centered.

For example, the circle in the following example should be in the center of the brackets, not sitting halfway above them.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{equation}
  A + B \Big \langle \tikz \draw (0,0) circle (0.5cm); \Big \rangle
\end{equation}
\end{document}

How do I achieve this?

I'm aware of the hack here, but find this unsuitable. (It will be impractical for me to hack 50+ such diagrams by hand.)

10

The standard way to center content in math is \vcenter which alignes a list of vertical material centered to the math axis (the invisible line the minus sign, the equal sign, fractions etc. are aligned):

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{equation}
  A + B \Big \langle \vcenter{\hbox{\tikz \draw (0,0) circle (0.5cm);}} \Big \rangle
\end{equation}
\end{document}

Output:

enter image description here

2

It is still worth mentioning that baseline can help you in an efficient way: by defining a style vertical align that abbreviates the long baseline=... command.

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \tikzset{
        vertical align/.style={
            baseline=-.5*(height("$+$")-depth("$+$"))
        }
    }
    \foreach\size in{\tiny,\footnotesize,\normalsize,\Large,\huge}{
        \size
        \begin{equation}
            A+B\Big\langle\tikz[vertical align]\draw circle(0.5);\Big\rangle^{
            A+B\Big\langle\tikz[vertical align]\draw circle(0.5);\Big\rangle^{
            A+B\Big\langle\tikz[vertical align]\draw circle(0.5);\Big\rangle}}
        \end{equation}
    }
    \end{document}

What can be done further is that you can apply style globally by saying

\tikzset{
    every picture/.style={vertical align}
}

For irrelevant pictures, you can cancel the effect by either grouping or saying [baseline=0pt] or just [baseline]. (Or you can define no vertical align/.style={baseline})


You can also encode the drawing of the circle in a .pic. For example

\tikzset{
    cir5/.pic={
        \draw circle(0.5);
    }
}

Together with the global setting of baseline, the code is now much simpler

    \begin{equation}
          A+B\Big\langle\tikz\pic{cir5};\Big\rangle
    \end{equation}

in contrast to

    \begin{equation}
        A+B\Big\langle\vcenter{\hbox{\tikz\draw circle(0.5);}}\Big\rangle
    \end{equation}

Of course one can do something like \def\vcentertikz#1{\vcenter{\hbox{\tikz{#1}}}}, just like one can do \def\tikzvcenterpic#1{\tikz[vcenter]{\pic{#1}}}. So as long as one does not need tricky baseline setting (such as baseline=(node.anchor)), both methods are interchangeable.


The following is the full code.

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \tikzset{
        vertical align/.style={
            baseline=-.5*(height("$+$")-depth("$+$"))
        }
    }
    \foreach\size in{\tiny,\footnotesize,\normalsize,\Large,\huge}{
        \size
        \begin{equation}
            A+B\Big\langle\tikz[vertical align]\draw circle(0.5);\Big\rangle^{
            A+B\Big\langle\tikz[vertical align]\draw circle(0.5);\Big\rangle^{
            A+B\Big\langle\tikz[vertical align]\draw circle(0.5);\Big\rangle}}
        \end{equation}
    }

    \clearpage

    \tikzset{
        every picture/.style={vertical align}
    }
    \foreach\size in{\tiny,\footnotesize,\normalsize,\Large,\huge}{
        \size
        \begin{equation}
            A+B\Big\langle\tikz\draw circle(0.5);\Big\rangle^{
            A+B\Big\langle\tikz\draw circle(0.5);\Big\rangle^{
            A+B\Big\langle\tikz\draw circle(0.5);\Big\rangle}}
        \end{equation}
    }

    \clearpage

    \tikzset{
        cir5/.pic={
            \draw circle(0.5);
        }
    }
    \foreach\size in{\tiny,\footnotesize,\normalsize,\Large,\huge}{
        \size
        \begin{equation}
              A+B\Big\langle\tikz\pic{cir5};\Big\rangle
            ^{A+B\Big\langle\tikz\pic{cir5};\Big\rangle
            ^{A+B\Big\langle\tikz\pic{cir5};\Big\rangle}}
        \end{equation}
    }

    \clearpage

    \foreach\size in{\tiny,\footnotesize,\normalsize,\Large,\huge}{
        \size
        \begin{equation}
            A + B \Big \langle \vcenter{\hbox{\tikz \draw (0,0) circle (0.5cm);}} \Big \rangle^{
            A + B \Big \langle \vcenter{\hbox{\tikz \draw (0,0) circle (0.5cm);}} \Big \rangle^{
            A + B \Big \langle \vcenter{\hbox{\tikz \draw (0,0) circle (0.5cm);}} \Big \rangle}}
        \end{equation}
    }

\end{document}

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.