2

I am trying to replicate the hand drawn table in latex.

desired output

So far what I have is the following code

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\renewcommand{\thesection}{\arabic{section}}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{fancyhdr}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{color,xcolor}
\usepackage{booktabs,array}
\usepackage{hyperref}
\usepackage{graphicx}
\usetikzlibrary{arrows}
\usepackage{polynom}
\usepackage{flexisym}
\usepackage{wallpaper}
\usepackage{blkarray}
\usepackage{caption}
\usepackage{adjustbox}
\usepackage{multirow}
\usepgfplotslibrary{fillbetween}
\newenvironment{tightcenter}{
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}}{\end{center}}
\begin{document}

\begin{center}{
\setlength\arraycolsep{6pt} % default is "5pt"
$\begin{array}{lccc}
\midrule
\multirow{2}{*}{SECTION A} & \multicolumn{1}{l}{\begin{array}{@{}c@{}}\text{Number of}\\\text{questions}\end{array}} & \multicolumn{1}{l}{\begin{array}{@{}c@{}}\text{Number of questions}\\\text{to be answered}\end{array}} & \multicolumn{1}{l}{\begin{array}{@{}c@{}}\text{Number of}\\\text{marks}\end{array}}\\[0.224cm]
\hline
 & \multicolumn{1}{c}{7} & \multicolumn{1}{c}{7} & \multicolumn{1}{c}{36} \\[0.224cm]
\hline
\text{SECTION B} & \begin{array}{@{}c@{}}\text{Number of}\\\text{questions}\end{array} & \begin{array}{@{}c@{}}\text{Number of questions}\\\text{to be answered}\end{array} & \begin{array}{@{}c@{}}\text{Number of}\\\text{marks}\end{array}\\[0.124cm]
\hline
& 4 & 2 & 24\\ 
\hline
&  &  & \text{Total }60\\
\hline
\end{array}$}
\end{center}
\end{document}

It seems to generate somewhat a similar table (as shown below), however the multi-row in the first column for Section A and Section B is not happening.

What I am getting

How do I achieve this? I also what to horizontally and vertically centre the texts in the table.

Thanks!

2

try

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{caption}
\usepackage{booktabs, makecell, multirow}

\begin{document}
    \begin{center}
\begin{tabular}{lccc}
    \toprule
\multirow{3}{*}{SECTION A}
    & \makecell{Number of\\ questions}
        & \makecell{Number of questions\\ to be answered}
            & \makecell{Number of\\ marks}      \\
    \cmidrule{2-4}
    & 7 & 7 & 36                                \\
    \midrule
\multirow{3}{*}{SECTION B}
    & \makecell{Number of\\ questions}
        & \makecell{Number of questions\\ to be answered}
            & \makecell{Number of\\ marks}      \\
    \cmidrule{2-4}
    & 4 & 2 & 24                                \\
    \midrule
    &   &   & Total 60                          \\
    \bottomrule
\end{tabular}
    \end{center}
\end{document}

enter image description here

is this what you looking for?

as you can see, in above mwe (minimal working example) i remove from your code all what is not related to table. also i remove all not needed code complications as use \multicolumn for one column cells. observe, that instead hline are used rules from booktabls: \toprule, \midrule and \bottomrule. for two line cells' content is used \makecell from the \makecell package.

3

An alternative using the very powerful and versatile tabu package:
Apparently the tabu package is considered obsolete (thanks @Zarko for the heads-up), but the same effect can be achieved with the tabulary package.

enter image description here

\documentclass{article}

\usepackage{tabulary}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}
    \begin{tabulary}{0.8\linewidth}{l*{3}{C}}
        \toprule
        \multirow{2}{*}{SECTION A} & Number of questions & Number of questions to be answered & Number of marks \\
        \cmidrule{2-4}
        & 7 & 7 & 36 \\\midrule
        \multirow{2}{*}{SECTION B} & Number of questions & Number of questions to be answered & Number of marks \\\cmidrule{2-4}
        & 4 & 2 & 24 \\\midrule
        & & & Total 60\\ \bottomrule
    \end{tabulary}
\end{document}

But I believe you don't really need multirows, if you use the table like this:

enter image description here

With

    \begin{tabulary}{0.8\linewidth}{l*{3}{C}}
        \toprule
        & Number of questions & Number of questions to be answered & Number of marks \\
        \midrule
        Section A & 7 & 7 & 36 \\
        Section B & 4 & 2 & 24 \\
        \midrule
        Total & & & 60\\
        \bottomrule
    \end{tabulary}
  • 1
    your second table is nice, however, i would not use tabu. it is not maintained and buggy. – Zarko Aug 5 '18 at 10:56
  • @Zarko I was unaware of that, I've been using it for quite some time and have never encountered bugs I believe. Maybe I haven't stretched its abilities with my use-cases :) I will adjust my answer. What package would you recommend as alternative? – Max Aug 5 '18 at 10:58
  • 2
    mostly it works. but now, after new version of the array package, it become incompatible to it (when you use m columns) and you will have problems with before normally working tables. – Zarko Aug 5 '18 at 11:03
2

Here is a solution, with a simpler code, based on makecell. I don't think you need an array environment for this table, tabular is just fine here, and using systematically the rules form booktabs will save you typing additional spacings like[0.224mm] since these rules incorporate some vertical padding.

Also, don't load a package several times. Note that xcolor already loads color, mathtools loads amsmath, blkarray loads array. Last, not least, hyperref should be loaded as the last package, with very few exceptions.

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\renewcommand{\thesection}{\arabic{section}}
\usepackage{mathtools, amssymb}
\usepackage{cancel}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\usetikzlibrary{arrows}
\usepackage{fancyhdr}
\usepackage{bigints}
\usepackage{tcolorbox}
\usepackage{xcolor}
\usepackage{booktabs, caption}
\usepackage{blkarray, multirow, makecell}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{polynom}
\usepackage{flexisym}
\usepackage{wallpaper}
\usepackage{hyperref}
\newenvironment{tightcenter}{
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}}{\end{center}}

\begin{document}

\begin{center}
\begin{tabular}{lccc}
\toprule
\multirowcell{3.3}{SECTION A} & \makecell{Number of \\ questions} & \makecell{Number of questions \\ to be answered} & \makecell{Number of \\ marks}\\
\cmidrule{2-4}
 & \multicolumn{1}{c}{7} & \multicolumn{1}{c}{7} & \multicolumn{1}{c}{36} \\
\midrule
 \multirowcell{3.4}{SECTION B} & \makecell{Number of \\ questions} & \makecell{Number of questions \\ to be answered} & \makecell{Number of \\ marks}\\
\cmidrule{2-4}
& 4 & 2 & 24\\
\midrule
& & & \text{Total }60\\
\bottomrule
\end{tabular}
\end{center}

\end{document} 

enter image description here

  • With overleaf.com, there are .3 and .4 printed above Section A and Section B, it looks like misunderstanding \multirowcell{3.3}. How to fix these errors? – pzorba75 Aug 5 '18 at 15:18
  • 1
    @pzorba75: Does overleaf use a (comparatively) recent version of multirow. Last version is 2.2, dating back to 25/11/2016. A work-around would consist in using the optional argument of \multirow something like \multirowcell[3}[-2pt]{SECTION A} yields the same result as \multirowcell{3.3}{SECTION A}, as far as I ca see. – Bernard Aug 5 '18 at 15:31

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.