0

I use the following code to produce a table (using XeLaTex):

%!TEX encoding = UTF-8 Unicode

\documentclass[twoside,a4paper]{article}

%---- Allow the insertion of graphics and tables ----%
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{multirow}

%---- Set margins ----%
\usepackage{geometry}
 \geometry{a4paper,total={210mm,297mm},
 left=20mm,
 right=20mm,
 top=25mm,
 bottom=25mm,
 }

%---- Set line spacing to 1.5 lines ----%
\usepackage{setspace}
\onehalfspacing

%----- Set fonts ----%

\usepackage{fontspec}
\setmainfont{Calibri}

\begin{document}

\begin{table}[htbp]
  \centering
  \caption{Table title}
\resizebox{160mm}{!}{    
    \begin{tabular}{ccccc}
    \toprule
    \multirow{2}[2]{*}{\textbf{Col. 1}} & \multicolumn{2}{c}{\textbf{Section 1 title}} & \multicolumn{2}{c}{\textbf{Extremely long section title in multicolumn}} \\
          & \textbf{Subtitle A} & \textbf{Subtitle B} & \textbf{Subtitle C} & \textbf{Subtitle C} \\
   \midrule
    Row 1  &  xxx &  xxx &  xxx & xxx \\
    Row 2  &  xxx &  xxx &  xxx & xxx \\
    Row 3  &  xxx &  xxx &  xxx & xxx \\
    Row 4  &  xxx &  xxx &  xxx & xxx \\
    Row 5  &  xxx &  xxx &  xxx & xxx \\
        \toprule
    \textbf{Total} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} \\
    \bottomrule
    \end{tabular}%
}
  \label{tab:addlabel}%
\end{table}%

\end{document}

I would like to do two things:

  • Manually insert line breaks in certain cells:

    "Subtitle A" and "Subtitle B" - insert breaks between "Subtitle" and "A"/"B"

    "Extremely long..." - insert break between "Extremely long section" and "title in multicolumn"

  • Make the columns "Subtitle A" to "Subtitle D" the same width (larger than "Col. 1")

How can this be achieved?

3

You can achieve all this with David Carlisle's tabularx package. I thought to provide 2 different solutions, since manual breaks can be done inside a tabular environment, but that does not solve your problem properly IMO.

Column width is fixed within tabularx preamble:

>{\centering\arraybackslash}X means "centered-text (>{\centering\arraybackslash}), fixed-width (X) column". This fixed width depends on tabularx total width.
In place of \centering you could use \raggedleft or \raggedright. Default X column has text aligned to the left.

This is the first solution, which uses manual breaks, using command \break. Notice that manual \break is not working in \multicolumn's argument, therefore the extremely long title cannot be splitted.

\documentclass[twoside,a4paper]{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{tabularx}

\usepackage{geometry}
 \geometry{a4paper,
 left=20mm,
 right=20mm,
 top=25mm,
 bottom=25mm,
 }

\usepackage{setspace}
\onehalfspacing

\usepackage{fontspec}
\setmainfont{Calibri}

\begin{document}

\begin{table}[htbp]
  \centering
  \caption{Table title}
    \begin{tabularx}{160mm}
    {c
    >{\centering\arraybackslash}X  %centered-text, fixed-width column
    >{\centering\arraybackslash}X
    >{\centering\arraybackslash}X
    >{\centering\arraybackslash}X}
    \toprule
    \multirow{3}[3]{*}{\textbf{Col. 1}} & \multicolumn{2}{c}{\textbf{Section 1 title}} & \multicolumn{2}{c}{\textbf{Extremely long section title in multicolumn}} \\
          & \textbf{Subtitle\break A} & \textbf{Subtitle\break B} & \textbf{\break Subtitle C} & \textbf{\break Subtitle D} \\
   \midrule
    Row 1  &  xxx &  xxx &  xxx & xxx \\
    Row 2  &  xxx &  xxx &  xxx & xxx \\
    Row 3  &  xxx &  xxx &  xxx & xxx \\
    Row 4  &  xxx &  xxx &  xxx & xxx \\
    Row 5  &  xxx &  xxx &  xxx & xxx \\
        \toprule
    \textbf{Total} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} \\
    \bottomrule
    \end{tabularx}%
  \label{tab:addlabel}%
