使用 tabularx 环境在多列单元格中换行

使用 tabularx 环境在多列单元格中换行

我有以下侧向表格,其中标题和最后一个单元格需要跨越两列并带有换​​行符,否则表格将无法阅读。“specs”列只是在“主题”行中是必需的,而不是在标题和有问题的行中。

我的问题出在“标题 4”以及有问题的行中的单元格。

\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}

代码表

如果我在多列规范中不使用“X”,而是使用“l”,则换行符不起作用,但单元格会覆盖两列。

在这种情况下,如何才能同时出现换行符和多列单元格?

答案1

类似于@Bernard 的回答(+1),但没有\makebox[\linewidth]{...}并使用makecell包来编写列标题并在单元格内容周围添加更多垂直空间:

在此处输入图片描述

\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}

附录: 不幸的是你没有提供必要的信息,今天我的水晶球也坏了……根据你的评论,我估计,我的答案的以下变体(稍微修复了你的姆韦)应该做你想做的事:

\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}

在此处输入代码

答案2

这个布局怎么样?

\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} 

在此处输入图片描述

答案3

借助它可以调整类型列>{\hsize=.75\hsize}的宽度:X

在此处输入图片描述

\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}

相关内容