将表格前三列的文本与第四列的图形顶部水平对齐

将表格前三列的文本与第四列的图形顶部水平对齐

我有下面的表格,有 9 行 4 列。 在 Latex 中生成的表格 前三列是文本,但第四列是使用 Tikz 生成的图形。目前,文本与第四列中每个对应图形的底部水平对齐。如何调整下面给出的代码,使文本与每个图形的顶部对齐?

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage{booktabs} 

\DeclareMathOperator{\rota}{rot_{90}}
\DeclareMathOperator{\rotb}{rot_{180}}
\DeclareMathOperator{\rotc}{rot_{270}}
\DeclareMathOperator{\fliph}{flip_h}
\DeclareMathOperator{\flipv}{flip_v}
\DeclareMathOperator{\flipd}{flip_d}  
\DeclareMathOperator{\flipa}{flip_a}  
\DeclareMathOperator{\I}{I}

\begin{document}

\begin{table}[h!]
\centering
\renewcommand{\arraystretch}{0.8}
\begin{tabular}{c | l | c | c}
\toprule
Index  & Symmetry & Type \# & Example \\
\midrule
8 & none & 1 & 
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (3,0) -- (3,1) -- (1,1) -- (1,2) -- (0,2) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (0,0) grid (2,1);  % do grid last
\end{tikzpicture}}
}$
 \\
\midrule
4 & $\rotb$ & 2 & 
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (1,0) -- (3,0) -- (3,1) -- (2,1) -- (2,2) -- (0,2) -- (0,1) -- (1,1) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,0) grid (2,2);  % do grid last
\end{tikzpicture}}
}$
 \\
4 & $\fliph$ or $\flipv$ & 3 & 
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (3,0) -- (3,1) -- (2,1) -- (2,2) -- (1,2) -- (1,1) -- (0,1) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,0) grid (2,1);  % do grid last
\end{tikzpicture}}
}$
\\
4 & $\flipd$ or $\flipa$ & 4 &
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (2,0) -- (2,1) -- (1,1) -- (1,2) -- (0,2) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (0,0) grid (1,1);  % do grid last
\end{tikzpicture}}
}$
\\
\midrule
2 & $\rota$ (implies $\rotb$ and $\rotc$) & 5 &
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (1,0) -- (2,0) -- (2,1) -- (4,1) -- (4,2) -- (3,2) -- (3,4) -- (2,4) -- (2,3) -- (0,3) -- (0,2) -- (1,2) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,1) grid (3,3);  % do grid last
\end{tikzpicture}}
}$
 \\
2 &  $\flipv$ and $\fliph$ (implies $\rotb$) & 6 & 
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (2,0) -- (2,1) -- (0,1) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (0,0) grid (2,1);  % do grid last
\end{tikzpicture}}
}$
\\ 
2 &  $\flipd$ and $\flipa$ (implies $\rotb$) & 7 & 
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,2) -- (1,2) -- (1,1) -- (2,1) -- (2,0) -- (3,0) -- (3,1) -- (4,1) -- (4,2) -- (3,2) -- (3,3) -- (2,3) -- (2,4) -- (1,4) -- (1,3) -- (0,3) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,1) grid (3,3);  % do grid last
\end{tikzpicture}}
}$
\\ 
\midrule
1 & all of the above symmetries & 8 & 
${
{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle; % draw piece
\end{tikzpicture}}
}$
\\
\bottomrule
\end{tabular}
\caption{Types of polyominoes depending on their non-trivial symmetry.}
\label{tab:polyominoes}
\end{table}
\end{document}

答案1

我建议你删除所有垂直线,用说明代替大多数\midrule指令\addlinespace。我还建议你\adjustbox使用在@samcarter_is_at_topanswers.xyz 的回答

在此处输入图片描述

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
%\usepackage{amsfonts} % amsfonts is loaded automatically by amssymb
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage{booktabs} 
\usepackage[export]{adjustbox}

\DeclareMathOperator{\rota}{rot_{90}}
\DeclareMathOperator{\rotb}{rot_{180}}
\DeclareMathOperator{\rotc}{rot_{270}}
\DeclareMathOperator{\fliph}{flip_h}
\DeclareMathOperator{\flipv}{flip_v}
\DeclareMathOperator{\flipd}{flip_d}  
\DeclareMathOperator{\flipa}{flip_a}  
\DeclareMathOperator{\I}{I}

\begin{document}

\begin{table}[h!]
\centering

