1

Using LaTeX I want to layout a catalogue of exam question with the following requirements:

  • All cell contents should be centered vertically
  • Question and answer cells should have their text left-aligned
  • and the remaining cells should have their text centered

This is the desired layout (the X marks the correct answer):

Pic1

That output was actually produced by my LaTeX code (attached below) but it does not deal well when the questions and/or answers are longer than in that example.

I tried two ways of formatting the table:

  1. Using \multirow{3}{=} to have the question span three rows and \multicolumn{3} for the "Possible answers" heading
  2. Using a nested table for the "Possible answers" stuff

Note that I am using the tabu package because I might consider using the longtabu environment to deal with multi-page tables.

This is my current LaTeX output and the corresponding code is attached below.

Pic2

\documentclass[draft]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{tabu}

\newcommand\question[4]{
    \multirow{3}{=}{#1} & #2 & A & \\
    \cline{2-4}
    & #3 & B & X \\
    \cline{2-4}
    & #4 & C & \\
    \hline
}
\tabulinesep=2.2mm

\begin{document}
    Attempt using \texttt{\textbackslash multirow}: \\

    \begin{tabu}{|X[l]|X[l]|c|c|}
        \hline
        \multicolumn{1}{|c|}{Question} & \multicolumn{3}{c|}{Possible answers} \\
        \hline

        \question{This is a short and simple question}
        {First answer}
        {Second answer}
        {Third answer}

        \question{This is a longer question which is still properly centered vertically because the answers are single-line}
        {First answer}
        {Second answer}
        {Third answer}

        \question{However, if this question gets even longer, it crosses the cell's border. This is probably an incompatibility between multicolumn and the tabu package}
        {First answer}
        {Second answer}
        {Third answer}

        \question{Another short question but not properly centered vertically because the first answer is multi-line}
        {First very very very long answer spanning multiple lines}
        {Second answer}
        {Third answer}
    \end{tabu}

    \vspace{1cm}
    Trying to get rid of \texttt{\textbackslash multirow} by nesting tables produces unwanted padding: \\

    \begin{tabu}{|X[l]|X[l]}
        \hline
        \multicolumn{1}{|c|}{Question} & \multicolumn{1}{c|}{Possible answers} \\
        \hline

        This is an attempt using nested tables & \begin{tabu}{X|c|c|}
            First answer & A & \\
            \hline
            Second answer & B & X \\
            \hline
            Third answer & C &
        \end{tabu} \\
        \hline
    \end{tabu}
\end{document}

Do you have an idea of how the desired layout can be achieved?
Thanks in advance!

6
  • 1
    Although this won't fix the issue here, but you might want to consider using tabularx instead of tabu and xltabular instead of longtabu.
    – leandriis
    Jan 15, 2021 at 19:28
  • Any advantages/disadvantages? I wasn't aware that there's multi-page table support with tabularx in which case it would definitely be an option.
    – shilch
    Jan 15, 2021 at 19:31
  • 1
    tabu is currently unmaintained and can cause quite some issues. tabularx does not suppost multiple page tables, but xltabular does, so this package might be an alternative for your case.
    – leandriis
    Jan 15, 2021 at 19:33
  • The probably easiest way to avoid \multirow from spilling into the following cell is by adjusting the column widths accordingly. You could make the first column wider while simultaneously reducing the width of the second column.
    – leandriis
    Jan 15, 2021 at 19:33
  • 1
    Generally, \multirow requires some manual adjustments since its first argument is the number of lines of text the \multirow spans, not the number of table rows.
    – leandriis
    Jan 15, 2021 at 19:37

1 Answer 1

3

I have always used the tabu package and although it is abandoned and currently has problems when you want to color the background of the cells, it still works, since it is an extension of the packages for tables only that automates and provides controls for the style and size distribution of the columns, things of taste.

The following code has updated the command you use to not rewrite so much although it removes adjustment options for each row, added a variable to control the vertical position vpos of the multirow command \multirow[vpos]{nrows}[bigstruts]{width}[vmove]{text}, and added a phantom rule according to the multirow manual page 14 , code that can be turned on or off using a conditional value. I also put the multiline text in a \parbox that allows it to be justified.

RESULT:

enter image description here

MWE:

\documentclass[draft]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
%Packages for beautifull tables.
\usepackage{tabu}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{array}
\usepackage[longtable]{multirow}
\usepackage[table]{xcolor}
\newcommand\question[7]{
    \ifnum#6=1\rule[#7]{0pt}{20pt} \fi
    \multirow{#5}{8cm}{\parbox{8cm}{#1}} & #2 & A & \\  [-\TableLineSize] \tabucline{2-4}
    \ifnum#6=1\rule[#7]{0pt}{20pt} \fi
    & #3 & B & X \\ [-\TableLineSize] \tabucline{2-4} 
    \ifnum#6=1\rule[#7]{0pt}{20pt} \fi
    & #4 & C & \\ \tabucline -
}

\begin{document}
\begin{table}[!h]
    \def\TableLineSize{1.5pt}   
    \small
    \tabulinesep = 5pt 
    \tabulinestyle{\TableLineSize cyan!50!lime}
    \begin{tabu} to \linewidth{|X[10lm]|X[5lm]|X[0.5cm]|X[0.5cm]|}
        \tabucline - 
        \multicolumn{1}{|c|}{Question} & \multicolumn{3}{c|}{Possible answers} \\ \tabucline - 
        
        \question{This is a short and simple question}
        {First answer }
        {Second answer}
        {Third answer}{3.2}{0}{0pt}
        \question{This is a longer question which is still properly centered vertically because the answers are single-line.}
        {First answer}
        {Second answer}
        {Third answer}{3.2}{0}{0pt}
        
        \question{Thowever, if this question gets even longer, it crosses the cell's border. This is probably an incompatibility between multicolumn and the tabu package {\color{red}{[Not true, the problem is that the height is estimated according to the number of rows that are joined and is controlled by the veriable vpos]} }Then in case the text exceeds the space given by the rows, in the multirrow manual it indicates the trick that you can use to make these rows increase the space to fit the multi-row text.}
        {First answer}
        {Second answer}
        {Third answer}{-1.2}{1}{10pt}
        
        \question{Another short question but not properly centered vertically because the first answer is multi-line  {\color{red}{[Not now...]} }}
        {First very very very long answer spanning multiple lines}
        {Second answer}
        {Third answer}{2}{0}{20pt}
    \end{tabu}
    \caption{Beautiful table using tabu}
    \label{tab:tab1}
\end{table}

\end{document}
1
  • Thanks for your answer, I like the result! However, the issue I have with the code is that it requires manual positioning. I'm looking for a solution where I don't have to provide any measurements myself since the document is auto-generated.
    – shilch
    Jan 16, 2021 at 17:29

You must log in to answer this question.

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