多行合并单元格的垂直对齐

多行合并单元格的垂直对齐

我使用 LateX 的多行和数组包制作了下表。最终结果是:

运输问题表

但是,我注意到标签 L1 和 L2 与条目 $a_{11}、a_{12}$ 等垂直对齐不完全。这是为什么?我的代码有什么问题吗(其中有一些代码是从其他答案复制粘贴的)?

\documentclass{article}
\usepackage{multirow}
\usepackage{array} 
\newcommand{\nocorner}{1cm}
\newcommand{\corner}{6mm}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{table}
    \setlength\extrarowheight{3pt}
    \begin{tabular}
        [c]{c C{\nocorner} C{\corner} C{\nocorner} C{\corner} C{\nocorner} C{\corner}}
         & \multicolumn{2}{c}{$H1$} & \multicolumn{2}{c}{$H2$} & \multicolumn{2}{c}{$H3$}\\\cline{2-7}
        \multicolumn{1}{c|}{\multirow{2}{*}{$L1$}} &
         \multicolumn{1}{C{\nocorner}}{\multirow{2}{*}{$a_{11}$}} & \multicolumn{1}{|C{\corner}|}{$c_{11}$} & \multicolumn{1}{C{\nocorner}}{\multirow{2}{*}{$a_{12}$}} & \multicolumn{1}{|C{\corner}|}{$c_{12}$} & \multicolumn{1}{C{\nocorner}}{\multirow{2}{*}{$a_{13}$}} & \multicolumn{1}{|C{\corner}|}{$c_{13}$} \\\cline{3-3}\cline{5-5}\cline{7-7}
         \multicolumn{1}{l|}{} & \multicolumn{1}{C{\nocorner}}{} & \multicolumn{1}{C{\corner}|}{} & \multicolumn{1}{C{\nocorner}}{} & \multicolumn{1}{C{\corner}|}{} & \multicolumn{1}{C{\nocorner}}{} & \multicolumn{1}{C{\corner}|}{} \\\cline{2-7}
         \multicolumn{1}{c|}{\multirow{2}{*}{$L2$}} &
         \multicolumn{1}{C{\nocorner}}{\multirow{2}{*}{$a_{21}$}} & \multicolumn{1}{|C{\corner}|}{$c_{21}$} & \multicolumn{1}{C{\nocorner}}{\multirow{2}{*}{$a_{22}$}} & \multicolumn{1}{|C{\corner}|}{$c_{22}$} & \multicolumn{1}{C{\nocorner}}{\multirow{2}{*}{$a_{23}$}} & \multicolumn{1}{|C{\corner}|}{$c_{23}$} \\\cline{3-3}\cline{5-5}\cline{7-7}
         \multicolumn{1}{l|}{} & \multicolumn{1}{C{\nocorner}}{} & \multicolumn{1}{C{\corner}|}{} & \multicolumn{1}{C{\nocorner}}{} & \multicolumn{1}{C{\corner}|}{} & \multicolumn{1}{C{\nocorner}}{} & \multicolumn{1}{C{\corner}|}{} \\\cline{2-7}
    \end{tabular}
    \setlength\extrarowheight{0pt}
\end{table}

\end{document}

