3

I create the following table: enter image description here

I'm using siunitx table-format=3.2 because I have numbers which are 2dp.

The combination of numbers of different magnitudes is creating a really ugly effect.

I would like some way to align Rows 1-2 together, then 'reset' the table and align Rows 3-5 together, and then reset the table again and align rows 6-9 together.

How can I achieve this?


Code for the above:

 % these define the type of document and the look of the page
\documentclass[]{article}
\usepackage[margin=0.8in, top = 1.3in, headheight = 0.6in]{geometry}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\setlength{\parindent}{0pt} % space at start of paragraph
\setlength{\parskip}{0.14in} % space between paragraphs

% colors
\usepackage[table]{xcolor}
\definecolor{bg3}{HTML}{ECECEC} % t gray = gray!15!white

% table-related
\usepackage{array}
\usepackage{booktabs}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\usepackage{siunitx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

% math
\usepackage{amsmath}

\begin{document}

\pagecolor{bg3}
\newcommand{\colcel}{\cellcolor{white}}
\def\arraystretch{1.5}

\sisetup{table-format=3.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\multicolumn{6}{l}{Title} \\
\Xhline{1.2pt}
& \multicolumn{4}{c|}{2019} & { 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & { 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 &  11.1 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 &  1.3 \\
\hhline{~-----} & \multicolumn{5}{c}{Heading 1} \\ \hhline{~-----}
Row 3 & 12438 & 12009 & 12701 & 13159 &  19696 \\
Row 4 & 6601 & 6206 & 6745 & 7256 &  6665 \\
Row 5 & 164 & 322 & 820 & 0 &  2174 \\
\hhline{~-----} & \multicolumn{5}{c}{Heading 2} \\ \hhline{~-----}
Row 6 & -0.8 & -0.3 & 0.2 & 0.3 &  -0.4 \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 &  -0.04 \\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 &  1 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 &  -30.2 \\ \Xhline{1.2pt}
\end{tabular}

\end{document}

1 Answer 1

2

Split the table into three tabular environments and use a different table-format for each of the three tabulars:

Side note: Please be aware that the resulting table will be 2pt wider than the textwidth. You might want to correct that.

enter image description here

\documentclass[]{article}
\usepackage[margin=0.8in, top = 1.3in, headheight = 0.6in]{geometry}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\setlength{\parindent}{0pt} % space at start of paragraph
\setlength{\parskip}{0.14in} % space between paragraphs

% colors
\usepackage[table]{xcolor}
\definecolor{bg3}{HTML}{ECECEC} % t gray = gray!15!white

% table-related
\usepackage{array}
\usepackage{booktabs}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\usepackage{siunitx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

% math
\usepackage{amsmath}

\begin{document}

\pagecolor{bg3}
\newcommand{\colcel}{\cellcolor{white}}
\def\arraystretch{1.5}

\sisetup{table-format=2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\multicolumn{6}{l}{Title} \\
\Xhline{1.2pt}
& \multicolumn{4}{c|}{2019} & { 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & { 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 &  11.1 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 &  1.3 \\
\end{tabular}
\sisetup{table-format=5, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 1} \\ \hhline{~-----}
Row 3 & 12438 & 12009 & 12701 & 13159 &  19696 \\
Row 4 & 6601 & 6206 & 6745 & 7256 &  6665 \\
Row 5 & 164 & 322 & 820 & 0 &  2174 \\
\end{tabular}
\sisetup{table-format=-2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 2} \\ \hhline{~-----}
Row 6 & -0.8 & -0.3 & 0.2 & 0.3 &  -0.4 \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 &  -0.04 \\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 &  1 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 &  -30.2 \\ \Xhline{1.2pt}
\end{tabular}

\end{document}

With tabularx and its X type column you can prevent the table from being wider than the textwidth while avoiding to manually calculate the required widths. I have defined a new Y column type with left aligned contents. With \renewcommand\tabularxcolumn[1]{m{#1}} I have made sure that X and Y column contents are vertically centered in order to match the original definition of the L type column form your example:

\documentclass[]{article}
\usepackage[margin=0.8in, top = 1.3in, headheight = 0.6in]{geometry}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\setlength{\parindent}{0pt} % space at start of paragraph
\setlength{\parskip}{0.14in} % space between paragraphs

% colors
\usepackage[table]{xcolor}
\definecolor{bg3}{HTML}{ECECEC} % t gray = gray!15!white

% table-related
\usepackage{array}
\usepackage{booktabs}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\usepackage{siunitx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{Y}{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}X}
% math
\usepackage{amsmath}

\begin{document}

\pagecolor{bg3}
\newcommand{\colcel}{\cellcolor{white}}
\def\arraystretch{1.5}

\sisetup{table-format=2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabularx}{\textwidth}{Y|S|*{3}{S|}S}
\multicolumn{6}{l}{Title} \\
\Xhline{1.2pt}
& \multicolumn{4}{c|}{2019} & { 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & { 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 &  11.1 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 &  1.3 \\
\end{tabularx}
\sisetup{table-format=5, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabularx}{\textwidth}{Y|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 1} \\ \hhline{~-----}
Row 3 & 12438 & 12009 & 12701 & 13159 &  19696 \\
Row 4 & 6601 & 6206 & 6745 & 7256 &  6665 \\
Row 5 & 164 & 322 & 820 & 0 &  2174 \\
\end{tabularx}
\sisetup{table-format=-2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabularx}{\textwidth}{Y|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 2} \\ \hhline{~-----}
Row 6 & -0.8 & -0.3 & 0.2 & 0.3 &  -0.4 \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 &  -0.04 \\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 &  1 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 &  -30.2 \\ \Xhline{1.2pt}
\end{tabularx}

\end{document}
3
  • Pardon, is the 2pt wider a symptom of this solution or of my initial table? Either way, thank you!
    – Thev
    May 11, 2020 at 18:53
  • 1
    @TheveshTheva: The table was already too wide in your original example. I will update my answer to include a tabularx based solution that can help overcome this without having to manually calculated the required width for the first column.
    – leandriis
    May 11, 2020 at 19:28
  • That's brilliant, thank you!
    – Thev
    May 11, 2020 at 19:30

You must log in to answer this question.

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