2

I'm trying to make a table like this one: enter image description here

This is my current code for it:

\documentclass[a4paper, 12pt]{article}

%Tables
\usepackage{multirow}
\usepackage{array}
\usepackage{tabu}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\titleformat{\section}
{\normalfont\large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}
{\normalfont\large\bfseries}{\thesubsection}{1em}{}

\begin{document}

\hline
\multirow{3}{4em}{\center{Time elapsed (s) $\pm$1s}} & \multicolumn{9}{|c|}{Volume of gas produced (cm$^3$) $\pm$1cm$^3$}\\\cline{2-10}
& \multicolumn{3}{|P{9em}|}{0.42mol/dm$^3$ C$_2$H$_4$O$_2$ and 0.4mol/dm$^3$ of NaHCO3} & \multicolumn{3}{|P{9em}|}{0.42mol/dm$^3$ C$_2$H$_4$O$_2$ and 0.4mol/dm$^3$ of NaHCO3} & \multicolumn{3}{|P{9em}|}{0.84mol/dm$^3$ C$_2$H$_4$O$_2$ and 0.8mol/dm$^3$ of NaHCO$_3$}\\\cline{2-10}
& Trial 1 & Trial 2 & Trial 3 & Trial 1 & Trial 2 & Trial 3 & Trial 1 & Trial 2 & Trial 3\\
\hline
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
\hline
15 & 20 & 22 & 18 & 25 & 15 & 17 & 5 & 5 & 6\\
\hline
30& 27& 29& 25& 31& 25& 22& 8&  8&  8\\
\hline
45& 32& 33& 29& 35& 32& 28& 10& 11& 11\\
\hline
60& 35& 36& 33& 38& 38& 34& 12& 13  12\\
\hline

\end{tabular}
\end{center}

\end{document}

Most of it is ok however the centering in the multicolumns is off and I'm not sure how to fix it, same goes for the multirow on the top left.

2
  • Please make your code compilable. Currently the beginning of the table is missing.
    – leandriis
    Jul 7, 2020 at 9:03
  • Unrelated to the issue, but you might want to consider using a package such as chemformula or mhchem or oder to typeset the chemical formulae instead of using math mode for this task. siunitx for the units might also come in handy.
    – leandriis
    Jul 7, 2020 at 9:04

3 Answers 3

6

