4

Usually, when I add tables I use \usepackage{booktabs} and code that looks like this:

\begin{table}[ht]
    \centering
    \begin{tabular}{l|rrr}
    \toprule
    Country / Property & Population       & Area     & HDI \\\midrule
    France             & $66 \cdot 10^6$  & $\SI{668763}{\km\squared}$  & 0.89 \\
    Germany            & $81 \cdot 10^6$  & $\SI{357167}{\km\squared}$  & 0.92 \\
    United States      & $317 \cdot 10^6$ & $\SI{9629091}{\km\squared}$ & 0.94 \\\bottomrule
    \end{tabular}
    \caption{Information about countries}
    \label{table:countries}
\end{table}

But is there any semantic way to mark the table header row?

Or a way to change the style of a complete row / column?

enter image description here

MWE

\documentclass[a4paper]{scrartcl}
\usepackage{amssymb, amsmath} % needed for math
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout

\usepackage{siunitx}
\usepackage{booktabs}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Begin document                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

    \newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
    \newcolumntype{^}{>{\currentrowstyle}}
    \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
    #1\ignorespaces
    }

    \begin{table}[ht]
        \centering
        \begin{tabular}{+l|^r^r^r}
        \toprule
        \rowstyle{\bfseries}%
        Country / Property & Population       & Area     & HDI \\\midrule
        France             & $66 \cdot 10^6$  & $\SI{668763}{\km\squared}$  & 0.89 \\
        Germany            & $81 \cdot 10^6$  & $\SI{357167}{\km\squared}$  & 0.92 \\
        United States      & $317 \cdot 10^6$ & $\SI{9629091}{\km\squared}$ & 0.94 \\\bottomrule
        \end{tabular}
        \caption{Information about countries}
        \label{table:countries}
    \end{table}
\end{document}

What I've seen so far

On www.latex-community.org I have seen this:

\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
#1\ignorespaces
}

\begin{table}[ht]
    \centering
    \begin{tabular}{+l|^r^r^r}
    \toprule
    \rowstyle{\bfseries}%
    Country / Property & Population       & Area     & HDI \\\midrule
    France             & $66 \cdot 10^6$  & $\SI{668763}{\km\squared}$  & 0.89 \\
    Germany            & $81 \cdot 10^6$  & $\SI{357167}{\km\squared}$  & 0.92 \\
    United States      & $317 \cdot 10^6$ & $\SI{9629091}{\km\squared}$ & 0.94 \\\bottomrule
    \end{tabular}
    \caption{Information about countries}
    \label{table:countries}
\end{table}
4
  • 1
    Please complete your code to provide a compilable document. That is much easier to work with than mere fragments. I'm actually not really at all clear what you are asking. What sort of markup do you want? And obviously you can change the style of a column when you specify the settings for the environment. (A row is a bit trickier.) longtable defines \endhead and \endfirsthead and similarly for the footer. Other packages do similar things but I'm not sure if that's what you mean or not. – cfr Aug 25 '14 at 1:44
  • @cfr: Sorry, I sometimes forget about this. The MWE was added. – Martin Thoma Aug 25 '14 at 2:36
  • @cfr: I want to make sure that I have a consistent way in which I show my tables. Hence I would like to have something like \headercell{content} where I can specify what the header should look like. Of course, I could define a macro myself. But I would like to know if there already exists something like this that eventually allows more sophisticated options like "add a border like \midrule between header and content" or an option that I only have to state once "the following row / column is a header and not do it for every single cell. – Martin Thoma Aug 25 '14 at 2:42
  • @Mico: I think I'll write an article about it as soon as I have some free time and leave a comment here. Or, probably better, contact you via chat. I think that does eventually not belong here. (I've removed my comment and will remove this comment, too.) – Martin Thoma Aug 25 '14 at 11:13
5

Rather than try to improve the display of the information given in the top left-hand cell, it may be more fruitful to disentangle the information and create two groups of columns -- a "country" column and a "property" column, with the latter getting three sub-columns (population, area, and HDI).

