我想使用 TikZ 来格式化表格。我的目标是得到一个 4 列的表格,其总宽度为\texwidth
(我设法通过将最小长度设置为来获得这个值0.25\texwidth
,尽管我猜这不是最好的,因为它会创建一个坏框)。
在此表格中,我希望文本左对齐,并在某些单元格上叠加,但我希望叠加覆盖整个单元格。更改此处找到的一些代码后,我得到了以下结果:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{patterns,tikzmark}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}
\usepackage{hf-tikz}
\pgfkeys{tikz/mymatrix/.style={matrix of nodes,nodes in empty cells,left,inner sep=0pt,outer sep=0pt,column sep=0pt,row sep=0pt,nodes={align=left,minimum width=0.25\textwidth,minimum height=10pt,anchor=center,inner sep=0pt,outer sep=0pt}}}
\tikzset{style green/.style={
set fill color=green!50!lime!60,draw opacity=0.4,
set border color=green!50!lime!60,fill opacity=0.1,
},
style cyan/.style={
set fill color=cyan!90!blue!60, draw opacity=0.4,
set border color=blue!70!cyan!30,fill opacity=0.1,
},
style orange/.style={
set fill color=orange!90, draw opacity=0.8,
set border color=orange!90, fill opacity=0.3,
},
style brown/.style={
set fill color=brown!70!orange!40, draw opacity=0.4,
set border color=brown, fill opacity=0.3,
},
style purple/.style={
set fill color=violet!90!pink!20, draw opacity=0.5,
set border color=violet, fill opacity=0.3,
},
cll/.style={
above left offset={-0.1,0.15},
below right offset={0.10,-0.17},
#1
},set fill color/.code={\pgfkeysalso{fill=#1}},
set border color/.style={draw=#1}
}
\begin{document}
\begin{tikzpicture}[baseline={-1ex}]
\matrix [mymatrix,inner sep=0pt] (m)
{
\tikzmarkin[cll=style green]{first}11 11&12&13&14\tikzmarkend{first}\\
\tikzmarkin[cll=style orange]{second}21&22&23\tikzmarkend{second}&24\\
\tikzmarkin[cll=style cyan]{third}31&32&33&34\tikzmarkend{third}\\
41&\tikzmarkin[cll=style brown]{fourth}42\tikzmarkend{fourth}&43&44\\
};
\end{tikzpicture}
\vspace{1em}
\lipsum[1-1]
\end{document}
目前我无法让覆盖层覆盖整个单元格,例如,它从单元格 11 中的文本之前开始,到单元格 14 中的文本之后结束。如何实现?更改锚点和/或对齐没有任何作用。此外,我如何才能强制矩阵具有固定的整体宽度?
答案1
您已经定义了具有正确大小的节点,但是tikz-markin
引用被放置在文本之前和之后,而没有考虑节点的大小。
因此,我建议使用fitting
节点(来自fit
库),这些节点围绕其他节点绘制节点,并考虑它们的完整大小。
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{patterns,tikzmark, fit}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}
\usepackage{hf-tikz}
\pgfkeys{tikz/mymatrix/.style={matrix of nodes,nodes in empty cells,left,inner sep=0pt,outer sep=0pt,column sep=0pt,row sep=0pt,nodes={align=left,minimum width=0.25\textwidth,minimum height=10pt,anchor=center,inner sep=0pt,outer sep=0pt}}}
\tikzset{style green/.style={
set fill color=green!50!lime!60,draw opacity=0.4,
set border color=green!50!lime!60,fill opacity=0.1,
},
style cyan/.style={
set fill color=cyan!90!blue!60, draw opacity=0.4,
set border color=blue!70!cyan!30,fill opacity=0.1,
},
style orange/.style={
set fill color=orange!90, draw opacity=0.8,
set border color=orange!90, fill opacity=0.3,
},
style brown/.style={
set fill color=brown!70!orange!40, draw opacity=0.4,
set border color=brown, fill opacity=0.3,
},
style purple/.style={
set fill color=violet!90!pink!20, draw opacity=0.5,
set border color=violet, fill opacity=0.3,
},
cll/.style={
above left offset={-0.1,0.15},
below right offset={0.10,-0.17},
#1
},set fill color/.code={\pgfkeysalso{fill=#1}},
set border color/.style={draw=#1}
}
\begin{document}
\begin{tikzpicture}[baseline={-1ex}]
\matrix [mymatrix,inner sep=0pt] (m)
{
11 11&12&13&14\\
21&22&23&24\\
31&32&33&34\\
41&42&43&44\\
};
\node[rounded corners, style green, fit=(m-1-1) (m-1-4), inner sep=0pt] {};
\node[rounded corners, style orange, fit=(m-2-1) (m-2-3), inner sep=0pt] {};
\node[rounded corners, style cyan, fit=(m-3-1) (m-3-4), inner sep=0pt]{};
\node[rounded corners, style brown, fit=(m-4-2), inner sep=0pt]{};
\end{tikzpicture}
\vspace{1em}
\lipsum[1-1]
\end{document}