Your document example has some issues as:

  • it is not complete (missing `\begin{tabular}
  • the table is huge and it can not be fit in text width (in MWE below is increased text width and reduced font size)
  • for chemistry formulae is sensible to use the mhchem package
  • for units and columns with numbers is handy to use the siunitx package
\documentclass[12pt]{article}
\usepackage[a4paper, margin=25mm]{geometry}
\usepackage{makecell, multirow, tabularx}
\usepackage[version=4]{mhchem}
\usepackage{siunitx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c|}{#2}}
\NewExpandableDocumentCommand\mcx{O{1}m}
    {\multicolumn{#1}{>{\centering\arraybackslash}X|}{#2}}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%


\begin{document}
    \begin{table}[htb]
\caption{My wide table}
\label{tab:wide}
    \footnotesize
\setlength\tabcolsep{1pt}
\setcellgapes{3pt}
\makegapedcells
    \begin{tabularx}{\linewidth}{|*{10}{S[table-format=2.0]|}}
    \hline
\multicolumn{1}{|X|}{{\multirow{5}{=}{\centering Time elapsed\\ \si{s \pm 1 s}}}}
    & \mcc[9]{Volume of gas produced (\si{cm^3 \pm 1cm^3})}  \\
    \cline{2-10}
    & \mcc[3]{\makecell{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}}
        & \mcc[3]{\makecell{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}}
            & \mcc[3]{\makecell{\SI{0.84}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.8}{mol/dm^3} of \ce{NaHCO3}}}\\
    \cline{2-10}
    & \mcx{Trial 1} 
        & \mcx{Trial 2}
            & \mcx{Trial 3}
                    & \mcx{Trial 1}
                        & \mcx{Trial 2}
                            & \mcx{Trial 3}
                                    & \mcx{Trial 1}
                                        & \mcx{Trial 2}
                                            & \mcx{Trial 3}     \\
        \hline
 0  & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0\\
    \hline
15  & 20    & 22    & 18    & 25    & 15    & 17    & 5     & 5     & 6\\ 
    \hline
30  & 27    & 29    & 25    & 31    & 25    & 22    & 8     & 8     & 8\\
    \hline
45  & 32    & 33    & 29    & 35    & 32    & 28    & 10    & 11    & 11\\
    \hline
60  & 35    & 36    & 33    & 38    & 38    & 34    & 12    & 13    & 12\\
    \hline
    \end{tabularx}
    \end{table}
\end{document}

enter image description here

(red lines show part of page layout)

4

Here is a variation on Zarko's answer. Instead of {tabularx}, you can use {NiceTabular} of nicematrix.

It's easier for the rules since you only have to specify the key hvlines to have all the expected rules.

However, there is no column type X in {NiceTabular} and that's why I have computed the width of the columns in \colwith in order to have the tabular exactly fitting in \textwidth.

\documentclass[12pt]{article}
\usepackage[a4paper, margin=25mm]{geometry}
\usepackage{nicematrix}
\usepackage[version=4]{mhchem}
\usepackage{siunitx}


\begin{document}

\NiceMatrixOptions{cell-space-limits=3pt}

\begin{table}[htb]
\caption{My wide table}

\footnotesize

\setlength\tabcolsep{0pt}

% We compute the width of the columns to fit in \textwidth
\newlength{\colwidth}
\setlength{\colwidth}{(\textwidth-11\arrayrulewidth)/10}

\begin{NiceTabular}{*{10}{S[table-format=2.0]}}[hvlines,columns-width = \colwidth]
\Block{3-1}{Time\\ elapsed\\ \unit{s} $\pm$ \qty{1}{s}}
    & \Block{1-9}{Volume of gas produced (\unit{cm^3} $\pm$ \qty{1}{cm^3})}  \\
        & \Block{1-3}{\qty{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \qty{0.4}{mol/dm^3} of \ce{NaHCO3}} &&
            & \Block{1-3}{\qty{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \qty{0.4}{mol/dm^3} of \ce{NaHCO3}} &&
                 & \Block{1-3}{\qty{0.84}{mol/dm^3} \ce{C2H4O2} and\\ \qty{0.8}{mol/dm^3} of \ce{NaHCO3}} \\
\RowStyle{\bfseries}
    & {Trial 1} 
        & {Trial 2}
            & {Trial 3}
                 & {Trial 1}
                      & {Trial 2}
                           & {Trial 3}
                                & {Trial 1}
                                     & {Trial 2}
                                          & {Trial 3}     \\
 0  & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0\\
15  & 20    & 22    & 18    & 25    & 15    & 17    & 5     & 5     & 6\\ 
30  & 27    & 29    & 25    & 31    & 25    & 22    & 8     & 8     & 8\\
45  & 32    & 33    & 29    & 35    & 32    & 28    & 10    & 11    & 11\\
60  & 35    & 36    & 33    & 38    & 38    & 34    & 12    & 13    & 12\\
\end{NiceTabular}
\end{table}
\end{document}

Output of the above code

1

If you insist using vertical and horizontal rules, vertically and horizontal spanned cells and similar, you should take a look at the package cals. Very easy to set up.

In addition, I have used a KOMA-script class, because it has sensible margins and internal commands to change captions.

The way I have set up the calstable, it may span multiple pages. It is not floating, and I use

\captionaboveof{table}{My wide table\label{tab:wide}} 

to have a correctly formatted caption. The \label-command goes inside the caption command, so it is not lost.

The best is of cause to get rid of most of the rules, an using booktabs and additional spacing to group rows and columns to make the tabular more readable.

enter image description here

\documentclass[british, DIV=12, captions=tableheading]{scrartcl}
\usepackage{cals, url}
\usepackage{babel}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[T1]{fontenc}

\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\addtokomafont{caption}{\sffamily\bfseries\small}
\setkomafont{captionlabel}{\normalfont}

\begin{document}

\bigskip

\begin{calstable}[c]

\captionaboveof{table}{My wide table\label{tab:wide}}


% Defining columns relative to each other and relative to the margins
\colwidths{{\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
    }
% The tabular fills the text area if sum of all columns is 10

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.8pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.6pt}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.6pt}
\def\cals@bgcolor{}

\setlength{\cals@paddingL}{3.2pt}
\setlength{\cals@paddingR}{3.2pt}


% R1H1
\thead{\sffamily\small%           The first 3 rows are heading and vill be repeated on every page
\brow
    \nc{lrt}
    \nc{ltb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{rtb}\alignC\sc{Volume of gas produced (\si{cm^3 \pm 1cm^3})}
\erow
%R2H2
\brow
    \nc{lr}
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\SI{0.84}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.8}{mol/dm^3} of \ce{NaHCO3}}
\erow
%R3H3
\brow
    \nc{lrb}\alignC\sc{\vfil Time elapsed \si{s \pm 1 s}}
    \bfseries\cell{Trial 1}
    \cell{Trial 2}
    \cell{Trial 3}
    \cell{Trial 1}
    \cell{Trial 2}
    \cell{Trial 3}
    \cell{Trial 1}
    \cell{Trial 2}
    \cell{Trial 3}\normalfont
\erow
}
\tfoot{\lastrule\strut}
%R4B1
\brow
    \cell{15}
    \cell{20}
    \cell{22}
    \cell{18}
    \cell{25}
    \cell{15}
    \cell{17}
    \cell{5}
    \cell{5}
    \cell{6}
\erow
%R5B2
\brow
    \cell{30}
    \cell{27}
    \cell{29}
    \cell{25}
    \cell{31}
    \cell{25}
    \cell{22}
    \cell{8}
    \cell{8}
    \cell{8}
\erow

%R6B3
\brow
    \cell{45}
    \cell{32}
    \cell{33}
    \cell{29}
    \cell{35}
    \cell{32}
    \cell{28}
    \cell{10}
    \cell{11}
    \cell{11}
\erow
%R7B4
\brow
    \cell{60}
    \cell{35}
    \cell{36}
    \cell{33}
    \cell{38}
    \cell{38}
    \cell{34}
    \cell{12}
    \cell{13}
    \cell{12}
\erow
%R8B5
\brow
    \cell{75}
    \cell{38}
    \cell{38}
    \cell{36}
    \cell{41}
    \cell{42}
    \cell{38}
    \cell{14}
    \cell{15}
    \cell{14}
\erow
%R9B6
\brow
    \cell{90}
    \cell{41}
    \cell{42}
    \cell{38}
    \cell{43}
    \cell{46}
    \cell{42}
    \cell{15}
    \cell{17}
    \cell{16}
\erow
%R10B7
\brow
    \cell{105}
    \cell{42}
    \cell{44}
    \cell{41}
    \cell{44}
    \cell{50}
    \cell{46}
    \cell{16}
    \cell{19}
    \cell{17}
\erow
%R11B8
\brow
    \cell{120}
    \cell{44}
    \cell{45}
    \cell{43}
    \cell{46}
    \cell{53}
    \cell{48}
    \cell{18}
    \cell{21}
    \cell{18}
\erow
%R12B9
\brow
    \cell{135}
    \cell{46}
    \cell{47}
    \cell{44}
    \cell{47}
    \cell{55}
    \cell{51}
    \cell{18}
    \cell{22}
    \cell{20}
\erow
%R13B10
\brow
    \cell{150}
    \cell{48}
    \cell{49}
    \cell{46}
    \cell{49}
    \cell{58}
    \cell{54}
    \cell{19}
    \cell{24}
    \cell{22}
\erow
%R14B11
\brow
    \cell{165}
    \cell{50}
    \cell{51}
    \cell{47}
    \cell{50}
    \cell{60}
    \cell{56}
    \cell{20}
    \cell{25}
    \cell{23}
\erow
%R15B12
\brow
    \cell{180}
    \cell{51}
    \cell{52}
    \cell{49}
    \cell{51}
    \cell{61}
    \cell{58}
    \cell{21}
    \cell{26}
    \cell{25}
\erow

\makeatletter
\end{calstable}

\end{document}

You must log in to answer this question.

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