怎样才能创建一个以对角线元素突出显示的表格呢?
我使用的例子发现这里使用这段(不太好的)代码来获得上图
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
rectangle,
draw=black,
align=center
},
minimum height=1.5em,
text depth=0.5ex,
text height=2ex,
nodes in empty cells,
row 1 column 1/.style={
nodes={fill=gray!30}
},
row 2 column 2/.style={
nodes={fill=gray!30}
},
row 2 column 3/.style={
nodes={fill=gray!15}
},
row 3 column 2/.style={
nodes={fill=gray!15}
},
row 4 column 5/.style={
nodes={fill=gray!15}
},
row 5 column 4/.style={
nodes={fill=gray!15}
},
row 6 column 7/.style={
nodes={fill=gray!15}
},
row 7 column 6/.style={
nodes={fill=gray!15}
},
row 3 column 3/.style={
nodes={fill=gray!30}
},
row 4 column 4/.style={
nodes={fill=gray!30}
},
row 5 column 5/.style={
nodes={fill=gray!30}
},
row 6 column 6/.style={
nodes={fill=gray!30}
},
row 7 column 7/.style={
nodes={fill=gray!30}
}
}
}
\begin{tikzpicture}
\matrix (first) [table,text width=6em]
{
SO & $f(x,y,z)$ & & & & &\\
& AS & B
& C & D & & \\
2 & F & 3D & H & J&& \\
3 & A & B & DS & D&& \\
4 & F & G & H & 4D &&\\
5 & F & G & H & I & PS& \\
6 & F & G & H & I & & 4D\\
};
\end{tikzpicture}
\end{document}
但当我尝试在单元格中输入更多数据时,例如
我希望它调整大小(高度)以适合文本,并调整所有单元格的高度和宽度。我应该怎么做?
另外,有没有办法让一个小箭头从 AS -> B 和从 B -| 3D 指向?
答案1
您可以使用
nodes={
rectangle,
draw=black,
align=center,
text width = 2cm, %%% adjust this
minimum height=4.5em,
anchor=south,
% text depth=0.5ex,
% text height=2ex,
},
代码:
\documentclass[varwidth,tikz,border=5pt]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
rectangle,
draw=black,
align=center,
text width = 2cm, %%% adjust this
minimum height=4.5em,
anchor=south,
% text depth=0.5ex,
% text height=2ex,
},
nodes in empty cells,
row 1 column 1/.style={
nodes={fill=gray!30}
},
row 2 column 2/.style={
nodes={fill=gray!30}
},
row 2 column 3/.style={
nodes={fill=gray!15}
},
row 3 column 2/.style={
nodes={fill=gray!15}
},
row 4 column 5/.style={
nodes={fill=gray!15}
},
row 5 column 4/.style={
nodes={fill=gray!15}
},
row 6 column 7/.style={
nodes={fill=gray!15}
},
row 7 column 6/.style={
nodes={fill=gray!15}
},
row 3 column 3/.style={
nodes={fill=gray!30}
},
row 4 column 4/.style={
nodes={fill=gray!30}
},
row 5 column 5/.style={
nodes={fill=gray!30}
},
row 6 column 6/.style={
nodes={fill=gray!30}
},
row 7 column 7/.style={
nodes={fill=gray!30}
}
}
}
\begin{tikzpicture}
\matrix (first) [table]
{
SO & $f(x,y,z)$, $g(a,b,c)$, & & & & &\\
& AS & B
& C & D & & \\
2 & F & 3D & H & J&& \\
3 & A & B & DS & D&& \\
4 & F & G & H & 4D &&\\
5 & F & G & H & I & PS& \\
6 & F & G & H & I & & 4D\\
};
\begin{scope}
\draw[latex-] ([xshift=5ex]first-2-3.west)--([xshift=-5ex]first-2-2.east);
\draw[-latex] ([xshift=-5ex]first-2-3.east) -- ++(4ex,0) |- ([xshift=-5ex]first-3-3.east) ;
\end{scope}
\end{tikzpicture}
\end{document}