2

I have the following sidewaystable, in which the headers and the last cell need to be across two columns with line breaks, else the table is not readable. The "specs" columns are just necessary in the "topic" rows, not in the header and in the problematic line.

My issues are with "Header 4" and with the cells in the problematic line.

\begin{sidewaystable}
\sffamily
\centering
\setlength\extrarowheight{4pt}
    \begin{tabularx}{\textwidth}{@{}>{\bfseries}l|XrXrXrXr|}

& \multicolumn{2}{l}{{Header 1}} & \multicolumn{2}{l}{{Header 2}} & \multicolumn{2}{l}{{Header 3}} & \multicolumn{2}{X}{{Header 4, which is too long}}\\ \hline

Topic 1         &$P_{1a}$.& Spec. &$P_{2a}$.& Spec. & $P_{3a}$.&    Spec. &$P_{4a}$ &Spec.\\
More topics     &...&...&...&...&...&...&...&...\\

Problematic line:&  
\multicolumn{2}{X}{Long text over two columns, which necessitates a line break}& 
\multicolumn{2}{X}{Long text over two columns, which necessitates a line break}& 
\multicolumn{2}{X}{Long text over two columns, which necessitates a line break}&
\multicolumn{2}{X}{Long text over two columns, which necessitates a line break}\\

    \end{tabularx}  
\end{sidewaystable}

Table from the code

If I don't use "X" in the multicolumn specification, but, i.e. "l", the line break does not work, but the cells cover both columns.

How can I have a line break and simultaneously a multicolumn cell in this context?

2

similar to @Bernard answer (+1), but without \makebox[\linewidth]{...} and use makecell package for writing column headers and adding more vertical space around cells' contents:

enter image description here

\documentclass{article}
\usepackage{rotating}
\usepackage{ragged2e}
\usepackage{makecell, tabularx}
\renewcommand\theadfont{\normalsize}
\renewcommand\theadgape{}
\setcellgapes{3pt}

\begin{document}
    \begin{sidewaystable}
\sffamily
\centering
\makegapedcells
\begin{tabularx}{\textwidth}{@{}>{\bfseries}l|*{4}{>{\RaggedRight\arraybackslash}X}|}
        & \thead[b]{Header 1}
            & \thead[b]{Header 2}
                & \thead[b]{Header 3}
                    & \thead[b]{Header 4, which is\\ 
                                longer than one line}   \\ \hline
Topic 1 &$P_{1a}$.\hfill Spec.
            & $P_{2a}$. \hfill Spec.
                & $P_{3a}$.\hfill Spec.
                    & $P_{4a}$ \hfill Spec.             \\
More topics
        & \dots\hfill\dots
            & \dots\hfill\dots
                & \dots\hfill\dots
                    & \dots\hfill\dots                  \\
Problematic line:
    &   A long text over many rows, which necessitates a line break
        &   A long text over many rows, which necessitates a line break
            &   A long text over many rows, which necessitates a line break
                &   A long text over many rows, which necessitates a line break
\end{tabularx}
    \end{sidewaystable}
\end{document}

addendum: unfortunately you didn't provide necessary information and today is also my crystal ball out of order ... according to your comment i estimate, that the following variant of my answer (slightly repaired your mwe) should do what you are after:

\documentclass{article}
\usepackage{rotating}
\usepackage{ragged2e}
\usepackage{makecell, tabularx}
\renewcommand\theadfont{\normalsize}
\renewcommand\theadgape{}
\setcellgapes{3pt}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\newcolumntype{R}{>{\RaggedLeft\arraybackslash}X}

\begin{document}
    \begin{sidewaystable}
