How to vertically center-align an inline tikzpicture
You can use any coordinate for baseline
, so long as it is defined somewhere in the picture. So
\coordinate (p) at ([yshift=-.5ex]current bounding box.center);
with
baseline=(p)
will work. Or, if you surround it by curly brackets, you can add the shift into the argument to baseline
, as Symbol1 suggested.
baseline={([yshift=-.5ex]current bounding box.center)}
You should not use \\
to break lines outside special environments, such as tabular
and array
. Leave a blank line or use \par
instead.
Moreover, the entire equations should be in maths mode to ensure proper spacing, as Torbjørn T. noted. For example,
$\permute{2,3,1}\permute{1,3,2}=\permute{2,1,3}$
\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={([yshift=-.5ex]current bounding box.center)}]
\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: \par
$\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: \par
$\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: \par
$\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}
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}