\begin{tabular}{@{} clcc @{}}
\toprule
Index  & Symmetry & Type \# & Example \\
\midrule
8 & none & 1 & 
\adjustbox{valign=c}{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (3,0) -- (3,1) -- (1,1) -- (1,2) -- (0,2) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (0,0) grid (2,1);  % do grid last
\end{tikzpicture}}
\\ \addlinespace

4 & $\rotb$ & 2 & 
\adjustbox{valign=c}{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (1,0) -- (3,0) -- (3,1) -- (2,1) -- (2,2) -- (0,2) -- (0,1) -- (1,1) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,0) grid (2,2);  % do grid last
\end{tikzpicture}}
\\ \addlinespace

4 & $\fliph$ or $\flipv$ & 3 & 
\adjustbox{valign=c}{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (3,0) -- (3,1) -- (2,1) -- (2,2) -- (1,2) -- (1,1) -- (0,1) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,0) grid (2,1);  % do grid last
\end{tikzpicture}}
\\ \addlinespace

4 & $\flipd$ or $\flipa$ & 4 &
\adjustbox{valign=c}{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (2,0) -- (2,1) -- (1,1) -- (1,2) -- (0,2) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (0,0) grid (1,1);  % do grid last
\end{tikzpicture}}
\\ \addlinespace

2 & $\rota$ (implies $\rotb$ and $\rotc$) & 5 &
\adjustbox{valign=c}{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (1,0) -- (2,0) -- (2,1) -- (4,1) -- (4,2) -- (3,2) -- (3,4) -- (2,4) -- (2,3) -- (0,3) -- (0,2) -- (1,2) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,1) grid (3,3);  % do grid last
\end{tikzpicture}}
\\ \addlinespace

2 &  $\flipv$ and $\fliph$ (implies $\rotb$) & 6 & 
\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (2,0) -- (2,1) -- (0,1) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (0,0) grid (2,1); % do grid last
\end{tikzpicture}
\\ \addlinespace
 
2 &  $\flipd$ and $\flipa$ (implies $\rotb$) & 7 & 
\adjustbox{valign=c}{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,2) -- (1,2) -- (1,1) -- (2,1) -- (2,0) -- (3,0) -- (3,1) -- (4,1) -- (4,2) -- (3,2) -- (3,3) -- (2,3) -- (2,4) -- (1,4) -- (1,3) -- (0,3) -- cycle;    % draw piece
\draw[step=1cm,gray,very thin] (1,1) grid (3,3); % do grid last
\end{tikzpicture}}
\\ \addlinespace

1 & all of the above symmetries & 8 & 
\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle; % draw piece
\end{tikzpicture}
\\
\bottomrule
\end{tabular}

\caption{Types of polyominoes depending on their non-trivial symmetry.}
\label{tab:polyominoes}
\end{table}

\end{document}

答案2

您可以使用baseline钥匙。我们可以在这里指定图表中某处的坐标,然后 PGF/TikZ 将移动图片,使该坐标位于周围文本的基线上。

我建议使用

baseline={([yshift=+-.5\pgflinewidth,
  shift=(down:2.5mm)]current bounding box.north)}

这样会在基线上方产生一个“框”。因此,单个框将位于基线上(一半的线宽将位于其下方)。

\pgflinewidth是必要的,因为多边形的线宽会添加到边界框,但我们希望线的中间位于基线上。

我无需考虑角落的坐标,而是设置了几个键、 和lr左、右、下、上),它们会朝该方向绘制一条单位线。最后有一个隐式函数,因此您实际上不必将其指定回起始坐标。如果更深入地解析类似的东西,就可以允许而不是,但就目前而言,这有点过头了。du-- cyclerdrrdlllr,d,r,r,d,l,l,l

稍微扩展一下,您可以绘制包含多个封闭区域的图表,但这将需要更多的键。

我没有选择将网格放在多边形骨牌的顶部,而是选择将网格添加为,path picture这意味着它将被您刚刚绘制的形状所剪裁,但它将达到整个形状。

在我看来,这与添加到它的自定义宏一起\strut也可以为您提供良好的行距。

里面table有一个\tikzset命令,可以让所有图表在实际图表外画一条线,显示基线的位置。(这个命令在这个例子中效果很好,因为所有这些图表的起点都在顶部(0, 0)。)

代码

\documentclass[varwidth]{standalone}
%\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{booktabs}

