3

In this table the column headers of the number columns formatted with siunitx seem to be centered. Is there a way to align them to the right, so that the column header itself is right aligned and ends in one line with the percentage sign?

Table with siunitx formatting

Edit: Extended to Minimum Working Example

The LaTeX code for the table looks like this. I compile it with XeLaTeX.

\documentclass[11pt, a4paper, oneside]{article}

% Packages
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage{fullpage}
\usepackage{fancyhdr}
\usepackage{fancybox}
\usepackage{parskip}
\usepackage{booktabs}
\usepackage{siunitx}

\usepackage[usegeometry]{typearea}% before geometry!
\usepackage[landscape]{geometry}
\geometry{
  left=0.5in, right=0.5in, top=0.6in, bottom=1in,headheight=23pt,includehead
}
\newcommand*{\useportrait}{%
  \clearpage
  \KOMAoptions{paper=portrait,DIV=current}%switch to portrait
  \newgeometry{% geometry settings for portrait
    left=0.5in, right=0.5in, top=0.6in, bottom=1in,headheight=23pt,includehead
  }%
  \fancyhfoffset{0pt}% <- recalculate head and foot width for fancyhdr
}
\newcommand*{\uselandscape}{%
  \clearpage
  \KOMAoptions{paper=landscape,DIV=current}%switch to landscape
  \newgeometry{% geometry settings for landscap
    left=0.5in, right=0.5in, top=0.6in, bottom=1in,headheight=23pt,includehead
  }%
  \fancyhfoffset{0pt}% recalculate head and foot width for fancyhdr
}

\sisetup{
    round-mode          = places,
    group-digits        =  true,
    output-decimal-marker = {.},
    group-separator = {,},
    detect-all
}

% Settings
\setmainfont{Calibri}
\geometry{paper=a4paper, left=20mm, right=20mm, top=25mm, bottom=25mm}

\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[R]{\footnotesize\thepage}

% Caption
\setlength{\abovecaptionskip}{1pt}

% Page breaks
\let\oldsection\section
\renewcommand\section{\clearpage\oldsection}

% List of figures/tables
\renewcommand{\listfigurename}{Figures}
\renewcommand{\listtablename}{Tables}

\newcommand{\specialcell}[2][c]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}

\begin{document}

\uselandscape
\section{Global Equities}
\begin{tabular}{p{7cm}S[table-format=2.1, round-precision=1, table-column-width=2cm]S[table-format=2.1, round-precision=1, table-column-width=2cm]S[table-format=2.1, round-precision=1, table-column-width=2cm]S[table-format=2.1, round-precision=1, table-column-width=2cm]S[table-format=2.1, round-precision=1, table-column-width=2cm]S[table-format=2.1, round-precision=1, table-column-width=2cm]S[table-format=2.1, round-precision=1, table-column-width=2cm]S[table-format=2.1, round-precision=1, table-column-width=2cm]}
\midrule
{} & {\specialcell{Stock Market \\ Crash \\ 1973-1974}} & {\specialcell{Bear Market \\ 1980-1982}} & {\specialcell{Stock Market \\ Crash \\ 1987}} & {\specialcell{Gulf War \\ 1990}} & {\specialcell{Dot-Com Bubble \\ 2000}} & {\specialcell{Financial \\ Crisis \\ 2008}} & {\specialcell{Equity Markets \\ Rebound \\ 2009}} & {\specialcell{Oil Prices \\ Drop \\ 2010}} \\
\toprule
Nexi SpA & -72.5\si{\percent} & -37.2\si{\percent} & -34.7\si{\percent} & -30.1\si{\percent} & -58.0\si{\percent} & -76.1\si{\percent} & 65.0\si{\percent} & -10.6\si{\percent} \\
Fiserv Inc & -49.9\si{\percent} & 12.8\si{\percent} & -44.1\si{\percent} & -27.5\si{\percent} & -2.6\si{\percent} & -41.5\si{\percent} & 32.6\si{\percent} & -8.2\si{\percent} \\
\bottomrule
\end{tabular}

\end{document}
4
  • 3
    How or where is \specialcell defined?
    – Mico
    Commented Aug 13 at 9:35
  • 1
    Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – cabohah
    Commented Aug 13 at 9:39
  • Which documentclass you use? BTW, our table is 23 cm wide, i.e. more than A4 paper width ... Please extend your code fragment to complete small document.
    – Zarko
    Commented Aug 13 at 9:42
  • @cabohah Thank you for pointing this out, I extended it to a minimum example. Commented Aug 13 at 11:13

2 Answers 2

5

Don't specify column widths.

On the other hand, repeating % all over again is boring for the reader.

\documentclass[draft]{article}
\usepackage{geometry}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}[htp]
\centering
\footnotesize

\newcommand{\specialcell}[1]{\begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}
\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  *{8}{S[table-format=-2.1\%, round-precision=1]}
  @{}
}
\toprule
  & {\specialcell{Stock Market \\ Crash \\ 1973--1974}}
  & {\specialcell{Bear Market \\ 1980--1982}} 
  & {\specialcell{Stock Market \\ Crash \\ 1987}}
  & {\specialcell{Gulf War \\ 1990}} 
  & {\specialcell{Dot-Com \\ Bubble \\ 2000}}
  & {\specialcell{Financial \\ Crisis \\ 2008}} 
  & {\specialcell{Equity Markets \\ Rebound \\ 2009}}
  & {\specialcell{Oil Prices \\ Drop \\ 2010}} \\