To enhance the readability of the table, it's probably also a good idea to remove common factors (\cdot 10^6 and \km\squared) from the cells and place that information in the header section. From a point of view of programming aesthetics, it's probably also a good idea to choose S as the column type for columns 2, 3, and 4, rather than giving them a generic r type and then overlaying various cells with \SI macros.

I wouldn't rule out categorically the use of bold face for material provided in the header cells. More often than not, though, use of bold face smacks of a desperate (and frequently futile) attempt to rescue an improperly designed table from irrelevance.

The screenshot below shows the output of your original code as well as the result of implementing the suggestions given above. If I wanted to improve the table some more, I'd probably start with stating the countries' areas in thousands of square kilometers -- and round off the decimals completely.

enter image description here

\documentclass{article}
\usepackage{booktabs,siunitx,caption}
\begin{document}
\begin{table}[ht]
    \caption{Information about countries}
    \label{table:countries}
    \centering

    \begin{tabular}{l|rrr}
    \toprule
    Country / Property & Population       & Area     & HDI \\\midrule
    France             & $66 \cdot 10^6$  & $\SI{668763}{\km\squared}$  & 0.89 \\
    Germany            & $81 \cdot 10^6$  & $\SI{357167}{\km\squared}$  & 0.92 \\
    United States      & $317 \cdot 10^6$ & $\SI{9629091}{\km\squared}$ & 0.94 \\\bottomrule
    \end{tabular}

    \bigskip

    \begin{tabular}{lS[table-format=3.0]
                     S[table-format=7.0]
                     S[table-format=1.2]}
    \toprule
    Country & \multicolumn{3}{c}{Property}\\
    \cmidrule{2-4}
    & {Population} & {Area} & {HDI} \\
    & {(mio.)} & {(km\textsuperscript{2})}\\
    \midrule
    France             & 66  & 668763  & 0.89 \\
    Germany            & 81  & 357167  & 0.92 \\
    United States      & 317 & 9629091 & 0.94 \\
    \bottomrule
    \end{tabular}

\end{table}
\end{document} 
1
  • 1
    I find the “Property” header useless, as it adds no information. – egreg Aug 25 '14 at 9:13
1

Table can also be created as shown below.

\documentclass{article}
\usepackage{booktabs,siunitx,caption}
\begin{document}
\begin{table}[ht]
    \caption{Information about countries}
    \label{table:countries}
    \centering
\begin{tabular*}{1.2\textwidth}{r|r|r||r|r|r||r|r|r}
 \toprule
  \multicolumn{9}{c}{Country}\\
\midrule
  \multicolumn{3}{c}{France} & \multicolumn{3}{c}{Germany} & \multicolumn{3}{c}{United States}\\
\midrule

\multicolumn{3}{c}{Property}& \multicolumn{3}{c}{Property}& \multicolumn{3}{c}{Property}\\
\midrule
Population & Area &{HDI}& Population & Area &{HDI}&Population & Area &{HDI}\\
 {(mio.)}& {(km\textsuperscript{2})}& &{(mio.)}& {(km\textsuperscript{2})}& &{(mio.)}& {(km\textsuperscript{2})}&\\
 \midrule
 66  & 668763  & 0.89 &  81  & 357167  & 0.92 & 317 & 9629091 & 0.94 \\
 \bottomrule
 \end{tabular*}
\end{table}
\end{document} 

output: tablenew

4
  • Is it a good idea to specify a table width that's 20 percent larger than the width of the text block? – Mico Aug 25 '14 at 9:16
  • While viewing the whole page with text and tables it may add some visual impact – murugan Aug 25 '14 at 9:33
  • But how do you know that making the table 20% wider than the text block won't also make the table exceed the physical width of the paper that the document might get printed on? That'll make for terrible visual impact, won't it? In general, unless you can be absolutely sure that it's permissible to expand the width of the text block, it is simply not a good idea to let anything exceed that width. – Mico Aug 25 '14 at 13:16
  • Thanks. Absolutely correct. Don't expand the width of the text block beyond the actual width designed with a view to get the document printed. – murugan Aug 25 '14 at 13:40

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.