2

I have a table that serves as an answer sheet in an exam. This is a legacy from previous year, when there were 4 sections in the exam. However, I now have 3 sections...so I want to remove the Section IV column (so basically 6 columns). Easy peasy, I thought...just remove 6 &representing column border from the end of each row (and do the corresponding for the 3 title rows). Unfortunately, that does not do the trick :-( The problem is that I dont understand the role of arraystretch and linewidth/tabcolsep up top...

To clarify, I do want the 3 sections to expand to take on the original table's width...

{\rowcolors{5}{black!15}{black!5} 
\def\arraystretch{1.8}% 
\begin{tabular}{ |p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|p{\dimexpr 0.0416667\linewidth-2\tabcolsep}|  } 
\hline 
\multicolumn{6}{|c|}{\textbf{SECTION I}} & \multicolumn{6}{|c|}{\textbf{SECTION II}} & \multicolumn{6}{|c|}{\textbf{SECTION III}} & \multicolumn{6}{|c|}{\textbf{SECTION IV}} \\
 \multicolumn{6}{|c|}{\textbf{Name 1}} & \multicolumn{6}{|c|}{\textbf{Name 2}} & \multicolumn{6}{|c|}{\textbf{Name 3}} & \multicolumn{6}{|c|}{\textbf{Name 4}} \\
 \multicolumn{6}{|c|}{\textbf{Analysis}} & \multicolumn{6}{|c|}{\textbf{Analysis}} & \multicolumn{6}{|c|}{\textbf{Analysis}} & \multicolumn{6}{|c|}{\textbf{Analysis}} \\
 \hline 
\# & A & B & C & D & E & \# & A & B & C & D & E & \# & A & B & C & D & E & \# & A & B & C & D & E & 
\hline\hline 
1 &   &   &   &   &   & 1 &   &   &   &   &   & 1 &   &   &   &   &   & 1 &   &   &   &   &   & 
\hline 
2 &   &   &   &   &   & 2 &   &   &   &   &   & 2 &   &   &   &   &   & 2 &   &   &   &   &   & 
\hline 
3 &   &   &   &   &   & 3 &   &   &   &   &   & 3 &   &   &   &   &   & 3 &   &   &   &   &   & 
\hline 
4 &   &   &   &   &   & 4 &   &   &   &   &   & 4 &   &   &   &   &   & 4 &   &   &   &   &   & 
\hline 
5 &   &   &   &   &   & 5 &   &   &   &   &   & 5 &   &   &   &   &   & 5 &   &   &   &   &   & 
\hline 
6 &   &   &   &   &   & 6 &   &   &   &   &   & 6 &   &   &   &   &   & 6 &   &   &   &   &   & 
\hline 
7 &   &   &   &   &   & 7 &   &   &   &   &   & 7 &   &   &   &   &   & 7 &   &   &   &   &   & 
\hline 
8 &   &   &   &   &   & 8 &   &   &   &   &   & 8 &   &   &   &   &   & 8 &   &   &   &   &   & 
\hline 
9 &   &   &   &   &   & 9 &   &   &   &   &   & 9 &   &   &   &   &   & 9 &   &   &   &   &   & 
\hline 
10 &   &   &   &   &   & 10 &   &   &   &   &   & 10 &   &   &   &   &   & 10 &   &   &   &   &     & 
\hline 
11 &   &   &   &   &   & 11 &   &   &   &   &   & 11 &   &   &   &   &   & 11 &   &   &   &   &   & 
\hline 
12 &   &   &   &   &   & 12 &   &   &   &   &   & 12 &   &   &   &   &   & 12 &   &   &   &   &   & 
\hline 
13 &   &   &   &   &   & 13 &   &   &   &   &   & 13 &   &   &   &   &   & 13 &   &   &   &   &   & 
\hline 
14 &   &   &   &   &   & 14 &   &   &   &   &   & 14 &   &   &   &   &   & 14 &   &   &   &   &   & 
\hline 
15 &   &   &   &   &   & 15 &   &   &   &   &   & 15 &   &   &   &   &   & 15 &   &   &   &   &   & 
\hline 
\end{tabular}}
OC2PS
  • 147
  • 1
    Please make your code compilable by adding the documentclass as well as the relevant packages. – leandriis Jun 12 '21 at 13:33
  • 1
    Every row in a tabular has to end with a \\. In your code all occurences of \\ right before a \hline are missing. Thus your code shouldresult in multiple error messages upon trying to compile it. – leandriis Jun 12 '21 at 13:41

2 Answers2

4

Here is a corrected/simplified version of your original table as well as an adapted version that takes up the same width, but only contains 3 instead of 4 sections:

enter image description here

What I changed in comparison to the original code:

  • Replaced the last & in every row with \\ in order to get a compilable code.
  • Removed some of the | in the column declarations of the \multicolumn commands in order to avoid too wide vertical lines in the reader rows
  • Added \rowcolor{white} for the reader rows.
  • Used nested tabulars to avoid repeating the \multicolumn command multiple times.
  • Replacedtabular with tabularx in order to have acess to the flexible width X type column which can be used instead of the manually calculated column widths that were used in the original code.
  • Used *{4}{rXXXXX} instead of rXXXXXrXXXXXrXXXXXrXXXXX in order to avoid repetition

What I changed in order to switch from a 4-section table to a 3-section table:

  • Delete the last occurence of & <number> & & & & & in each row
  • Switch from *{4}{rXXXXX} to *{3}{rXXXXX}

Here is the resulting complete and compilable MWE:

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize\bfseries}

