1

The following code comes from a solution to the question (How to make tables with a small cell to the right inside another large cell?), but if I add a header with text longer than what is established the table is distorted, I would like the width of the columns to fit the content of the table header.

\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}

Result

  • 1
    I have made two changes -- expand the node width nodes={minimum width=6em, -- and -- \usepackage{makecell} to break the long text into multi line of choice -- read the documentation for many more options available at -- http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/makecell/makecell.pdf – js bibra Feb 18 '21 at 04:13

2 Answers2

1

enter image description here

\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}

js bibra
  • 21,280
1

In original code long text one is inside a \makebox which is unbreakable. nodes are something similar to a parbox and can include multiline text, but a text width is needed for that. This is what is shown in following code. text width in main nodes, also fixes text width in inlay ones, so a new text width has been fixed there.

Also the matrix of math nodes has been changed to matrix of nodes and the table title has been included as a label in third column. This way first row can be eliminated.

\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}

enter image description here

Ignasi
  • 136,588