![表格单元格中 tikzpicture 的垂直对齐方式](https://linux22.com/image/471691/%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%AD%20tikzpicture%20%E7%9A%84%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%E6%96%B9%E5%BC%8F.png)
“结果”列中单元格底部有空格,但该列单元格顶部没有空格。此外,“条件”列中的单元格未垂直对齐。
任何想法如何解决这一问题?
提前致谢。
\documentclass[a4paper,fontsize=12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}
\tikzset{
trafficlight:red/.style = {%
matrix of nodes,
ampersand replacement=\&,
nodes in empty cells,
rounded corners,
draw = blue!70,
fill = blue!30,
nodes = {circle, minimum size=3mm, anchor=center, draw=black},
column 1/.style={nodes={fill=red}},
column 2/.style={nodes={fill=gray!30}},
column 3/.style={nodes={fill=gray!30}},
column sep=3mm,
},
trafficlight:green/.style = {%
matrix of nodes,
ampersand replacement=\&,
nodes in empty cells,
rounded corners,
draw = blue!70,
fill = blue!30,
nodes = {circle, minimum size=3mm, anchor=center, draw=black},
column 1/.style={nodes={fill=gray!30}},
column 2/.style={nodes={fill=gray!30}},
column 3/.style={nodes={fill=green}},
column sep=3mm,
},
trafficlight:yellow/.style = {%
matrix of nodes,
ampersand replacement=\&,
nodes in empty cells,
rounded corners,
draw = blue!70,
fill = blue!30,
nodes = {circle, minimum size=3mm, anchor=center, draw=black},
column 1/.style={nodes={fill=gray!30}},
column 2/.style={nodes={fill=yellow}},
column 3/.style={nodes={fill=gray!30}},
column sep=3mm,
}
}
%%% Работа с таблицами
\usepackage{tabularx} % Дополнительная работа с таблицами
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{table}[h]
\centering
\begin{tabularx}{\textwidth}{ | c | Y | }
\hline
Results & Criteria \\
\hline
\begin{tikzpicture}
\matrix[trafficlight:red]{\& \& \\};
\end{tikzpicture} & Test \\
\hline
\begin{tikzpicture}
\matrix[trafficlight:yellow]{\& \& \\};
\end{tikzpicture} & Test \\
\hline
\begin{tikzpicture}
\matrix[trafficlight:green]{\& \& \\};
\end{tikzpicture} & Test \\
\hline
\end{tabularx}
\end{table}
\end{document}
答案1
看看以下建议是否能满足您的要求:
\documentclass[a4paper,12pt]{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{fit,
matrix}
\newcommand\Semafor[3]%
{
\begin{tikzpicture}[baseline=-1.5mm]
\matrix (m) [matrix of nodes,
nodes = {circle, draw, fill=#1,
minimum size = 4mm, inner sep=0mm, anchor=center},
column sep = 1.5mm,
ampersand replacement=\&
]
{
|[#1]| \& |[#2]| \& |[#3]| \\
};
\node[draw, rounded corners, inner sep=1mm, fit=(m)] {};
\end{tikzpicture}%
}
\usepackage{ragged2e}
\usepackage{tabularray}
\begin{document}
\begin{table}[ht]
\centering
\begin{tblr}{hlines, vlines,
colspec = {Q[c] X[m, cmd={\RaggedRight}]},
row{1} = {cmd={\Centering}},
row{2-Z}= {rowsep = 5pt}
}
Results & Criteria \\
\Semafor{red}{gray}{gray} & \lipsum[66] \\
\Semafor{gray}{yellow}{gray} & Test \\
\Semafor{gray}{gray}{green} & Test \\
\end{tblr}
\end{table}
\end{document}
- 与您的代码相比,信号量的定义(名为
\semafor
更加简短和简单。 - 定义
\semafore
,该选项垂直移动图像基线,使得图像垂直居中,与相邻单元格的垂直中心对齐。 - 用于表
tabularray
包,它默认在单元格内容的下方和上方插入相同的垂直空间。 - 使用
\RaggedRigh
指令来对齐第二列中的文本。如果您喜欢水平居中的文本,请将其替换cmd={\Raggedright}
为c
,然后删除代码行row{1} = {cmd={\Centering}},
。