
本文档生成的表格与我想要的大致相同...只是我无法让左侧列中的文本垂直居中对齐。我真正想要的是让该列中的文本垂直居中对齐,水平居中对齐。
我查找了有关这方面的资料,但都表明
- 默认情况下,内容将顶部对齐
- 使用
array
包并指定使用列m{width}
将产生一个中间对齐的列。
但我得到的输出似乎并没有证实这一点。发生了什么?
\documentclass[a4paper]{amsart}
\usepackage{array, tikz}
\usetikzlibrary{arrows}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\tikzset{
s/.style = { font = \ttfamily,
anchor = west
},
r/.style = { draw,
shape = rectangle,
minimum width = 6mm,
minimum height = 6mm,
font = \ttfamily
},
c/.style = { draw,
shape = circle,
minimum width = 6mm,
minimum height = 6mm,
font = \ttfamily
},
arrow/.style = { ->,
> = angle 60,
thick
}
}
\newcommand{\extraSpaceAbove}{\node at (0, -0.4) {\phantom{x}};}
% Hack alert!
\begin{center}
\setlength{\tabcolsep}{4mm}
\renewcommand{\arraystretch}{1.4}
\begin{tabular}{m{30mm}|l}
\multicolumn{1}{c|}{Statement}
& \multicolumn{1}{c}{Symbol table}
\\ \hline
\tt{local a := 2;}
& \begin{tikzpicture} [x = 17.5mm, y = -7.5mm]
\extraSpaceAbove
\node [r] (a) at (0, 0) {a};
\node [c] (aval) at (1, 0) {2};
\draw [arrow] (a.east) -- (aval);
\end{tikzpicture}
\\ \hline
\tt{local b := 3;}
& \begin{tikzpicture} [x = 17.5mm, y = -7.5mm]
\extraSpaceAbove
\node [r] (a) at (0, 0) {a};
\node [r] (b) at (0, 1) {b};
\node [c] (aval) at (1, 0) {2};
\node [c] (bval) at (1, 1) {3};
\draw [arrow] (a.east) -- (aval);
\draw [arrow] (b.east) -- (bval);
\end{tikzpicture}
\end{tabular}
\end{center}
\end{document}
答案1
左侧的文字是垂直居中对齐:就 TeX 而言,tikzpicture
右列只是一行高文本,并且两列的基线正确对齐。
要使左侧的文本在视觉上垂直对齐,您可以使用 选项baseline
,tikzpicture
它告诉 TikZ 中的哪个 y 坐标tikzpicture
应对应于周围文本的基线。 在您的情况下,baseline=(current bounding box.center)
几乎可以工作,但它将左列的基线与 的中心对齐tikzpicture
,而不是将左列的中心与 的中心对齐tikzpicture
。 您可以使用baseline={($(current bounding box.center)+(0,-0.5ex)$)}
,它使用calc
库来计算低于中心 0.5ex 的坐标。
附注:\extraSpaceAbove
您不应使用命令,而应使用\path [use as bounding box]
语法(或等效简写\useasboundingbox
)来重新定义边界框。要在图片顶部和底部添加 1ex,请\useasboundingbox (current bounding box.north west) ++(0,1ex) (current bounding box.south east) ++(0,-1ex);
在 的末尾使用tikzpicture
。
\documentclass[a4paper]{amsart}
\usepackage{array, tikz}
\usetikzlibrary{arrows,calc}
\begin{document}
\tikzset{
s/.style = { font = \ttfamily,
anchor = west
},
r/.style = { draw,
shape = rectangle,
minimum width = 6mm,
minimum height = 6mm,
font = \ttfamily
},
c/.style = { draw,
shape = circle,
minimum width = 6mm,
minimum height = 6mm,
font = \ttfamily
},
arrow/.style = { ->,
> = angle 60,
thick
}
}
\begin{center}
\setlength{\tabcolsep}{4mm}
\renewcommand{\arraystretch}{1.4}
\begin{tabular}{m{30mm}|l}
\multicolumn{1}{c|}{Statement}
& \multicolumn{1}{c}{Symbol table}
\\ \hline
\ttfamily local a := 2;
& \begin{tikzpicture} [x = 17.5mm, y = -7.5mm, baseline={($(current bounding box.center)+(0,-0.5ex)$)}]
\node [r] (a) at (0, 0) {a};
\node [c] (aval) at (1, 0) {2};
\draw [arrow] (a.east) -- (aval);
\useasboundingbox (current bounding box.north west) ++(0,1ex) (current bounding box.south east) ++(0,-1ex);
\end{tikzpicture}
\\ \hline
\ttfamily local b := 3;
& \begin{tikzpicture} [baseline={($(current bounding box.center)+(0,-0.5ex)$)},x = 17.5mm, y = -7.5mm]
\node [r] (a) at (0, 0) {a};
\node [r] (b) at (0, 1) {b};
\node [c] (aval) at (1, 0) {2};
\node [c] (bval) at (1, 1) {3};
\draw [arrow] (a.east) -- (aval);
\draw [arrow] (b.east) -- (bval);
\useasboundingbox (current bounding box.north west) ++(0,1ex) rectangle (current bounding box.south east) ++(0,-1ex);
\end{tikzpicture}
\end{tabular}
\end{center}
\end{document}