\end{table}%

\end{document}

The second one doesn't use \break, it has one more row to properly arrange every subtitle.

\documentclass[twoside,a4paper]{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{tabularx}

\usepackage{geometry}
 \geometry{a4paper,
 left=20mm,
 right=20mm,
 top=25mm,
 bottom=25mm,
 }

\usepackage{setspace}
\onehalfspacing

\usepackage{fontspec}
\setmainfont{Calibri}

\begin{document}

\begin{table}[htbp]
  \centering
  \caption{Table title}
    \begin{tabularx}{160mm}
    {c
    >{\centering\arraybackslash}X  %centered-text, fixed-width column
    >{\centering\arraybackslash}X
    >{\centering\arraybackslash}X
    >{\centering\arraybackslash}X}
    \toprule
    \multirow{3}[3]{*}{\textbf{Col. 1}} & \multicolumn{2}{c}{\textbf{Section 1 title}} & \multicolumn{2}{c}{\textbf{Extremely long section}} \\
    &\textbf{Subtitle}&\textbf{Subtitle}&\multicolumn{2}{c}{\textbf{title in multicolumn}}\\
          &\textbf{A} &\textbf{B} & \textbf{Subtitle C} & \textbf{Subtitle D} \\
   \midrule
    Row 1  &  xxx &  xxx &  xxx & xxx \\
    Row 2  &  xxx &  xxx &  xxx & xxx \\
    Row 3  &  xxx &  xxx &  xxx & xxx \\
    Row 4  &  xxx &  xxx &  xxx & xxx \\
    Row 5  &  xxx &  xxx &  xxx & xxx \\
        \toprule
    \textbf{Total} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} \\
    \bottomrule
    \end{tabularx}%
  \label{tab:addlabel}%
\end{table}%

\end{document}
  • 2
    With \\ is impossible: LaTeX will think there is a tabular end of line. You can use \break as in the first solution, but it creates a new line and moves down all the following rows without an explicit specification in the code. And this makes me feel unconfortable. – RicoRally Nov 21 '14 at 12:46
  • Cool answer! What about \par instead of \break? This was my "manually"-habit...which one is better? – MattAllegro Nov 21 '14 at 18:24
2

The makecell package is done for that: it allows for linebreaks in cells, and common formatting for column heads. Also you shouldn't use \esizebox as it also scales the text in a tabular. I used the tabulars environment instead, with a table total width of 16 cm.. Although one should not use \multicolumns that come across X type columns, it seems to be working in the present case. Finally, I loadd the caption package to ensure proper vertical spacing between caption and table.

%!TEX encoding = UTF-8 Unicode
\documentclass[twoside,a4paper]{article}

%---- Allow the insertion of graphics and tables ----%
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{multirow}

%---- Set margins ----%
\usepackage{geometry}
\geometry{a4paper, hmargin=20mm, vmargin=25mm }

%---- Set line spacing to 1.5 lines ----%
\usepackage{setspace}
\onehalfspacing

%----- Set fonts ----%

\usepackage{fontspec}
\setmainfont{Calibri}

\usepackage{tabularx}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}

\usepackage{caption}
\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[htbp]
\centering
\caption{Table title}
\label{tab:addlabel}%
\begin{tabularx}{160mm}{c*{4}{X}}
\toprule
&  multicolumn{2}{c}{\thead{Section 1 title}}  & \multicolumn{2}{c}{\thead{Extremely long section\\ title in multicolumn}} \\
\thead{Col. 1} & \thead{Subtitle\\ A} & \thead{Subtitle\\ B} & \thead{Subtitle\\ C} & \thead{Subtitle\\ D} \\
\midrule\addlinespace
Row 1 & xxx & xxx & xxx & xxx \\
Row 2 & xxx & xxx & xxx & xxx \\
Row 3 & xxx & xxx & xxx & xxx \\
Row 4 & xxx & xxx & xxx & xxx \\
Row 5 & xxx & xxx & xxx & xxx \\
\addlinespace
    \toprule
\textbf{Total} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} & \textbf{xxx} \\
\bottomrule
\end{tabularx}%
\end{table}%

\end{document} 

enter image description here

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.