如何制作一个表格,其中的小单元格位于另一个大单元格的右侧?

如何制作一个表格,其中的小单元格位于另一个大单元格的右侧?

我想知道是否有更有效的方法来获得相同的结果,我留下了我编写的代码,在我看来,它非常广泛。谢谢你的回答。

参考图

\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

这是一个{NiceTabular}使用 的解决方案nicematrix

在该环境下,PGF/Tikz 节点在表格的行、列和单元格下构建。

我使用这些节点来构造小方块环境``|code-after|''中主表格的构建{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}

您需要多次编译(因为nicematrix使用 PGF/Tikz 节点)。

上述代码的输出

答案2

在此处输入图片描述

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

相关内容