\sffamily
\centering
\makegapedcells
\newcommand\mcxx[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}L}{#1}}
\newcommand\mcxl[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}L|}{#1}}
\begin{tabularx}{\textwidth}{@{}>{\bfseries}l|*{4}{L R}|}
        & \mcxx{\thead[b]{Header 1}}
            & \mcxx{\thead[b]{Header 2}}
                & \mcxx{\thead[b]{Header 3}}
                    & \mcxl{\thead[b]{Header 4, which is\\
                                longer than one line}}   \\ \hline
Topic 1 &$P_{1a}$. and some more text
                &   Spec.
            & $P_{2a}$. &   Spec.
                & $P_{3a}$. &   Spec.
                    & $P_{4a}$. &   Spec.               \\
More topics
        & \dots     &   \dots
            & \dots     &   \dots
                & \dots     &   \dots
                    & \dots     &   \dots               \\
Problematic line:
    &   \mcxx{A long text over many rows, which necessitates a line break}
        &   \mcxx{A long text over many rows, which necessitates a line break}
            &   \mcxx{A long text over many rows, which necessitates a line break}
                &   \mcxl{A long text over many rows, which necessitates a line break}
\end{tabularx}
    \end{sidewaystable}
\end{document}

enter code here

  • Thank you for your solution. It nearly solves my problem, but \hfill won't work in my context, as there is text to be inserted, for example behind the P_{1a} (which I honestly forgot to mention). If there is a line break, I need "spec" still to be aligned with "Topic 1". – Maximilian May 6 at 22:33
  • @Maximilian, if you not present your context, i cant help you. – Zarko May 6 at 22:38
  • my context would increase the complexity unnecessarily, that is why I tried to simplify it. I think your solution is a very elegant one, but I doesn't solve my issue completely. But I appreciate your effort. – Maximilian May 6 at 23:01
  • @Maximilian, see addendum too my answer. – Zarko May 6 at 23:04
2

How about this layout?

\documentclass{article}
\usepackage{rotating}
\usepackage{tabularx}
\usepackage{ragged2e}

\begin{document}

\begin{sidewaystable}
\sffamily \setlength{\tabcolsep}{4pt}
\centering
\setlength\extrarowheight{4pt}
\begin{tabularx}{\textwidth}{@{}>{\bfseries}l|*{4}{>{\RaggedRight\arraybackslash}X}|}


& Header 1 & Header 2 & Header 3 & Header 4, which is too long \\ \hline
Topic 1 & {\begin{tabularx}{\linewidth}{@{}Xr@{}} $P_{1a}$. & Spec.\end{tabularx}} & {\begin{tabularx}{\linewidth}{@{}Xr@{}} $P_{2a}$. & Spec.\end{tabularx}} & {\begin{tabularx}{\linewidth}{@{}Xr@{}} $P_{3a}$. & Spec.\end{tabularx}} & {\begin{tabularx}{\linewidth}{@{}Xr@{}} $P_{4a}$. & Spec.\end{tabularx}} \\
More topics & \makebox[\linewidth]{ ... \hfill ...} & \makebox[\linewidth]{ ... \hfill ...} & \makebox[\linewidth]{ ... \hfill ...} & \makebox[\linewidth]{ ... \hfill ...} \\

Problematic line:&
 A long text over two co\-lumns, which necessitates a line break &
A long text over two co\-lumns, which necessitates a line break &
A long text over two co\-lumns, which necessitates a line break &
A long text over two co\-lumns, which necessitates a line break

\end{tabularx}

\end{sidewaystable}

\end{document} 

enter image description here

  • Thank you for your solution. It nearly solves my problem, but \hfill won't work in my context, as there is text to be inserted, for example behind the P_{1a} (which I honestly forgot to mention). If there is an additional line break, I need "spec" still to be aligned with "Topic 1". – Maximilian May 6 at 22:31
  • Will this also happen in the Spec. column (I mean text afterwards)? – Bernard May 6 at 22:39
  • No, that's just a variable; the cells won't contain additional texts. – Maximilian May 6 at 22:55
  • Please see my updated answer: I replaced \makebox + \hfill with a nested tabularx. – Bernard May 6 at 23:10
0

With the help of >{\hsize=.75\hsize} you can adjust the width of X type columns:

enter image description here

\documentclass{article}
\usepackage{rotating}
\usepackage{tabularx}
\usepackage{ragged2e}

\begin{document}

\begin{sidewaystable}
\sffamily
\centering
\setlength\extrarowheight{4pt}
    \begin{tabularx}{\textwidth}{@{}>{\bfseries}l|*{4}{>{\hsize=.75\hsize}X>{\hsize=.25\hsize\RaggedLeft}X}|}

& \multicolumn{2}{l}{{Header 1}} & \multicolumn{2}{l}{{Header 2}} & \multicolumn{2}{l}{{Header 3}} & \multicolumn{2}{X}{{Header 4, which is too long}}\\ \hline

Topic 1         &$P_{1a}$.& Spec. &$P_{2a}$.& Spec. & $P_{3a}$.&    Spec. &$P_{4a}$ &Spec.\\
More topics     &...&...&...&...&...&...&...&...\\

Problematic line:&  
\multicolumn{2}{>{\RaggedRight}X}{Long text over two columns, which necessitates a line break}& 
\multicolumn{2}{>{\RaggedRight}X}{Long text over two columns, which necessitates a line break}& 
\multicolumn{2}{>{\RaggedRight}X}{Long text over two columns, which necessitates a line break}&
\multicolumn{2}{>{\RaggedRight}X}{Long text over two columns, which necessitates a line break}\\

    \end{tabularx}  
\end{sidewaystable}

\end{document}

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.