3

I am trying to build a table with alternate evenly distributed columns and with each second column aligned to the decimal points.

By googling, I arrived to the following code:

\usepackage{tabularx}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{\cdot}{#1} }
\usepackage{array,booktabs,makecell}
\renewcommand\theadalign{cc}% centred tabular headers

\begin{table}
\caption{\label{tab1}Table 1}
\begin{tabularx}{\textwidth}{|X|d{2}||X|d{2}||X|d{2}|}
\hline
\thead{\textit{Country}}    & \thead{\textit{Weight}}   &   \thead{\textit{Country}}    &   \thead{\textit{Weight}} &   \thead{\textit{Country}}    &   \thead{\textit{Weight}} \\
\hline
\hline
\textbf{Belgium}    & $ 2.92 $  &   \textbf{Croatia}    &   $ 0.39 $    &   \textbf{Poland} &   $ 3.63 $    \\
\hline
\textbf{Bulgaria}   & $ 0.32 $  &   \textbf{Italy}  &   $ 12.39 $   &   \textbf{Portugal}   &   $ 1.19 $    \\
\hline
\textbf{Czech Rep.} & $ 1.94 $  &   \textbf{Cyprus} &   $ 0.08 $    &   \textbf{Romania}    &   $ 1.01 $    \\
\hline
\textbf{Denmark}    & $ 1.88 $  &   \textbf{Latvia} &   $ 0.11 $    &   \textbf{Slovenia}   &   $ 0.37 $    \\
\hline
\textbf{Germany}    & $ 27.18 $ &   \textbf{Lithuania}  &   $ 0.16 $    &   \textbf{Slovakia}   &   $ 0.65 $    \\
\hline
\textbf{Estonia}    & $ 0.13 $  &   \textbf{Luxembourg} &   $ 0.14 $    &   \textbf{Finland}    &   $ 1.63 $    \\
\hline
\textbf{Ireland}    & $ 1.77 $  &   \textbf{Hungary}    &   $ 1.06 $    &   \textbf{Sweden} &   $ 3.09 $    \\
\hline
\textbf{Greece} & $ 1.08 $  &   \textbf{Malta}  &   $ 0.04 $    &   \textbf{UK} &   $ 11.88 $   \\
\hline
\textbf{Spain}  & $ 6.66 $  &   \textbf{Netherlands}    &   $ 3.97 $    &   \textbf{EA19}   &   $ 74.80 $   \\
\hline
\textbf{France} & $ 11.61 $ &   \textbf{Austria}    &   $ 2.72 $    &   \textbf{EU28}   &   $ 100.00 $  \\
\hline
\end{tabularx}
\end{table}

However, with this code I get this:

enter image description here

So I have two problems: 1) For some reason the column "weight" is too large, with extra space on the right 2) Some text of the column "Country" is too big for size of the cell. Is this because of the size of the page?

Thanks!

Enrico
  • 87

2 Answers2

5

Please read https://www.inf.ethz.ch/personal/markusp/teaching/guides/guide-tables.pdf or have a look at Beautiful table samples

Concerning question no. 2:

By using X columns, the width of the columns is chosen to fit inside the available textwidth of your document. Normally latex would hyphenate all text too long for the cells, but it seems to need a little help with the country names, you can give optional hyphenation points with Luxem\-bourg.

\documentclass{article}

