15

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?

16
  • 1
    What's the question?
    – cfr
    Jul 17, 2017 at 15:23
  • 2
    baseline=(current bounding box.center)?
    – cfr
    Jul 17, 2017 at 15:35
  • 1
    At the end of the picture \coordinate (p) at ([yshift=1ex]current bounding box.center); and then use baseline=(p). Probably there is a more elegant way. (p) need not be defined when you write baseline=(p) so long as it is defined somewhere in the picture ....
    – cfr
    Jul 17, 2017 at 17:22
  • 1
    @cfr Interesting... Since baseline involves \tikz@scan@one@point, there should be no problem...
    – Symbol 1
    Jul 18, 2017 at 2:08
  • 1
    Unrelated: you want to have the whole equation in math mode to get proper spacing, not just \oplus. I.e. $\permute{2,3,1}\oplus\permute{1,3,2}=\compoundPermutation{{2,3,1},{1,3,2}}=\permute{2,1,3}$. Jul 18, 2017 at 9:01

2 Answers 2

9

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

5
  • @cfr Thanks. I removed the baseline specifications on the two macros. Jul 18, 2017 at 19:54
  • Good. (+1) I think this is a better solution than mine. I'd never heard of \vcenter before.
    – cfr
    Jul 18, 2017 at 19:58
  • @cfr Yes, it alleviates the need to guess where the math-centered axis is. Incidentally, I believe that \vcenter{\hbox{$.$}} is equivalent to \setbox0=\hbox{$.$}\raisebox{\dimexpr.5\ht\strutbox-.5\dp\strutbox-.5\ht0+.5\dp0\relax}{\copy0} Jul 18, 2017 at 20:11
  • That being so, I can't imagine why anybody would have introduced a wrapper for it.
    – cfr
    Jul 19, 2017 at 1:38
  • @cfr When I say "equivalent", I mean "functionally equivalent". For example, \vcenter works only in math mode. I don't know the actual code for \vcenterand if it performs additional tests, but it is a TeX primitive. Jul 19, 2017 at 9:35
11

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}$

adjusted equations

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .