\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,
%%
every even row/.style={
nodes={fill=gray!20}
},
column 1/.style={
nodes={text width=2em,font=\bfseries}
},
row 1/.style={
nodes={
fill=black,
text=white,
font=\bfseries
}
}
}
}
\begin{tikzpicture}
\matrix (first) [table,text width=6em]
{
& Monday & Tuesday & Wednesday & Thursday & Friday\\
1 & A & B & C & D & E \\
2 & F & G & H & J & K \\
3 & A & B & C & D & E \\
4 & F & G & H & J & K \\
};
\end{tikzpicture}
\end{document}
如果我在第一个单元格中写入“星期一是一天的第一天”而不是仅仅星期一,会怎么样?
我希望单元格垂直适应,增加整行的高度,以容纳文本
怎么做?
答案1
这是一种不太手动的方法,只需确定最大的测试在第一行将覆盖多少行,并相应地调整行高
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
table/.style={
matrix of nodes,
nodes in empty cells,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
rectangle,
draw=black,
minimum width=6em,
},
every even row/.style={
nodes={fill=gray!20}
},
row 1/.style={
nodes={draw,
fill=black,
text=white,
font=\bfseries,
align=center,
anchor=center,
minimum height=9ex, % Roughly 3ex for each line break
inner sep=0,
},
},
column 1/.style={
nodes={minimum width=2em,font=\bfseries},
},
row 1 column 6/.style={text width=4em,align=center,} % Align the width of the cell
}
}
\begin{document}
\begin{tikzpicture}
\matrix (first) [table]
{
& Monday & Tuesday & Wednesday & Thursday & Friday I'm in love\\
1 & A & B & C & D & E \\
2 & F & G & H & J & K \\
3 & A & B & C & D & E \\
4 & F & G & H & J & K \\
};
\end{tikzpicture}
\end{document}
抱歉,电台正在播放 The Cure。