![如何在以下表格环境中对齐事物?](https://linux22.com/image/461812/%E5%A6%82%E4%BD%95%E5%9C%A8%E4%BB%A5%E4%B8%8B%E8%A1%A8%E6%A0%BC%E7%8E%AF%E5%A2%83%E4%B8%AD%E5%AF%B9%E9%BD%90%E4%BA%8B%E7%89%A9%EF%BC%9F.png)
我试图按以下顺序排列下面的内容:(a)左侧的黑色文本(xx,yy,zz)应相对于右侧的相关表格单元格垂直对齐;(b)左列和右列之间没有太多带有框的空间;(c)这是一个棘手的问题:正如您所见,在右侧最下面的一组框中,我试图将 D 和 E 与 B 和 C 对齐,但由于某种原因,tikz 图片被“裁剪”了。
我应该提到,在找到以下答案后,我正在使用 tabularray:如何使单元格文本垂直居中?
以下是 MWE:
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\begin{document}
\begin{tblr}{
colspec = {X[l,m]X[l,m]},
rowsep = 0pt,
colsep = 0pt
}
zz: & something \\
yy: & \begin{tikzpicture}[cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}]
\path (0,0) node[cc] (A) {A} (2.3,0) node[cc] (B) {B} (4.6,0) node[cc] (C) {C};
\draw[->] (A)--(B);
\draw[->] (B)--(C);
\end{tikzpicture} \\
xx & \begin{tikzpicture}[cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={align=center,fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={align=center,fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}]
\path (2.3,0) node[cyellow] (AA) {D} (4.6,0) node[cgreen] (BB) {E};
\draw[->] (AA)--(BB);
\end{tikzpicture}
\end{tblr}
\end{document}
答案1
这个怎么样?我使用了正常tabular
环境,将tikzpicture
s 置于数学模式并\vcenter
对其进行了编辑。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tabular}{rl}
zz: & something\\
yy: & $\vcenter{\hbox{\begin{tikzpicture}[baseline=(current bounding box.center),cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}]
\path (0,0) node[cc] (A) {A} (2.3,0) node[cc] (B) {B} (4.6,0) node[cc] (C) {C};
\draw[->] (A)--(B);
\draw[->] (B)--(C);
\end{tikzpicture}}}$ \\
xx: & $\vcenter{\hbox{\begin{tikzpicture}[cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={align=center,fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={align=center,fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}]
\path (2.3,0) node[cyellow] (AA) {D} (4.6,0) node[cgreen] (BB) {E};
\draw[->] (AA)--(BB);
\end{tikzpicture}}}$
\end{tabular}
\end{document}
答案2
(a)设置 tikz 图片的基线。
(b)使用Q
列类型代替X
。
(c)在 A 下方绘制一个不可见的节点,使 B 与 D 对齐,使 C 与 E 对齐。
我增加了盒子的高度只是为了演示。
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tblr}{
colspec = {Q[l,m] Q[l,m]},
rowsep = 0pt,
colsep = 0pt
}
zz: & something \\
yy: & \begin{tikzpicture}[
cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm,minimum height=10mm},
cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
cgreen/.style={fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
corange/.style={fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
baseline={(A.base)}
]
\path (0,0) node[cc] (A) {A} (2.3,0) node[cc] (B) {B} (4.6,0) node[cc] (C) {C};
\draw[->] (A)--(B);
\draw[->] (B)--(C);
%\path node (yy) [left=of A, inner sep=0pt, rectangle, draw] {yy};
\end{tikzpicture} \\
xx & \begin{tikzpicture}[
cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm,minimum height=10mm},
cgreen/.style={align=center,fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
corange/.style={align=center,fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
invisible/.style={inner sep=4,outer sep=0,minimum width=18mm},
baseline={(AA.base)}
]
\path (0,0) node[invisible] {} (2.3,0) node[cyellow] (AA) {D} (4.6,0) node[cgreen] (BB) {E};
\draw[->] (AA)--(BB);
\end{tikzpicture}
\end{tblr}
\end{document}
答案3
- 用
arrows.meta˛
链条and
定位` Ti钾Z 库 - 常见的 tikz 样式定义
- 短代码 ...
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
chains,
positioning}
\begin{document}
\tikzset{
node distance = 0mm and 5mm,
start chain = going right,
N/.style = {fill=#1!20, rounded corners=2pt, anchor=base,
inner ysep=4mm, inner xsep=0mm, minimum width=18mm,
on chain},
N/.default = blue,
every edge/.append style = {-Straight Barb,}
}
\begin{tblr}{colspec = {Q[l,m] Q[r,m, wd=64mm]},
colsep = 1mm, rowsep = 0pt}
zz: & \SetCell{l} something \\
yy: & \tikz[baseline=1pt]
{\node[N] (a) {A};
\node[N] (b) {B};
\node[N] (c) {C};
\draw (a) edge (b) (b) edge (c);
} \\
xx: & \tikz[baseline=1pt]
{\node[N=yellow] (a) {D};
\node[N=green] (b) {E};
\draw (a) edge (b);
}
\end{tblr}
\end{document}
编辑:
或者使用join
宏来获得更短的代码:
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
chains,
positioning}
\begin{document}
\tikzset{
node distance = 0mm and 5mm,
start chain = going right,
N/.style = {fill=#1!20, rounded corners=2pt, anchor=base,
inner ysep=4mm, inner xsep=0mm, minimum width=18mm,
on chain},
N/.default = blue,
every join/.style = {-Straight Barb} % <---
}
\begin{tblr}{colspec = {Q[l,m] Q[r,m, wd=64mm]},
colsep = 1mm, rowsep = 0pt}
zz: & \SetCell{l} something \\
yy: & \tikz[baseline=1pt]
{\node[N] (a) {A};
\node[N, join] {B}; % <---
\node[N, join] {C}; % <---
} \\
xx: & \tikz[baseline=1pt]
{\node[N=yellow] {D};
\node[N=green, join] {E}; % <---
}
\end{tblr}
\end{document}
在两种情况下你都会得到相同的结果:
附录:
使用@Qrrbrbirlbel 回答我的[问题2使用库的可能解决方案graphs
可以是:
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
graphs}
\begin{document}
\begin{table}
\centering
\tikzset{
N/.style = {fill=#1!20, rounded corners=2pt, alias=N,
inner ysep=4mm, inner xsep=0mm, outer sep=0pt,
minimum width=18mm},
N/.default = blue,
}
\tikzgraphsset{
branch right sep=5mm,
every graph/.append style = {path, ->, /tikz/>=Straight Barb}}
\begin{tblr}{colspec = {Q[r] Q[r,wd=64mm]},
colsep = 1mm, rowsep = 0pt}
zz: & \SetCell{l} something \\
yy: & \tikz[baseline=(N.base)] \graph{A[N], B[N], C[N]}; \\
xx: & \tikz[baseline=(N.base)] \graph{D[N=yellow], E[N=green]};
\end{tblr}
\end{table}
\end{document}
编译结果和之前几乎相同。
答案4
这是一个{NiceTabular}
使用nicematrix
(和用于箭头的 Tikz)的解决方案。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\NewDocumentCommand{\Fill}{m}{\Block[fill=#1,rounded-corners]{}}
\begin{NiceTabular}{rw{c}{2cm}cw{c}{2cm}cw{c}{2cm}}[cell-space-limits=3mm]
xx: & something \\
yy: & \Fill{blue!15} A && \Fill{blue!15} B && \Fill{blue!15} C \\
xx: & && \Fill{yellow!15} D && \Fill{green!15} E \\
\CodeAfter
\begin{tikzpicture} [->]
\draw (2.5-|3) -- (2.5-|4) ;
\draw (2.5-|5) -- (2.5-|6) ;
\draw (3.5-|5) -- (3.5-|6) ;
\end{tikzpicture}
\end{NiceTabular}
\end{document}
与往常一样,nicematrix
您需要进行多次编译。