以下代码来自该问题的解决方案(如何制作一个表格,其中的小单元格位于另一个大单元格的右侧?),但如果我添加的标题文本比既定的文本长,表格就会扭曲,我希望列的宽度能够适合表格标题的内容。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{matrix, fit}
\begin{document}
\begin{tikzpicture}[
row 1/.style={nodes={draw=none}},
mmat/.style={matrix of math nodes,nodes in empty cells,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
nodes={minimum width=5.5em,minimum height=3.5em, draw,anchor=center, text depth=0.25ex,text height=0.8em}},
inlay/.style={label={[draw,thin,anchor=north east,minimum width=0em, minimum height=0em,inner sep=1.4pt]north east:#1}}]
\matrix[mmat] (mat){
& & & &\\
\mbox{long text one}
& \mbox{long text two}
&2
&3
&4
&5
&Oferta \\
1
& |[inlay=4]|11
& |[inlay=2]| 22
& |[inlay=5]|33
& |[inlay=5]| 44
& |[inlay=1]|55
&100 \\
};
\node[yshift=-1mm, fit=(mat-1-3)(mat-1-5)]{Destino};
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz,border=3mm]{standalone}
\usepackage{amsmath, makecell}
\usetikzlibrary{matrix, fit}
\begin{document}
\begin{tikzpicture}[
row 1/.style={nodes={draw=none}},
mmat/.style={matrix of math nodes,nodes in empty cells,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
nodes={minimum width=6em,minimum height=3.5em, draw,anchor=center, text depth=0.25ex,text height=0.8em}},
inlay/.style={label={[draw,thin,anchor=north east,minimum width=0em, minimum height=0em,inner sep=1.4pt]north east:#1}}]
\matrix[mmat] (mat){
& & & &\\
\makecell[l]{long \\text\\ one}
& \makecell[c]{long\ text\\ one}
&2
&3
&4
&5
&Oferta \\
1
& |[inlay=4]|11
& |[inlay=2]| 22
& |[inlay=5]|33
& |[inlay=5]| 44
& |[inlay=1]|55
&100 \\
};
\node[yshift=-1mm, fit=(mat-1-3)(mat-1-5)]{Destino};
\end{tikzpicture}
\end{document}
答案2
原始代码long text one
位于 a 内,\makebox
它是不可破坏的。snode
类似于 a parbox
,可以包含多行文本,但text width
为此需要 a。这就是以下代码中显示的内容。text width
在主节点中,也修复了text width
其中的内容,因此在那里修复了inlay
新的内容。text width
此外,matrix of math nodes
已将 改为matrix of nodes
,表格标题已作为label
第三列中的 包含。这样可以消除第一行。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{matrix, fit}
\begin{document}
\begin{tikzpicture}[
% row 1/.style={nodes={draw=none}},
mmat/.style={matrix of nodes,nodes in empty cells,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
nodes={minimum width=5.5em, minimum height=3.5em, draw, anchor=center, text width=4.5em, align=center}},
inlay/.style={label={[draw, thin, anchor=north east, minimum width=0em, minimum height=0em, inner sep=1.4pt, text width=2em]north east:#1}}]
\matrix[mmat] (mat){
% & & & &\\
long text one
& long text two
&2
&|[label=Destino]|3
&4
&5
&Oferta \\
1
& |[inlay=4]|11
& |[inlay=2]| 22
& |[inlay=5]|33
& |[inlay=5]| 44
& |[inlay=1]|55
&100 \\
};
% \node[yshift=-1mm, fit=(mat-1-3)(mat-1-5)]{Destino};
\end{tikzpicture}
\end{document}