4

Consider this example where I am trying to draw vertical line through part of the table:

\documentclass{memoir}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{graphicx}

\begin{document}
    \begin{tabular}{lrrrr}
    \toprule
     & &  \multicolumn{3}{c}{Actual} \\
     \cmidrule(lr){3-5}
      &           & Melanoma & Carcinoma & Normal \\
     \multirow{3}{*}{\multicolumn{1}{l|}{\rotatebox[origin=c]{90}{\footnotesize Predicted}}}
                                & Melanoma  & 30       & 2         & 0      \\  
                                & Carcinoma & 5        & 28        & 0      \\
                                & Normal    &  0       & 5         & 35     \\
    \bottomrule
    \end{tabular}
\end{document}

When running it I get:

! Misplaced \omit.
\multispan ->\omit
                   \@multispan
l.12 ...x[origin=c]{90}{\footnotesize Predicted}}}

?

If I just hit Enter and move on it produces this nice table:

enter image description here

which looks almost but not quite like what I want. (Seems the vertical line width is too thick) but what is all the omit thing about? Why is that there? And how can I fix it?

4
  • 3
    you can't use \multicolumn in the argument of a \multirow. \multicolumn must be at the begin of a cell. Oct 10, 2022 at 13:56
  • 1
    @UlrikeFischer I don't think I understand what you are saying. Or I understand what you are saying but I don't think I understand how to use your wisdom... :)
    – jonalv
    Dec 2, 2022 at 8:28
  • 1
    @UlrikeFischer Expand to an answer?
    – Joseph Wright
    Dec 2, 2022 at 8:42
  • Hm, I have just realised I most probably asked two questions at the same time here. Didn't really think of it as that at the time. And now I got two answers, one for each question, and don't know which one to accept... This just shows there is good reason to only ask one question. :/
    – jonalv
    Dec 21, 2022 at 10:22

2 Answers 2

6
+50

You cannot have \multicolumn inside \multirow. Only the other way around.

I add a different realization of the table that doesn't require readers to tilt their head.

The width of rules in tabular (those inserted via | characters) is \arrayrulewidth. For \cmidrule, booktabs uses \cmidrulewidth.

\documentclass{memoir}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{siunitx} % for the second realization

\setlength{\arrayrulewidth}{\cmidrulewidth}% for the first realization

\begin{document}

\begin{tabular}{lrrrr}
\toprule
& & \multicolumn{3}{c}{Actual} \\
\cmidrule(lr){3-5}
& & Melanoma & Carcinoma & Normal \\
\multicolumn{1}{l|}{\multirow{3}{*}{\rotatebox[origin=c]{90}{\footnotesize Predicted}}}
& Melanoma  & 30       & 2         & 0      \\
\multicolumn{1}{l|}{}
& Carcinoma & 5        & 28        & 0      \\
\multicolumn{1}{l|}{}
& Normal    &  0       & 5         & 35     \\
\bottomrule
\end{tabular}

\bigskip

\begin{tabular}{
  @{}
  l
  *{3}{S[table-format=2.0]}
  @{}
}
\toprule
Predicted & \multicolumn{3}{c}{Actual} \\
\cmidrule(l){2-4}
          & {Melanoma} & {Carcinoma} & {Normal} \\
\midrule
Melanoma  & 30         &  2          &  0       \\
Carcinoma &  5         & 28          &  0       \\
Normal    &  0         &  5          & 35       \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

1
  • here's one way to fix it, but not why it misfails...
    – jarnosz
    Dec 6, 2022 at 21:27
6

The tables are created in TeX (typically) by \halign primitive. This primitive has (roughly speaking) following syntax

\halign { premable1 # postamble1 & preamble2 # postamble2 \cr
            data11 & data12 \cr
            data21 & data22 \cr
}

This pseudo-example above creates a table:

 preamble1 data11 postamble1   preamble2 data12 postamble2
 preamble1 data21 postamble1   preamble2 data22 postamble2

Sometimes users want to omit the preamble-postamble for individual item in the table (or more items side by side in the table). TeX is ready to comply with the request if the data part of the table item begins with the \omit primitive. TeX starts reading the data part in a special mode where it expands input stream and ignores spaces. If the first non-space after expansion is \omit, then preamble-postamble are omitted. Else TeX returns to the preamble, expands it and then follows with reading the data of the item. If the \omit primitive is not first object of the item then TeX prints misplaced \omit error and ignores it.

The \multicoumn LaTeX macro expands to the first item \omit, because user want to omit the pramble-postamble here. For example r in the LaTeX preamble of table is trasformed to low-level preamble for \halign but user writes \multicolumn{2}{c}, i.e. c, no r. Moreower, if there is 2 then user want to span over two columns and the macro emits \span primitive (but is is not important for now). The LaTeX macro \mutlirow creates a \vbox, it means that if \multirow is first object in the table item then it does'n expands to \omit and the premable is normally used. But you have written \multispan after \multirow, i.e. inside a \vbox. The \multispan macro expands to \omit but this is not allowed place for using \omit. You get the error.

1
  • here is why/how it works, but not how to fix it.
    – jarnosz
    Dec 6, 2022 at 21:27

You must log in to answer this question.

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