[Tex/LaTex] How to vertically center-align an inline tikzpicture

tikz-pgfvertical alignment

I have a macro that creates a tikzpicture with an unknown height. I want to use it inline in equations, and I want it to to vertically line up with = and other operators. The code I have so far will draw a permutation and then draw a box around it. The first macro has a known height, so I can set the baseline of the tikzpicture and align it as I want. The second one does not have a known height, and here I've set the baseline to work for the most common usage.

\documentclass{article}
\usepackage{tikz}

% This works for fixed-height pictures.
\newcommand{\permute}[1]
  {\begin{tikzpicture}[x=2ex,y=-2ex, baseline=-3.5ex]
    \foreach \from [count=\to] in {#1}{
      \draw (\from,1) -- (\to,2);
    }
    \draw[gray] (0.5,0.5) rectangle (\to+0.5,2.5);
  \end{tikzpicture}}

% This no longer works because the height isn't known in advance.
\newcommand{\compoundPermutation}[1]
  {\begin{tikzpicture}[x=2ex,y=-2ex, baseline=-4.5ex]
    \foreach \list [count=\row] in {#1} {
      \foreach \from [count=\to] in \list {
        \draw (\from,\row) -- (\to,\row+1);
      }
    }
    \foreach \list [count=\count] in {#1} {
      \ifx \count \row
        \foreach \from [count=\to] in \list {
        }
        \draw[gray] (0.5,0.5) rectangle (\to+0.5,\row+1.5);
      \fi
    }
  \end{tikzpicture}}

\begin{document}

\begin{itemize}
  \item The group operation, $\oplus$, can be thought of as stacking: \\
        \permute{2,3,1}$\oplus$\permute{1,3,2}=\compoundPermutation{{2,3,1},{1,3,2}}=\permute{2,1,3}
  \item Sometimes, for brevity, we omit the operator's symbol: \\
        \permute{2,3,1}\permute{1,3,2}=\permute{2,1,3}
  \item If we look at it this way, it's clear that we satisfy associativity: \\
        \permute{2,3,1}\permute{1,3,2}\permute{3,1,2}=\compoundPermutation{{2,3,1},{1,3,2}}\permute{3,1,2}=\permute{2,3,1}\compoundPermutation{{1,3,2},{3,1,2}}=\compoundPermutation{{2,3,1},{1,3,2},{3,1,2}}=\permute{3,2,1}
\end{itemize}

\end{document}

This code produces a picture like this:

Result so far

What can I change so that the vertical center of the tikzpicture always lines up with the vertical center of the text?

Best Answer

To get things centered about the math axis, I added a \vcenter{\hbox{...}} to wrap around your prior macro definitions. I also expressed the complete relations in math mode, to get proper spacing around the operators. Per cfr's comment, I removed the baseline specification from the two macros.

\documentclass{article}
\usepackage{tikz}

% This works for fixed-height pictures.
\newcommand{\permute}[1]
  {\vcenter{\hbox{\begin{tikzpicture}[x=2ex,y=-2ex]
    \foreach \from [count=\to] in {#1}{
      \draw (\from,1) -- (\to,2);
    }
    \draw[gray] (0.5,0.5) rectangle (\to+0.5,2.5);
  \end{tikzpicture}}}}

% This no longer works because the height isn't known in advance.
\newcommand{\compoundPermutation}[1]
  {\vcenter{\hbox{\begin{tikzpicture}[x=2ex,y=-2ex]
    \foreach \list [count=\row] in {#1} {
      \foreach \from [count=\to] in \list {
        \draw (\from,\row) -- (\to,\row+1);
      }
    }
    \foreach \list [count=\count] in {#1} {
      \ifx \count \row
        \foreach \from [count=\to] in \list {
        }
        \draw[gray] (0.5,0.5) rectangle (\to+0.5,\row+1.5);
      \fi
    }
  \end{tikzpicture}}}}

\begin{document}

\begin{itemize}
  \item The group operation, $\oplus$, can be thought of as stacking: \\
        $\permute{2,3,1}\oplus\permute{1,3,2}=\compoundPermutation{{2,3,1},{1,3,2}}=\permute{2,1,3}$
  \item Sometimes, for brevity, we omit the operator's symbol: \\
        $\permute{2,3,1}\permute{1,3,2}=\permute{2,1,3}$
  \item If we look at it this way, it's clear that we satisfy associativity: \\
        $\permute{2,3,1}\permute{1,3,2}\permute{3,1,2}=\compoundPermutation{{2,3,1},{1,3,2}}\permute{3,1,2}=\permute{2,3,1}\compoundPermutation{{1,3,2},{3,1,2}}=\compoundPermutation{{2,3,1},{1,3,2},{3,1,2}}=\permute{3,2,1}$
\end{itemize}

\end{document}

enter image description here