![以表格中的 tikzpicture 为中心](https://linux22.com/image/473341/%E4%BB%A5%E8%A1%A8%E6%A0%BC%E4%B8%AD%E7%9A%84%20tikzpicture%20%E4%B8%BA%E4%B8%AD%E5%BF%83.png)
我正在尝试在表格中垂直居中,但以特定组件为中心。我希望以通用方式执行此操作,因为我通过算法生成 LaTeX。
以下是 MWE:
\documentclass[a4paper, 10pt]{scrartcl}
\usepackage{soul}
\usepackage[cm]{fullpage}
\usepackage{array}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\begin{center}
% Table for row 1, which has 2 columns.
%\begin{table}[h]
\begin{tabular}{>{\centering\arraybackslash}m{7cm}l>{\centering\arraybackslash}m{4cm}}
% SHAPE: rect
\parbox[c][4.4cm][c]{7cm}{\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0cm,0cm) -- (0cm,4cm) -- (7cm,4cm) -- (7cm,0cm) -- cycle;
\end{tikzpicture}
\center first}
& \hspace{1cm} & % SHAPE: rect
\parbox[c][3.4cm][c]{4cm}{\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0cm,0cm) -- (0cm,3cm) -- (4cm,3cm) -- (4cm,0cm) -- cycle;
\end{tikzpicture}
\center second}
\\
\end{tabular}
\end{center}
\end{document}
生成如下内容:
这样做的目的是使其垂直居中(我认为由于边界框问题,它看起来似乎不能完美地工作)。
然而,我真正想要的是让 tikz 框垂直对齐他们的居中,但文本仍然悬挂在框的正下方,如上图所示。为了清楚起见,我希望框按其垂直中间对齐,如下所示(虚线只是为了引导眼睛):
答案1
像这样:
\documentclass[a4paper, 10pt]{scrartcl}
\usepackage{soul}
\usepackage[cm]{fullpage}
\usepackage{array}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\begin{center}
% Table for row 1, which has 2 columns.
\begin{tabular}{>{\centering\arraybackslash}m{7cm}l>{\centering\arraybackslash}m{4cm}}
% SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0,0) rectangle (7,4);
\draw [red,dotted] (0,2) -- (7,2);
\end{tikzpicture}\begin{center}
first
\end{center}
& \hspace{1cm} & % SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0,0) rectangle (4,3);
\draw [red,dotted] (0,1.5) -- (4,1.5);
\end{tikzpicture}\begin{center}
second
\end{center}\\
\end{tabular}
\end{center}
\end{document}
更新:
我之前应该提到这一点。实际上,您不需要在列说明符>{\centering\arraybackslash}
之前使用m
。m
默认情况下,列说明符将使单元格内的每个条目垂直和水平居中,与行的其余部分成比例。因此,如果您只有图片tikz
,它们将始终对齐为单元格的中心(水平和垂直方向)。但是,图片下方的文本可能会引起问题。如果文本占用不同的行,因为整个内容垂直居中,那么图片将会偏移。为了使图片再次居中对齐,您需要使下面的文本占据相同的行(例如,留出空行)。这是一个例子:
\documentclass[a4paper, 10pt]{scrartcl}
\usepackage{soul}
\usepackage[cm]{fullpage}
\usepackage{array}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\begin{center}
% Table for row 1, which has 2 columns.
\begin{tabular}{|m{7cm}|l|m{4cm}|}
% SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0,0) rectangle (7,4);
\draw [red,dotted] (0,2) -- (7,2);
\end{tikzpicture}\begin{center}
first
\end{center}
& \hspace{1cm} & % SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0,0) rectangle (4,3);
\draw [red,dotted] (0,1.5) -- (4,1.5);
\end{tikzpicture}\begin{center}
second picture have long text under the picture
\end{center}\\
\end{tabular}
\end{center}
The second example centered the picure again by putting a empty line below first.
\begin{center}
%Table for row 1, which has 2 columns.
\begin{tabular}{|m{7cm}|l|m{4cm}|}
% SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0,0) rectangle (7,4);
\draw [red,dotted] (0,2) -- (7,2);
\end{tikzpicture}\begin{center}
first\\\null%put a manually line break and empty contents for the new line
\end{center}
& \hspace{1cm} & % SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0,0) rectangle (4,3);
\draw [red,dotted] (0,1.5) -- (4,1.5);
\end{tikzpicture}\begin{center}
second picture have long text under the picture
\end{center}\\
\end{tabular}
\end{center}
\end{document}
输出: