我想使用tabular
和 叠加 来制作一个 Zwicky Box tikzpicture
。如何(半)自动对齐节点到其对应单元格的垂直和水平中心?
我知道它可能适用于矩阵它与的左上角对齐,tabular
并且其行和列分隔以某种方式与单元格的大小相关(所有单元格都有固定的宽度和高度)。
我的问题是:
- 如何将 a
tikzpicture
与 的左上角完全对齐tabular
? - 如何根据
tabular
环境的单元格宽度和高度来计算列和行的分隔大小? - 我如何告诉一个节点在哪个单元格中矩阵它应被放置并且应与单元格的 v/h 中心对齐吗?
- 我是否应该使用
tikzpicture
覆盖tabular
?或者最好放弃tabular
并将所有内容放入矩阵中?
答案1
tabular
我会使用几个宏简单地做到这一点tikzmark
。
代码
\documentclass[convert=false]{standalone}
\usepackage{tikz,booktabs,array}
\makeatletter
\newcommand{\markZwicky}[1][]{\pgfutil@ifnextchar({\mark@Zwicky{#1}}{\mark@Zwicky{#1}()}}
\def\mark@Zwicky#1(#2)#3{%
\tikz[every Zwicky picture,#1]{%
\node[every Zwicky node,draw=none,inner sep=+\z@,outer sep=+\z@] {#3};
\def\tikz@Mark@name{#2}%
\ifx\tikz@Mark@name\pgfutil@empty\else
\tikzset{every Zwicky node/.append style={name={#2}}}%
\fi
\node[every Zwicky node,overlay] {\phantom{#3}};
}%
}
\newcommand{\tikzZwicky}[1][]{%
\def\tikz@Zwicky@args{#1}%
\let\tikz@Zwicky@list\pgfutil@gobble
\let\tikz@Zwicky@first\pgfutil@empty
\pgfutil@ifnextchar(\tikz@Zwicky@collect\tikz@Zwicky@finish
}
\def\tikz@Zwicky@collect(#1){%
\ifx\tikz@Zwicky@first\pgfutil@empty
\edef\tikz@Zwicky@first{#1}%
\else
\edef\tikz@Zwicky@list{\tikz@Zwicky@list,#1}%
\fi
\pgfutil@ifnextchar(\tikz@Zwicky@collect\tikz@Zwicky@finish
}
\def\tikz@Zwicky@finish{%
\tikz[remember picture,overlay]
\draw[every Zwicky connector,/expanded=\tikz@Zwicky@args]
(\tikz@Zwicky@first) [/expanded={@Zwicky@list/.list={\tikz@Zwicky@list}}] [every Zwicky connect finish/.try];
}
\pgfkeys{/expanded/.code={\edef\pgfkeys@temp{{#1}}\expandafter\pgfkeysalso\pgfkeys@temp}}
\makeatother
\tikzset{
@Zwicky@list/.style={insert path={to[every Zwicky connector how/.try] (#1)}},
every Zwicky picture/.style={
baseline,
remember picture,
},
every Zwicky node/.style={
remember picture,
anchor=base,
inner sep=+2pt
},
every Zwicky connector/.style={
ultra thick,
red!80!black,
draw opacity=.5,
line cap=round,
line join=round
}
}
\begin{document}
\begin{tabular}{>{\bfseries}lcccc}
\toprule
& \multicolumn{4}{c}{\bfseries Configurations} \\ \cmidrule{2-5}
Parameters & \bfseries Cookie A & \bfseries Cookie B & \bfseries Cookie C & \bfseries Cookie D \\ \midrule
Texture & Smooth & Soft & \markZwicky(1-3){Chunky} & Soft \\
Consistency & \markZwicky(2-1){Chewy} & Goowy & Crunchy & Chewy \\
Size & Medium & Small & Large & \markZwicky(3-4){Huge} \\
Base & Plain & Plain & \markZwicky(4-3){Chocholate} & Oatmeal \\
Topping & Sugar & None & Icing & \markZwicky(5-4){Chocholate} \\
Stuffing & Chocholate & \markZwicky(6-2){Dates and Harda} & Macadamia & Chocholate \\ \bottomrule
\end{tabular}
\tikzZwicky(1-3)(2-1.east)(3-4.west)(4-3.east)(5-4.west)(6-2)
\tikzZwicky[blue](2-1.east)(5-4.west)(3-4.west)(1-3.center)(4-3.center)(6-2)
\end{document}
输出
答案2
{NiceTabular}
的环境与经典环境(由包定义)nicematrix
类似,但在单元格、行和列下创建 PGF/TikZ 节点。{tabular}
array
可以使用这些节点绘制任何你想要的规则,使用 TikZ 数组构造主数组,但也可以前(所谓的\CodeBefore
)。
然而,您需要多次编译。
\documentclass{article}
\usepackage{nicematrix,booktabs,tikz}
\begin{document}
\begin{NiceTabular}{@{}>{\bfseries}lcccc@{}}
\CodeBefore [create-cell-nodes]
\tikz \draw [red!50, line width=2pt, line join = round]
plot [samples at={3-4, 4-2, 5-5, 6-4, 7-5, 8-3}] (\x) ;
\Body
\toprule
\RowStyle[nb-rows=2]{\bfseries}
& \Block{1-4}{Configurations} \\
\cmidrule{2-5}
Parameters & Cookie A & Cookie B & Cookie C & Cookie D \\
\midrule
Texture & Smooth & Soft & Chunky & Soft \\
Consistency & Chewy & Goowy & Crunchy & Chewy \\
Size & Medium & Small & Large & Huge \\
Base & Plain & Plain & Chocholate & Oatmeal \\
Topping & Sugar & None & Icing & Chocholate \\
Stuffing & Chocholate & Dates and Harda & Macadamia & Chocholate \\
\bottomrule
\end{NiceTabular}
\end{document}