答案1

  • 使用您的 MWE 并使用最新版本的array软件包,我无法重现问题中显示的表格图像。“L1”、“a1”和\clines下方“c”单元格的基线是垂直对齐的。您是否安装了最新版本的 LaTeX?阅读问题下方的评论
  • 您的代码可以更短(大多数\multicolumns{1}{...}{...}都是多余的,添加几个 \multicolumns{2}{c}{}` 可以简化代码):
\documentclass{article}
\usepackage{multirow}
\usepackage{array}
\newcommand{\nocorner}{1cm}
\newcommand{\corner}{6mm}
\newcommand\mcc{\multicolumn{2}{c|}{}}  % new

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{table}
    \setlength\extrarowheight{3pt}
    \begin{tabular}{c | *{3}{C{\nocorner}|C{\corner}|} }
    \multicolumn{1}{c}{}
         & \multicolumn{2}{c}{$H1$} & \multicolumn{2}{c}{$H2$} & \multicolumn{2}{c}{$H3$}   \\ \cline{2-7}
    \multirow{2}{*}{$L1$}
        & \multirow{2}{*}{$a_{11}$}
            & $c_{11}$
                & \multirow{2}{*}{$a_{12}$} 
                    & $c_{12}$
                        & \multirow{2}{*}{$a_{13}$}
                            & $c_{13}$  \\
        \cline{3-3}\cline{5-5}\cline{7-7}
        & \mcc & \mcc & \mcc            \\
        \cline{2-7}
%
    \multirow{2}{*}{$L2$}
        & \multirow{2}{*}{$a_{21}$}
            & $c_{21}$
                & \multirow{2}{*}{$a_{22}$}
                    & $c_{22}$
                        & \multirow{2}{*}{$a_{23}$}
                            & $c_{23}$  \\
        \cline{3-3}\cline{5-5}\cline{7-7}
        & \mcc & \mcc & \mcc            \\
        \cline{2-7}
    \end{tabular}
    \setlength\extrarowheight{0pt}
\end{table}

\end{document}

在此处输入图片描述

答案2

看,妈妈!不multirow;-)

\documentclass{article}
\usepackage{array,hhline}

\newcommand{\corner}[1]{%
  \begin{array}{@{}|c}
  #1\vphantom{\Big|}\\
  \hhline{|-}
  \multicolumn{1}{c}{\vphantom{\Big|}}\end{array}%
}
\newcommand{\entry}[1]{\quad#1\quad}

\begin{document}

$\begin{array}{@{}r@{\hspace{\arraycolsep}\vline}*{3}{c@{\vline}}}
 \multicolumn{1}{@{}c@{}}{}& \multicolumn{1}{c}{H1} & \multicolumn{1}{c}{H2} & \multicolumn{1}{c}{H3} \\
 \hhline{~|---}
 L1 & \entry{a_{11}}\corner{c_{11}}  & \entry{a_{12}}\corner{c_{12}} & \entry{a_{13}}\corner{c_{13}} \\
 \hhline{~|---}
 L2 & \entry{a_{21}}\corner{c_{21}}  & \entry{a_{22}}\corner{c_{22}} & \entry{a_{23}}\corner{c_{23}} \\
 \hhline{~|---}
 \end{array}$

\end{document}

在此处输入图片描述

答案3

使用以下版本的表格排版卡路里此版本二模仿了问题中的列宽(10mm/6mm+2tabcolsep+arrayrulewidth):


\documentclass{article}
\usepackage{cals}

\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\newcommand{\corner}{\dimexpr(6mm+12.4pt)\relax}          % Same column width as in the question
\newcommand{\noncorner}{\dimexpr(10mm+12.4pt)\relax} % Comepnsated for  and tabcolsep

\begin{document}

\begin{calstable}

% Defining seven columns relativ to each other and relativ to the margins.
\colwidths{{10mm}
            {\noncorner}
            {\corner}
            {\noncorner}
            {\corner}
            {\noncorner}
            {\corner}
    }

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}


\def\tb{\ifx\cals@borderT\relax      % Top border switch (off-on)
    \def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}

\def\bb{\ifx\cals@borderB\relax      % Botton border switch (off-on)
    \def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

\def\rb{\ifx\cals@borderR\relax      % Right border switch (off-on)
    \def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}

\def\lb{\ifx\cals@borderL\relax      % Left border switch (off-on)
    \def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}


% R1
\brow
    \rb\lb\tb\cell{}
    \nc{ltb}
    \nc{rtb}\alignC\sc{$H1$}
    \nc{ltb}
    \nc{rtb}\sc{$H3$}
    \nc{ltb}
    \nc{rtb}\sc{$H3$}\tb\lb\rb
\erow
% R2 Body
\brow
    \lb\tb\nc{lrt}\tb\lb
    \nc{lrt}
    \cell{$c11$}
    \nc{lrt}
    \cell{$c12$}
    \nc{lrt}
    \cell{$c13$}
\erow
% R3 Body
\brow
    \bb\lb\nc{lrb}\alignR\sc{\vfil $L1$}\lb\bb
    \alignC\nc{lrb}\sc{\vfil $a11$}
    \lb\cell{}\lb
    \nc{lrb}\sc{\vfil $a12$}
    \lb\cell{}\lb
    \nc{lrb}\sc{\vfil $a13$}
    \lb\cell{\rule{\baselineskip}{0pt}}\lb
\erow
% R4 Body
\brow
    \lb\nc{lrt}\lb
    \nc{lrt}
    \cell{$c21$}
    \nc{lrt}
    \cell{$c22$}
    \nc{lrt}
    \cell{$c23$}
\erow
% R5 Body
\brow
    \bb\lb\nc{lrb}\alignR\sc{\vfil $L2$}\lb\bb
    \alignC\nc{lrb}\sc{\vfil $a21$}
    \lb\cell{}\lb
    \nc{lrb}\sc{\vfil $a22$}
    \lb\cell{}\lb
    \nc{lrb}\sc{\vfil $a23$}
    \lb\cell{\rule{\baselineskip}{0pt}}\lb
\erow
\makeatletter
\end{calstable}\par % \par needed to align the tabular

\end{document}

在此处输入图片描述

答案4

看,妈妈!不\LaTeX,不\halign;-)

\newdimen\colw \colw=2cm

\def\cell#1, #2 {\vrule depth2ex height3ex \kern-.4pt 
   \hbox to\colw{\hfil$#1$\hfil\raise1ex\vtop{
      \hbox to.4\colw{\vrule height2ex depth.7ex\hss$#2$\hss}\hrule}}}
\def\nllap{\noindent\llap}
\def\ch#1 {\hbox to\colw{\hfil$#1$\hfil}}

\moveright\parindent\vbox{
   \hbox{\ch H1 \ch H2 \ch H3 }\smallskip
   \hrule
   \hbox{\llap{$L1$\enspace}\cell a_{11}, c_{11}
                            \cell a_{12}, c_{12}
                            \cell a_{13}, c_{13} \vrule}
   \hrule
   \hbox{\llap{$L1$\enspace}\cell a_{21}, c_{21}
                            \cell a_{22}, c_{22}
                            \cell a_{23}, c_{23} \vrule}
   \hrule
}

\bye

相关内容