如何垂直居中对齐内联 tikzpicture

如何垂直居中对齐内联 tikzpicture

我有一个宏,它创建了tikzpicture高度未知的。我想在方程式中内联使用它,并且我希望它与=和其他运算符垂直对齐。到目前为止,我拥有的代码将绘制一个排列,然后在其周围绘制一个框。第一个宏具有已知高度,因此我可以设置的baselinetikzpicture根据需要对齐它。第二个宏没有已知高度,在这里我将设置baseline为最常见的用法。

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

这段代码生成如下图片:

目前结果

我可以做哪些改变以使垂直中心tikzpicture始终与文本的垂直中心对齐?

答案1

为了使事物以数学轴为中心,我添加了一个以环绕您之前的宏定义。我还在数学模式下表达了完整的关系,以便在运算符周围获得适当的间距。根据 cfr 的评论,我从两个宏中\vcenter{\hbox{...}}删除了规范。baseline

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

在此处输入图片描述

答案2

你可以使用任意坐标baseline,只要它在图片的某处有定义即可。所以

\coordinate (p) at ([yshift=-.5ex]current bounding box.center);

baseline=(p)

可以。或者,如果你用花括号括起来,你可以将移位添加到参数中baseline正如 Symbol1 所建议的那样

baseline={([yshift=-.5ex]current bounding box.center)}

\\在特殊环境(例如tabular和 )之外,不应使用来换行array。请保留空行或使用 来\par代替。

此外,整个方程式应该处于数学模式,以确保适当的间距,正如 Torbjørn T. 所言。 例如,

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

相关内容