3

I would like to know if there is a more efficient way to obtain the same result, I leave the code that I made which in my opinion is very extensive. Thank you for your answers.

Reference image

\begin{table}[H]
  \centering
    \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|}
    \hline
          & \multicolumn{11}{c|}{Destino} \\
    \hline
    Origen & \multicolumn{2}{c|}{1} & \multicolumn{2}{c|}{2} & \multicolumn{2}{c|}{3} & \multicolumn{2}{c|}{4} & \multicolumn{2}{c|}{5} & \multicolumn{1}{l|}{Oferta} \\
    \hline
    \multirow{2}[4]{*}{1} &       & 4    &       & 2    &       & 5    &       & \multicolumn{1}{c|}{5} &       & 1    & \multirow{2}[4]{*}{10} \\
\cline{3-3}\cline{5-5}\cline{7-7}\cline{9-9}\cline{11-11}          & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{22} & \multicolumn{2}{l|}{33} & \multicolumn{2}{l|}{44} & \multicolumn{2}{l|}{55} &  \\
    \hline
    \multirow{2}[4]{*}{2} &       & 2    &       & 1    &       & 4    &       & \multicolumn{1}{c|}{1} &       & 4    & \multirow{2}[4]{*}{12} \\
\cline{3-3}\cline{5-5}\cline{7-7}\cline{9-9}\cline{11-11}          & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{22} & \multicolumn{2}{l|}{33} & \multicolumn{2}{l|}{44} & \multicolumn{2}{l|}{55} &  \\
    \hline
    \multirow{2}[4]{*}{3} &       & 3    &       & 4    &       & 1    &       & \multicolumn{1}{c|}{2} &       & 1    & \multirow{2}[4]{*}{5} \\
\cline{3-3}\cline{5-5}\cline{7-7}\cline{9-9}\cline{11-11}          & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} &  \\
    \hline
    \multirow{2}[4]{*}{4} &       & 2    &       & 2    &       & 3    &       & \multicolumn{1}{c|}{4} &       & 2    & \multirow{2}[4]{*}{10} \\
\cline{3-3}\cline{5-5}\cline{7-7}\cline{9-9}\cline{11-11}          & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} & \multicolumn{2}{l|}{11} &  \\
    \hline
    Demanda & \multicolumn{2}{c|}{6} & \multicolumn{2}{c|}{8} & \multicolumn{2}{c|}{3} & \multicolumn{2}{l|}{9} & \multicolumn{2}{c|}{11} &  \\
    \hline
    \end{tabular}%
\end{table}%
  • 1
    https://tex.stackexchange.com/a/540553/197451 – js bibra Feb 16 '21 at 06:59
  • I have made the table matrix for the first two rows -- you just have to fill the content of the remaining rows using the same format -- see the answer below if it suits the requirement – js bibra Feb 16 '21 at 10:57

2 Answers2

2

Here is a solution with {NiceTabular} of nicematrix.

With that environment, PGF/Tikz nodes are constructed under the rows, columns and cells of the tabular.

I use that nodes to construct the small squares after the construction of the main tabular in the ``|code-after|'' of the environment {NiceTabular}.

\documentclass{article}
\usepackage{nicematrix,tikz}

\ExplSyntaxOn \makeatletter \cs_new_protected:Nn __cespedes_draw_angle:nnn { \begin{tikzpicture} \draw ([xshift=-4mm]#1-|#2) |- ([yshift=-4mm]#1-|#2) ; \node at ([xshift=-2mm,yshift=-2mm]#1-|#2) { #3 } ; \end{tikzpicture} }

\NewDocumentCommand{\Cell}{mm} { \vrule height 20 pt depth 0 pt width 0 pt % to increase the height of the cells \makebox[8mm]{#1;} \tl_gput_right:Nx \g_nicematrix_code_after_tl { __cespedes_draw_angle:nnn { \int_use:N \c@iRow } { \int_eval:n { \c@jCol + 1 } } { #2 } } } \makeatother \ExplSyntaxOff

\begin{document} \begin{NiceTabular}{ccccccc}[hvlines] & \Block{1-6}{Destino} &&&&&\ Origen & 1 & 2 & 3 & 4 & 5 & Oferta \ 1 & \Cell{11}{4} & \Cell{22}{2} & \Cell{33}{5} & \Cell{44}{5} & \Cell{55}{1} & 10 \ 2 & \Cell{11}{2} & \Cell{22}{1} & \Cell{33}{4} & \Cell{44}{1} & \Cell{55}{4} & 12 \ 3 & \Cell{11}{3} & \Cell{11}{4} & \Cell{11}{1} & \Cell{11}{2} & \Cell{11}{1} & 5 \ 4 & \Cell{11}{2} & \Cell{11}{2} & \Cell{11}{3} & \Cell{11}{4} & \Cell{11}{2} & 10 \ Demanda & 6 & 8 & 3 & 9 & 11 \end{NiceTabular} \end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

F. Pantigny
  • 40,250
  • It looks good, however I can't compile in Overleaf. I get the following error. https://app.box.com/s/szpc2mxatgeefljnug1woabpwai5h478 – Roger Céspedes Feb 17 '21 at 20:20
  • @Roger Céspedes: The version of nicematrix available by default on Overleaf is too old. You can upload the lastest version of the file nicematrix.sty on the SVN server of TeXLive (https:www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/nicematrix/nicematrix.sty) and upload the file in the repertory of your project in Overleaf. – F. Pantigny Feb 17 '21 at 20:53
1

enter image description here

\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){
    &   &   &   &\\
    Origen 
        & 1 
            &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