\midrule
Nexi SpA   & -72.5\% & -37.2\% & -34.7\% & -30.1\% & -58.0\% & -76.1\% & 65.0\% & -10.6\% \\
Fiserv Inc & -49.9\% &  12.8\% & -44.1\% & -27.5\% &  -2.6\% & -41.5\% & 32.6\% &  -8.2\% \\
\bottomrule
\end{tabular*}

\caption{Some caption text}

\end{table}

\begin{table}[htp]
\centering
\footnotesize

\newcommand{\specialcell}[1]{\begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}
\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  *{8}{S[table-format=-2.1, round-precision=1]}
  @{}
}
\toprule
  & {\specialcell{Stock Market \\ Crash \\ 1973--1974}}
  & {\specialcell{Bear Market \\ 1980--1982}} 
  & {\specialcell{Stock Market \\ Crash \\ 1987}}
  & {\specialcell{Gulf War \\ 1990}} 
  & {\specialcell{Dot-Com \\ Bubble \\ 2000}}
  & {\specialcell{Financial \\ Crisis \\ 2008}} 
  & {\specialcell{Equity Markets \\ Rebound \\ 2009}}
  & {\specialcell{Oil Prices \\ Drop \\ 2010}} \\
\midrule
Nexi SpA   & -72.5 & -37.2 & -34.7 & -30.1 & -58.0 & -76.1 & 65.0 & -10.6 \\
Fiserv Inc & -49.9 &  12.8 & -44.1 & -27.5 &  -2.6 & -41.5 & 32.6 &  -8.2 \\
    \bottomrule
\end{tabular*}

\caption{Some caption text, data in percentage}

\end{table}

\end{document}

output

If you really want right alignment (but I can't see a reason to),

\documentclass[draft]{article}
\usepackage{geometry}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}[htp]
\centering
\footnotesize

\newcommand{\specialcell}[1]{\begin{tabular}[t]{@{}r@{}} #1 \end{tabular}}
\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  *{8}{S[table-format=-2.1\%, round-precision=1,table-number-alignment=right]}
  @{}
}
\toprule
  & {\specialcell{Stock Market \\ Crash \\ 1973--1974}}
  & {\specialcell{Bear Market \\ 1980--1982}} 
  & {\specialcell{Stock Market \\ Crash \\ 1987}}
  & {\specialcell{Gulf War \\ 1990}} 
  & {\specialcell{Dot-Com \\ Bubble \\ 2000}}
  & {\specialcell{Financial \\ Crisis \\ 2008}} 
  & {\specialcell{Equity Markets \\ Rebound \\ 2009}}
  & {\specialcell{Oil Prices \\ Drop \\ 2010}} \\
\midrule
Nexi SpA   & -72.5\% & -37.2\% & -34.7\% & -30.1\% & -58.0\% & -76.1\% & 65.0\% & -10.6\% \\
Fiserv Inc & -49.9\% &  12.8\% & -44.1\% & -27.5\% &  -2.6\% & -41.5\% & 32.6\% &  -8.2\% \\
\bottomrule
\end{tabular*}

\caption{Some caption text}

\end{table}

\end{document}

right alignment

6
  • +1. You may also want to reverse the OP's sequence of \midrule and \toprule directives.
    – Mico
    Commented Aug 13 at 9:56
  • @Mico Didn't notice. I'll fix
    – egreg
    Commented Aug 13 at 10:35
  • Thank you @egreg, is it possible to have the column header text itself right aligned, so that the right aligned text and the percentage sign are aligned along the same right-hand line? Commented Aug 13 at 11:15
  • @Hans-PeterSchrei Why would you want that?
    – egreg
    Commented Aug 13 at 12:40
  • 1
    @Hans-PeterSchrei I added it, although my eyes bleed. 😉
    – egreg
    Commented Aug 13 at 13:02
3

you not provide MWE, so we can only guess which document class you use and about page layout, and how is defined \specialcell command.

žPossible solution can be:

  • define page layout with use geometry package
  • for specialcell use thead of malecell
  • remove definitions of S cells width
  • instead of tabular use tabular* and determination of distance of columns left to LaTeX:
\documentclass{article}
\usepackage{geometry}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum}                              % for dummy text
%---------------------------------------------------------------%
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{makecell}

\begin{document}
    \begin{table}[ht]
    \centering
    \small
    \setlength\tabcolsep{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{S[table-format=-2.1, round-precision=1]} }
    \toprule
    & {\thead{Stock Market \\ Crash \\ 1973-1974}}  & {\thead{Bear\\ Market \\ 1980-1982}} 
    & {\thead{Stock\\ Market \\ Crash \\ 1987}}       & {\thead{Gulf\\ War \\ 1990}}
    & {\thead{Dot-Com\\ Bubble \\ 2000}}              & {\thead{Financial \\ Crisis \\ 2008}}
    & {\thead{Equity Markets \\ Rebound \\ 2009}}   & {\thead{Oil Prices \\ Drop \\ 2010}} \\
    \midrule
    Nexi SpA & -72.5\si{\percent} & -37.2\si{\percent} & -34.7\si{\percent} & -30.1\si{\percent} 
    & -58.0\si{\percent} & -76.1\si{\percent} & 65.0\si{\percent} & -10.6\si{\percent} \\
    Fiserv Inc & -49.9\si{\percent} & 12.8\si{\percent} & -44.1\si{\percent} & -27.5\si{\percent} 
    & -2.6\si{\percent} & -41.5\si{\percent} & 32.6\si{\percent} & -8.2\si{\percent} \\
    \bottomrule
\end{tabular*}
    \end{table}
\end{document}

enter image description here

(red lines indicate page layout)

You must log in to answer this question.

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