1

I create the following table:

enter image description here

with the following code:

\documentclass{standalone}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\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{color}
\usepackage{xcolor, colortbl}

\begin{document}

\def\arraystretch{1.5}
\begin{tabular}{L{5.75cm}|R{1.25cm}|R{1.25cm}|R{1.25cm}|R{1.25cm}|R{1.25cm}|R{1.25cm}|R{1.25cm}}
    \multicolumn{8}{l}{\textbf{Table 2: GDP by Economic Activity (at constant 2015 prices)}} \\ 
    \hline\hline
    & \multirowcell{3}{2019 \\ share \\ \%} &    \multicolumn{6}{c}{Annual growth, \%} \\
    \hhline{~~------} 
    & & \multicolumn{2}{c|}{2018} & \multicolumn{4}{c}{\cellcolor{blue!10!white} 2019} \\
    \hhline{~~------} 
    & &  3Q & 4Q & 1Q & 2Q & 3Q & {\cellcolor{blue!10!white} 4Q} \\ 
    \hline
    Services  & 57.7 & 7.3 &6.9 &    &    &    &   \cellcolor{blue!10!white} \\
    Manufacturing     & 22.3 &  5.0   &   4.7 &    &    &    &  \cellcolor{blue!10!white} \\
    Mining            &  7.1 &  $-$5.7   &   $-$0.7 &    &    &    &  \cellcolor{blue!10!white} \\
    Agriculture       &  7.1 &  $-$0.8   &  $-$0.1  &    &    &    &  \cellcolor{blue!10!white} \\
    Construction      &  4.7 &  4.7   &    2.6&    &    &    &  \cellcolor{blue!10!white} \\ \hline
    Real GDP$^1$      &  100.0 & 4.4    &   4.7 &    &    &    & \cellcolor{blue!10!white} \\ \hline
    \multicolumn{8}{l}{\small $^1$Shares do not add up due to rounding and exclusion of import duties.}         \\
    \multicolumn{8}{l}{\small Source: Department of Statistics Malaysia}   \\ \hline
\end{tabular}

\end{document}

What I want to change is to 'shift' the last 6 columns to the left (the columns with sub-headers 3Q, 4Q, etc), but without removing the right-align.

The reason for this is that typographically, numbers should be right-aligned for ease of reading. However, I'd like the entire block of right-aligned numbers to be centered for aesthetics.

An analog of this would be:

\begin{center}
\begin{minipage}
\flushright
Text
\end{minipage}
\end{center}

Such that the minipage itself is centred, but the text within is not.

How can I achieve this?

3
  • If I understand well, you would like to have the numbers centred, but aligned on the decimal dot?
    – Bernard
    Commented Mar 15, 2020 at 23:55
  • That's exactly right!
    – Thev
    Commented Mar 16, 2020 at 0:00
  • Have you tried the dcolumn package? This package comes with the LaTeX tools (no extra installation required) Commented Mar 16, 2020 at 0:21

1 Answer 1

1

The S column type (from siunitx) is done for you!. I added some simplifications: when you load xcolor, you don't have to load color. Similarly, loading it with option [table] loads colortbl. I took the liberty to group the notes at the bottom of the table on the same line, but you can easily revert to your original layout if you prefer.

Note that in S columns, non-numeric cells have their contents centred if you add a pair of braces around the contents.

\documentclass[border = 6pt]{standalone}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage[table]{xcolor}
\usepackage{siunitx}

\begin{document}
\sisetup{table-format=-1.1, table-number-alignment=center, table-column-width=1.25cm}
\def\arraystretch{1.5}
\begin{tabular}{L{5.75cm}|S[table-format=3.1]|*{5}{S|}S}
    \multicolumn{8}{l}{\textbf{Table 2: GDP by Economic Activity (at constant 2015 prices)}} \\
    \hline\hline
    & {\multirowcell{3}{2019 \\ share \\ \%}} & \multicolumn{6}{c}{Annual growth, \%} \\
    \hhline{~~------}
    & & \multicolumn{2}{c|}{2018} & \multicolumn{4}{c}{\cellcolor{blue!10!white} 2019} \\
    \hhline{~~------}
    & & {3Q} & {4Q} & {1Q} & {2Q} & {3Q} & {\cellcolor{blue!10!white} 4Q} \\
    \hline
    Services & 57.7 & 7.3 &6.9 & & & & \cellcolor{blue!10!white} \\
    Manufacturing & 22.3 & 5.0 & 4.7 & & & & \cellcolor{blue!10!white} \\
    Mining & 7.1 & -5.7 & -0.7 & & & & \cellcolor{blue!10!white} \\
    Agriculture & 7.1 & -0.8 & -0.1 & & & & \cellcolor{blue!10!white} \\
    Construction & 4.7 & 4.7 & 2.6& & & & \cellcolor{blue!10!white} \\ \hline
    Real GDP$^1$ & 100.0 & 4.4 & 4.7 & & & & \cellcolor{blue!10!white} \\ \hline
    \multicolumn{4}{@{}l}{\small $^1$Shares do not add up due to rounding and exclusion of import duties.}
   & \multicolumn{4}{r@{}}{\small Source: Department of Statistics Malaysia} \\ \hline
\end{tabular}

\end{document} 

enter image description here

6
  • a vertical is missing
    – js bibra
    Commented Mar 16, 2020 at 0:34
  • Thank you so much! Could I ask why the table format in the second column is 3.1, but 1.1 for the overall table?
    – Thev
    Commented Mar 16, 2020 at 0:36
  • the 3.1 accounts for the 100.1
    – js bibra
    Commented Mar 16, 2020 at 0:37
  • @jsbibra: where?
    – Bernard
    Commented Mar 16, 2020 at 0:37
  • 1
    @jsbibra: Oh! yes. I had modified the table preamble and placed erroneously the last S. Fixed. Thank you for pointing it!
    – Bernard
    Commented Mar 16, 2020 at 0:42

You must log in to answer this question.

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