4

I have produced the following table with LateX, using the multirow and array packages. The final result is:

Transportation problem table

However, I noticed that the labels L1 and L2 are not quite vertically aligned with the entries $a_{11},a_{12}$, etc. Why is that? Is there anything wrong with my code (which has some code which was copied and pasted from other answers)?

\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}
5
  • Could you please make a compilable MWE that allows others to reproduce teh output you show? If I add a documentclass as well as the relevat packages to your code, I get the desired alignment.
    – leandriis
    Jun 5, 2019 at 18:21
  • @leandriis: I just edited the code. This MWE produces the problematic output.
    – Daniel
    Jun 5, 2019 at 20:46
  • 1
    Thanks for editing your question. If I compile your example on my computer, I get the following output: i.stack.imgur.com/QQulu.png that seems quite well aligned to me and differs from the screenshot you showed.
    – leandriis
    Jun 5, 2019 at 20:52
  • @leandriis: This is strange... Maybe I should update my LaTeX system? I'm using TeXLive 2016 (usually I don't update often, since LaTeX is already quite stable).
    – Daniel
    Jun 5, 2019 at 21:02
  • 1
    There have been major changes in the array package, you should definitely update. Jun 6, 2019 at 9:27

4 Answers 4

2
  • With your MWE and using recent version of array package I cant reproduce in question showed image of table. The baselines of "L1" , "a1" and \clines below "c" cells are vertically aligned. Do you have recent version of LaTeX installation? Read comments below question
  • Your code can be much shorter (the most of \multicolumns{1}{...}{...} are superfluous, adding few of \multicolumns{2}{c}{}` simplify a code):
\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}

enter image description here

1
  • That worked. The problem was that my LaTeX system was too old. I updated my LaTeX installation from TeXLive 2016 to TeXLive 2019, and everything worked fine (including my previous code). But you are right that I could have made the code shorter (I was too involved in trying to get it working). Thanks!
    – Daniel
    Jun 11, 2019 at 11:14
3

Look, ma! No 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}

enter image description here

2

A version of your tabular typeset using cals. This version two mimics the column width in the question (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}

enter image description here

2

Look, ma! No \LaTeX, no \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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .