4

I'm using a multirow environment to span text over two rows. As it does not support \\ for inserting a line break, I'm nesting it into a shortstack (see MWE below). However, the text is in neither of both cases (single line text, dual line text) really centered. What am I doing wrong?

Example Image

The red line indicates the center of the heading row (spans over two rows). As shown in the image, neither of both lines are really vertically centered.

enter image description here

MWE

\documentclass[a4paper,twocolumn]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{multirow}

\usepackage[a4paper,top=3cm,bottom=2cm,left=1.5cm,right=1.5cm,marginparwidth=1.75cm]{geometry}

\title{Your Paper}
\author{You}

\begin{document}
    \begin{table}[htbp]
        \caption{Classifier configurations and their performance.}
        \label{tblClassifierConfig}
        \begin{tabular*}{1.2\columnwidth}{@{\extracolsep{\fill}}lrllll@{}}
        \toprule
        \multirow{2}{*}{\raisebox{-\heavyrulewidth}{\shortstack[l]{\textbf{N-gram}\\as Range}}} & \multirow{2}{*}{\raisebox{-\heavyrulewidth}{\shortstack[l]{\textbf{Vocab. Size}\\in \#n-grams}}} & \multicolumn{2}{c}{Test Set 1} & \multicolumn{2}{c}{Test Set 2} \\ 
        \cmidrule(l){3-6} &  & \textbf{Prec.} & \textbf{Recall} & \textbf{Prec.} & \textbf{Recall} \\ \midrule
        (2,4)-char                        & 500                                 & 0.9999         & 0.9999          & 0.5085         & 0.5849          \\
        (2,4)-char                        & 1000                                & 0.9999         & 0.9999          & 0.5114         & 0.6132          \\
        (2,3)-word                        & 1,500                               & 0.9999         & 0.9999          & 0.5134         & 0.6268          \\
        (1,3)-word                        & 2,000                               & 0.9999         & 0.9999          & 0.5138         & 0.6307          \\ \bottomrule
        \end{tabular*}
    \end{table}
\end{document}
  • Never use \shortstack for serious work. – egreg Jul 30 '18 at 11:40
  • Can you give or refer to an explanation why \shortstack shouldn't be used for serious work? – Patrick Jul 30 '18 at 11:57
  • 1
    Try, side by side, \shortstack{a\\e} and \shortstack{p\\b} – egreg Jul 30 '18 at 12:43
2

in multirow cells you also need to consider vertical space introduced by \cmidrule. it is about 0.4 row height, so you can wrote

 \multirow{2.4}{*}{ ...}

i also suggest in columns with numbers to use S column type from siunitx package, not increase table width (it will protrude in next column). istead this rather reduce tabocolsep:

\documentclass[a4paper,twocolumn,english]{article}

\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage{booktabs, makecell, multirow}
\usepackage{siunitx}
\usepackage[a4paper,
            top=3cm,bottom=2cm,left=1.5cm,right=1.5cm,
             marginparwidth=1.75cm]{geometry}
\usepackage{lipsum}

\title{Your Paper}
\author{You}

\begin{document}
\lipsum[11]
\begin{table}[htbp]
    \caption{Classifier configurations and their performance.}
    \label{tblClassifierConfig}
    \setlength\tabcolsep{0pt}
    \begin{tabular*}{\columnwidth}{@{\extracolsep{\fill}}
                            l
                             S[table-format=4.0,
                               group-four-digits,
                               group-separator=\,]
                        *{4}{S[table-format=1.4]}
                                   }
    \toprule
    \multirow{2.4}{*}{\makecell{\textbf{N-gram}\\as Range}} 
        & {\multirow{2.4}{*}{\makecell{\textbf{Vocab. Size}\\in \#n-grams}}}
            & \multicolumn{2}{c}{Test Set 1} 
                & \multicolumn{2}{c}{Test Set 2} \\
    \cmidrule{3-4} 
    \cmidrule{5-6}
        &  & {\textbf{Prec.}} & {\textbf{Recall}} & {\textbf{Prec.}} & {\textbf{Recall}} \\ 
    \midrule
    (2,4)-char  &   500 & 0.9999    & 0.9999    & 0.5085    & 0.5849            \\
    (2,4)-char  & 1 000 & 0.9999    & 0.9999    & 0.5114    & 0.6132            \\
    (2,3)-word  & 1 500 & 0.9999    & 0.9999    & 0.5134    & 0.6268            \\
    (1,3)-word  & 2 000 & 0.9999    & 0.9999    & 0.5138    & 0.6307            \\ 
    \bottomrule
    \end{tabular*}
\end{table}
\end{document}

which gives

enter image description here

  • Thanks for your answer and the table type addition! The table now is almost perfect. Is there any way to make the line below "Test Set 1" and "Test Set 2" as long as the whole column width? The line below "Test Set 1" is a little shorter on the right side, and the one below "Test Set 2" a little shorter on the left side. – Patrick Jul 30 '18 at 12:00
  • @Patrick, just delete (r) and ˙(ll)`. i corrected code. – Zarko Jul 30 '18 at 12:10
2

A variant with \multirowcell. The makecell package allows for line breaks in standard cells, and a common formatting (not used here). Please note your table is larger than the column width, so it will overflow into the column on the right. I loaded caption to have a sensible vertical spacing between caption and table. Also, I wonder what a \marginparwidth of 1.75cm means when the horizontal margins are set to 1.5cm.

\documentclass[a4paper,twocolumn]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{multirow}
\usepackage{booktabs, makecell, caption}
\captionsetup{skip=6pt}
\usepackage[a4paper,top=3cm, bottom=2cm, hmargin=1.5cm,marginparwidth=1.75cm, showframe]{geometry}

\usepackage{lipsum}
\title{Your Paper}
\author{You}

\begin{document}
\lipsum[11]

    \begin{table}[!htbp]
        \caption{Classifier configurations and their performance.}
        \label{tblClassifierConfig}
        \begin{tabular*}{1.2\columnwidth}{@{\extracolsep{\fill}}lrllll@{}}
        \toprule
        \multirowcell{2.3}{\textbf{N-gram}\\as Range} & \multirowcell{2.3}{\textbf{Vocab. Size}\\in \#n-grams} & \multicolumn{2}{c}{Test Set 1} & \multicolumn{2}{c}{Test Set 2} \\
        \cmidrule(l){3-6} & & \textbf{Prec.} & \textbf{Recall} & \textbf{Prec.} & \textbf{Recall} \\ \midrule
        (2,4)-char & 500 & 0.9999 & 0.9999 & 0.5085 & 0.5849 \\
        (2,4)-char & 1000 & 0.9999 & 0.9999 & 0.5114 & 0.6132 \\
        (2,3)-word & 1,500 & 0.9999 & 0.9999 & 0.5134 & 0.6268 \\
        (1,3)-word & 2,000 & 0.9999 & 0.9999 & 0.5138 & 0.6307 \\ \bottomrule
        \end{tabular*}
    \end{table}

\end{document} 

enter image description here

2

Using stackengine, I change \shortstack to a long \stackanchor, but (importantly) also added a 4pt vertical buffer above and below the stack.

\documentclass[a4paper,twocolumn]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[usestackEOL]{stackengine}
\usepackage[a4paper,top=3cm,bottom=2cm,left=1.5cm,right=1.5cm,marginparwidth=1.75cm]{geometry}
\def\stacktype{L}
\setstackgap{S}{4pt}
\title{Your Paper}
\author{You}

\begin{document}
    \begin{table}[htbp]
        \caption{Classifier configurations and their performance.}
        \label{tblClassifierConfig}
        \begin{tabular*}{1.2\columnwidth}{@{\extracolsep{\fill}}lrllll@{}}
        \toprule
        \multirow{2}{*}{\raisebox{-\heavyrulewidth}{\addstackgap{\stackanchor{\textbf{N-gram}}{as Range}}}} & \multirow{2}{*}{\raisebox{-\heavyrulewidth}{\addstackgap{\stackanchor{\textbf{Vocab. Size}}{in \#n-grams}}}} & \multicolumn{2}{c}{Test Set 1} & \multicolumn{2}{c}{Test Set 2} \\ 
        \cmidrule(l){3-6} &  & \textbf{Prec.} & \textbf{Recall} & \textbf{Prec.} & \textbf{Recall} \\ \midrule
        (2,4)-char                        & 500                                 & 0.9999         & 0.9999          & 0.5085         & 0.5849          \\
        (2,4)-char                        & 1000                                & 0.9999         & 0.9999          & 0.5114         & 0.6132          \\
        (2,3)-word                        & 1,500                               & 0.9999         & 0.9999          & 0.5134         & 0.6268          \\
        (1,3)-word                        & 2,000                               & 0.9999         & 0.9999          & 0.5138         & 0.6307          \\ \bottomrule
        \end{tabular*}
    \end{table}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.