![当插入的文本长度超过单元格的大小时,如何扩展表格?](https://linux22.com/image/264044/%E5%BD%93%E6%8F%92%E5%85%A5%E7%9A%84%E6%96%87%E6%9C%AC%E9%95%BF%E5%BA%A6%E8%B6%85%E8%BF%87%E5%8D%95%E5%85%83%E6%A0%BC%E7%9A%84%E5%A4%A7%E5%B0%8F%E6%97%B6%EF%BC%8C%E5%A6%82%E4%BD%95%E6%89%A9%E5%B1%95%E8%A1%A8%E6%A0%BC%EF%BC%9F.png)
\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。