\DeclareMathOperator{\rota}{rot_{90}}
\DeclareMathOperator{\rotb}{rot_{180}}
\DeclareMathOperator{\rotc}{rot_{270}}
\DeclareMathOperator{\fliph}{flip_h}
\DeclareMathOperator{\flipv}{flip_v}
\DeclareMathOperator{\flipd}{flip_d}
\DeclareMathOperator{\flipa}{flip_a}
\DeclareMathOperator{\I}{I}
\tikzset{
  tetris picture/.style={
    black, line width=+.5pt, x=+2.5mm, y=+2.5mm,
    baseline={([yshift=+-.5\pgflinewidth,
      shift=(down:2.5mm)]current bounding box.north)}},
  tetris/.code=\pgfqkeys{/tikz/tetris}{#1},
  tetris={
    l/.style={/tikz/insert path=--++(left:1)},
    r/.style={/tikz/insert path=--++(right:1)},
    u/.style={/tikz/insert path=--++(up:1)},
    d/.style={/tikz/insert path=--++(down:1)},
    path/.style={
      /tikz/path picture={
        \draw[help lines,step=1](path picture bounding box.south west)
                           grid (path picture bounding box.north east);}}}}
\newcommand*\tetris[2][]{%
  \strut\tikz[tetris picture,#1]\draw[tetris/path](0,0)[tetris={#2}]--cycle;}
\begin{document}
\begin{table}[h!]
\centering
%\tikzset{
%  every picture/.append style={
%    execute at end picture={
%      \draw[help lines, overlay] (0,-1) -- ++(left:10cm);}}}
\renewcommand{\arraystretch}{0.8}
\begin{tabular}{c l c c}
  \toprule
  Index & Symmetry                                & Type \# & Example                                \\ \midrule
    8   & none                                    & 1       & \tetris{r,d,r,r,d,l,l,l}               \\ \midrule
    4   & $\rotb$                                 & 2       & \tetris{r,r,d,r,d,l,l,u,l}             \\
    4   & $\fliph$ or $\flipv$                    & 3       & \tetris{r,d,r,d,l,l,l,u,r}             \\
    4   & $\flipd$ or $\flipa$                    & 4       & \tetris{r,d,r,d,l,l}                   \\ \midrule
    2   & $\rota$ (implies $\rotb$ and $\rotc$)   & 5       & \tetris{r,d,d,r,d,l,l,d,l,u,u,l,u,r,r} \\
    2   & $\flipv$ and $\fliph$ (implies $\rotb$) & 6       & \tetris{r,r,d,l,l}                     \\
    2   & $\flipd$ and $\flipa$ (implies $\rotb$) & 7       & \tetris{r,d,r,d,r,d,l,d,l,u,l,u,l,u,r} \\ \midrule
    1   & all of the above symmetries             & 8       & \tetris{r,d,l}                         \\ \bottomrule
\end{tabular}
\caption{Types of polyominoes depending on their non-trivial symmetry.}
\label{tab:polyominoes}
\end{table}
\end{document}

输出

在此处输入图片描述

答案3

您可以使用该adjustbox包(我删除了垂直线,因为它们与 booktabs 结合使用效果不佳):

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage{booktabs} 

\DeclareMathOperator{\rota}{rot_{90}}
\DeclareMathOperator{\rotb}{rot_{180}}
\DeclareMathOperator{\rotc}{rot_{270}}
\DeclareMathOperator{\fliph}{flip_h}
\DeclareMathOperator{\flipv}{flip_v}
\DeclareMathOperator{\flipd}{flip_d}  
\DeclareMathOperator{\flipa}{flip_a}  
\DeclareMathOperator{\I}{I}

\usepackage[export]{adjustbox}


\begin{document}

\begin{table}[h!]
\centering
\renewcommand{\arraystretch}{0.8}
\begin{tabular}{c  l  c  c}
\toprule
Index  & Symmetry & Type \# & Example \\
\midrule
2 &  $\flipd$ and $\flipa$ (implies $\rotb$) & 7 & 
\adjustbox{valign=t}{\begin{tikzpicture}[black,line width=0.5pt, scale=0.24]
\draw (0,2) -- (1,2) -- (1,1) -- (2,1) -- (2,0) -- (3,0) -- (3,1) -- (4,1) -- (4,2) -- (3,2) -- (3,3) -- (2,3) -- (2,4) -- (1,4) -- (1,3) -- (0,3) -- cycle; % draw piece
\draw[step=1cm,gray,very thin] (1,1) grid (3,3);  % do grid last
\end{tikzpicture}}
\\  \bottomrule
\end{tabular}
\caption{Types of polyominoes depending on their non-trivial symmetry.}
\label{tab:polyominoes}
\end{table}
\end{document}

在此处输入图片描述

相关内容