\usepackage{tabularx}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{\cdot}{#1} }
\usepackage{array,booktabs,makecell}
\renewcommand\theadalign{cc}% centred tabular headers

\begin{document}

\begin{table}
\caption{\label{tab1}Table 1}
\begin{tabularx}{\textwidth}{|X|d{2}||X|d{2}||X|d{2}|}
\hline
\thead{\textit{Country}}    & \thead{\textit{Weight}}   &   \thead{\textit{Country}}    &   \thead{\textit{Weight}} &   \thead{\textit{Country}}    &   \thead{\textit{Weight}} \\
\hline
\hline
\textbf{Belgium}    & $ 2.92 $  &   \textbf{Croatia}    &   $ 0.39 $    &   \textbf{Poland} &   $ 3.63 $    \\
\hline
\textbf{Bulgaria}   & $ 0.32 $  &   \textbf{Italy}  &   $ 12.39 $   &   \textbf{Portugal}   &   $ 1.19 $    \\
\hline
\textbf{Czech Rep.} & $ 1.94 $  &   \textbf{Cyprus} &   $ 0.08 $    &   \textbf{Romania}    &   $ 1.01 $    \\
\hline
\textbf{Denmark}    & $ 1.88 $  &   \textbf{Latvia} &   $ 0.11 $    &   \textbf{Slovenia}   &   $ 0.37 $    \\
\hline
\textbf{Germany}    & $ 27.18 $ &   \textbf{Lithuania}  &   $ 0.16 $    &   \textbf{Slovakia}   &   $ 0.65 $    \\
\hline
\textbf{Estonia}    & $ 0.13 $  &   \textbf{Luxem\-bourg} &   $ 0.14 $    &   \textbf{Finland}    &   $ 1.63 $    \\
\hline
\textbf{Ireland}    & $ 1.77 $  &   \textbf{Hungary}    &   $ 1.06 $    &   \textbf{Sweden} &   $ 3.09 $    \\
\hline
\textbf{Greece} & $ 1.08 $  &   \textbf{Malta}  &   $ 0.04 $    &   \textbf{UK} &   $ 11.88 $   \\
\hline
\textbf{Spain}  & $ 6.66 $  &   \textbf{Nether\-lands}    &   $ 3.97 $    &   \textbf{EA19}   &   $ 74.80 $   \\
\hline
\textbf{France} & $ 11.61 $ &   \textbf{Austria}    &   $ 2.72 $    &   \textbf{EU28}   &   $ 100.00 $  \\
\hline
\end{tabularx}
\end{table}
\end{document}

enter image description here


My suggestion:

\documentclass{article}

\usepackage{tabularx}
\usepackage{siunitx}
\usepackage{booktabs}

\renewcommand{\arraystretch}{1.3}
\setlength{\tabcolsep}{5pt}
\usepackage{caption} 
\captionsetup[table]{skip=5pt}

\begin{document}

\begin{table}
\caption{\label{tab1}Table 1}
\begin{tabularx}{\textwidth}{@{}*{3}{XS[table-format=3.2]@{\extracolsep{8pt}}}}
\toprule
Country    & {Weight} & Country    & {Weight} & Country  & {Weight}\\
\cmidrule{1-2}\cmidrule{3-4}\cmidrule{5-6}
Belgium    & 2.92     & Croatia    & 0.39     & Poland   & 3.63\\
Bulgaria   & 0.32     & Italy      & 12.39    & Portugal & 1.19\\
Czech Rep. & 1.94     & Cyprus     & 0.08     & Romania  & 1.01\\
Denmark    & 1.88     & Latvia     & 0.11     & Slovenia & 0.37\\
Germany    & 27.18    & Lithuania  & 0.16     & Slovakia & 0.65\\
Estonia    & 0.13     & Luxembourg & 0.14     & Finland  & 1.63\\
Ireland    & 1.77     & Hungary    & 1.06     & Sweden   & 3.09\\
Greece     & 1.08     & Malta      & 0.04     & UK       & 11.88\\
Spain      & 6.66     & Netherlands& 3.97     & EA19     & 74.80\\
France     & 11.61    & Austria    & 2.72     & EU28     & 100.00\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

enter image description here

  • Thanks, this looks good. I will just slightly adapt it to get italic text for the header :-) – Enrico Mar 07 '17 at 16:16
4

See, if the following (more professional look :-) ) of table is acceptable for you:

enter image description here

\usepackage[skip=1ex]{caption}
\usepackage{booktabs,makecell,tabularx}
\renewcommand\theadfont{\normalfont\itshape}
\newcommand\mch[1]{\multicolumn{1}{>{\centering}X}{\normalfont\itshape #1}}
\usepackage{siunitx}
\usepackage{array,booktabs,makecell}
\renewcommand\theadalign{cc}% centred tabular headers

\usepackage{showframe}% only for this MWE, remove in real document
\renewcommand*\ShowFrameColor{\color{green}}

\begin{document}
    \begin{table}
\caption{Table 1}
\label{tab1}
    \begin{tabularx}{\textwidth}{*{3}{>{\bfseries}l S[table-format=3.2]}}
    \toprule
\thead[l]{Country} &   \mch{Weight}    &   \thead[l]{Country}    
    &   \mch{Weight}    &   \thead[l]{Country} &   \mch{Weight}                    \\
    \cmidrule(r){1-2}\cmidrule(lr){3-4}\cmidrule(l){5-6}
Belgium     &   2.92    &   Croatia     &   0.39    &   Poland      &   3.63    \\
Bulgaria    &   0.32    &   Italy       &  12.39    &   Portugal    &   1.19    \\
    \addlinespace
Czech Rep.  &   1.94    &   Cyprus      &   0.08    &   Romania     &   1.01    \\
Denmark     &   1.88    &   Latvia      &   0.11    &   Slovenia    &   0.37    \\
    \addlinespace
Germany     &  27.18    &   Lithuania   &   0.16    &   Slovakia    &   0.65    \\
Estonia     &   0.13    &   Luxembourg  &   0.14    &   Finland     &   1.63    \\
    \addlinespace
Ireland     &  1.77     &   Hungary     &   1.06    &   Sweden      &   3.09    \\
Greece      &  1.08     &   Malta       &   0.04    &   UK          &  11.88    \\
    \addlinespace
Spain       &  6.66     &   Netherlands &   3.97    &   EA19        &  74.80    \\
France      & 11.61     &   Austria     &   2.72    &   EU28        & 100.00    \\
    \bottomrule
\end{tabularx}

    \end{table}
\end{document}

As you can see, beside new table layout also the code is simplified and in table you not need to hyphenate any words. Also it perfectly fit text width.

Edit: as suggest samcarter I change align of column headers at country, also replace midrule with cmidrule and left state names in boldface (regardless, that I will rather use normal face). To change them to normal face just delete >{\bfseries} before l column type:

\begin{tabularx}{\textwidth}{*{3}{l S[table-format=3.2]}}

and you will obtain. If you have time, please look: [how to design nice tables].2

enter image description here

Zarko
  • 296,517