\begin{document}

\noindent {\rowcolors{5}{black!15}{black!5} \def\arraystretch{1.8}% \begin{tabularx}{\linewidth}{|*{4}{r|X|X|X|X|X|}} \hline \rowcolor{white} \multicolumn{6}{|c|}{\bfseries\begin{tabular}{@{}c@{}}SECTION I\ Name 1\ Analysis\end{tabular}} & \multicolumn{6}{c|}{\bfseries\begin{tabular}{@{}c@{}}SECTION II\ Name 2\ Analysis\end{tabular}} & \multicolumn{6}{c|}{\bfseries\begin{tabular}{@{}c@{}}SECTION III\ Name 3\ Analysis\end{tabular}}
& \multicolumn{6}{c|}{\bfseries\begin{tabular}{@{}c@{}}SECTION IV\ Name 4\ Analysis\end{tabular}} \ \hline \rowcolor{white}# & A & B & C & D & E & # & A & B & C & D & E & # & A & B & C & D & E & # & A & B & C & D & E \ \hline\hline 1 & & & & & & 1 & & & & & & 1 & & & & & & 1 & & & & & \ \hline 2 & & & & & & 2 & & & & & & 2 & & & & & & 2 & & & & & \ \hline 3 & & & & & & 3 & & & & & & 3 & & & & & & 3 & & & & & \ \hline 4 & & & & & & 4 & & & & & & 4 & & & & & & 4 & & & & & \ \hline \end{tabularx}} \bigskip

\noindent {\rowcolors{2}{black!15}{black!5} \def\arraystretch{1.8}% \begin{tabularx}{\linewidth}{|*{3}{r|X|X|X|X|X|}} \hline \rowcolor{white} \multicolumn{6}{|c|}{\bfseries\begin{tabular}{@{}c@{}}SECTION I\ Name 1\ Analysis\end{tabular}} & \multicolumn{6}{c|}{\bfseries\begin{tabular}{@{}c@{}}SECTION II\ Name 2\ Analysis\end{tabular}} & \multicolumn{6}{c|}{\bfseries\begin{tabular}{@{}c@{}}SECTION III\ Name 3\ Analysis\end{tabular}} \ \hline \rowcolor{white}# & A & B & C & D & E & # & A & B & C & D & E & # & A & B & C & D & E \ \hline\hline 1 & & & & & & 1 & & & & & & 1 & & & & & \ \hline 2 & & & & & & 2 & & & & & & 2 & & & & & \ \hline 3 & & & & & & 3 & & & & & & 3 & & & & & \ \hline 4 & & & & & & 4 & & & & & & 4 & & & & & \ \hline \end{tabularx}}

\end{document}

leandriis
  • 62,593
  • This is incredible, thanks! I am struggling with another little issue now: For section 1, I need to provide 6 options. That sounded starightforward to me - in your second example, I changed section 1's \multicolumn{6} to \multicolumn{7} and added & F after & E and similarly & to the other rows. But what ends up happening is that the "extra" column ends up being pushed into the next row for each row.. – OC2PS Jun 12 '21 at 15:38
  • ""extra" column ends up being pushed into the next row " sounds extremely like an "output" that you would get from a code that acutally did not compile properly. Once you get an error message, do not ignore it and do not look at the output, but try to fix the error message first. Even if you get something that on first glance resembles a pdf file, there can still be issues with it. After an error, TeX only tries to recover enough to syntax check more of the file, it does not try to make sensible output after an error. – leandriis Jun 12 '21 at 15:51
  • 1
    Did you also inform latex that you added an extra column by adding an additional column specifier? The \begin{tabularx} line should now most likely look like this: \begin{tabularx}{\linewidth}{| r|X|X|X|X|X|X| *{2}{r|X|X|X|X|X|}}. – leandriis Jun 12 '21 at 15:52
  • Thank you! \begin{tabularx}{\linewidth}{| r|X|X|X|X|X|X| *{2}{r|X|X|X|X|X|}} fixed it, but I don't understand this line. What am I saying here? – OC2PS Jun 12 '21 at 16:01
  • 1
    r|X|X|X|X|X|X| stands for one r type column followed by 6 X type columns. This is what you need for the first section's block. For the other two blocks, you only need 5 X type columns. In order to avoid repeating r|X|X|X|X|X| twice, I used *{2}{r|X|X|X|X|X|} instead. I hope this helps. – leandriis Jun 12 '21 at 16:03
1

Normally, your way should have worked. I think the problem is that this code could not work from begin on, because there was one & too much and most of the \\ were missing.

Arraystretch just stretches the array;) If you comment this line out, you will see that the rows will be much denser. The 'trick' is the column definition in this tabular: previously there were 24 columns which should be expanded to \linewidth. That's why every column was supposed to have a width of 1/24=0.0416667 (from this value you have to subtract all automatically inserted spaces, this is the width of the vertical lines (\linewidth) and two times the space between column text and vertical line (\tabcolsep). The only thing to change is now to use a width of \linewidth/16 = 0.055555\linewidth and to remove the last 6 columns. enter image description here

\def\arraystretch{1.8}% 
\begin{tabular}{
      |p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|p{\dimexpr 0.0555555\linewidth-2\tabcolsep}|  }
   \hline
   \multicolumn{6}{|c|}{\textbf{SECTION I}} & \multicolumn{6}{|c|}{\textbf{SECTION II}} & \multicolumn{6}{|c|}{\textbf{SECTION III}} \\
   \multicolumn{6}{|c|}{\textbf{Name 1}}    & \multicolumn{6}{|c|}{\textbf{Name 2}}     & \multicolumn{6}{|c|}{\textbf{Name 3}}      \\
   \multicolumn{6}{|c|}{\textbf{Analysis}}  & \multicolumn{6}{|c|}{\textbf{Analysis}}   & \multicolumn{6}{|c|}{\textbf{Analysis}}    \\
   \hline
   \# & A & B & C & D & E & \# & A & B & C & D & E & \# & A & B & C & D & E \\ \hline\hline
   1  &   &   &   &   &   & 1  &   &   &   &   &   & 1  &   &   &   &   &   \\ \hline
   2  &   &   &   &   &   & 2  &   &   &   &   &   & 2  &   &   &   &   &   \\ \hline
   3  &   &   &   &   &   & 3  &   &   &   &   &   & 3  &   &   &   &   &   \\ \hline
   4  &   &   &   &   &   & 4  &   &   &   &   &   & 4  &   &   &   &   &   \\ \hline
   5  &   &   &   &   &   & 5  &   &   &   &   &   & 5  &   &   &   &   &   \\ \hline
   6  &   &   &   &   &   & 6  &   &   &   &   &   & 6  &   &   &   &   &   \\ \hline
   7  &   &   &   &   &   & 7  &   &   &   &   &   & 7  &   &   &   &   &   \\ \hline
   8  &   &   &   &   &   & 8  &   &   &   &   &   & 8  &   &   &   &   &   \\ \hline
   9  &   &   &   &   &   & 9  &   &   &   &   &   & 9  &   &   &   &   &   \\ \hline
   10 &   &   &   &   &   & 10 &   &   &   &   &   & 10 &   &   &   &   &   \\ \hline
   11 &   &   &   &   &   & 11 &   &   &   &   &   & 11 &   &   &   &   &   \\ \hline
   12 &   &   &   &   &   & 12 &   &   &   &   &   & 12 &   &   &   &   &   \\ \hline
   13 &   &   &   &   &   & 13 &   &   &   &   &   & 13 &   &   &   &   &   \\ \hline
   14 &   &   &   &   &   & 14 &   &   &   &   &   & 14 &   &   &   &   &   \\ \hline
   15 &   &   &   &   &   & 15 &   &   &   &   &   & 15 &   &   &   &   &   \\ \hline
\end{tabular}

Some additional stuff:

  • I left out the \rowcolors.
  • The table does not perfectly fit onto one page: The reason for that is, that e.g. one of the vertical lines (in a tabular like this with n columns there are n+1 vertical lines) was not taken into account. If you want to reduce "overfull \hbox"-warnings, maybe just use three or two digits of 1/16.
  • I'd prefer the first column per section (the ones with #) to be right aligned. p-columns are always justified. See e.g. https://tex.stackexchange.com/a/12712/237192 achieving right aligned columns.
Οὖτις
  • 2,897
  • 1
  • 5
  • 18