将 dcolumn 中定义的列类型与 tabularx 结合起来,第 2 部分

将 dcolumn 中定义的列类型与 tabularx 结合起来,第 2 部分

这是对我问的另一个问题。我正在尝试将dcolumn包与tabularx(或tabular*就此而言)结合起来,以便表格跨越我指定的宽度,列间距均匀,单元格按小数点对齐。

这是我现在所拥有的在此处输入图片描述

另一个问题的答案中建议的技巧是在环境中使用“D”列tabularx,然后\multicolumn在表格前几行中的 s 内使用“X”列。这在该问题给出的示例中效果很好。但现在我有一个很长的文本\multicolumn。“X”列似乎“忽略”了要跨越的列数\multicolumn。将“X”更改为“c”可防止单元格中断,如下所示:在此处输入图片描述

我认为一个解决方案是使用“p”列。但在我看来,指定列宽违背了目的,除非有自动执行的方法——我试图获得一些灵感从这个问题的答案这里但没走多远。

关于如何让文本跨越两列,您有什么想法吗?

编辑:这是我认为理想的基本想法。通过声明\tracingtabularx,tabularx 将打印最佳列宽。所以我第一次使用“X”列,看到 tabularx 在我的日志文件中回显的内容,将其乘以 2,然后将 X 列更改为具有计算宽度的“p”列。当然,手动执行此操作毫无意义。我想要的是让 tabularx 为我完成此操作。

以下是生成上述表格的代码:

\documentclass{report}

\usepackage{array}
\usepackage{tabularx}    
\usepackage{dcolumn}
\usepackage{booktabs}

\newcommand{\mc}{\multicolumn} % handy shortcut macro
\newcolumntype{Z}{>{\centering\arraybackslash}X} % handy shortcut macro
\newcolumntype{d}[1]{D{.}{\cdot}{#1}} % and yet another handy shortcut macro

\begin{document}

% TABLE 1
\noindent
The width of the text block is indicated by this horizontal rule:
\hrule

\begin{table}[h]
\centering
\caption{ multicolumn\{2\}\{X\}\{text\} }
\begin{tabularx}{\textwidth}{l*{3}{d{-2}}}

\toprule
                  & \mc{1}{X}{}       & \mc{2}{X}{A very long text that should span two columns, not one. 'X' doesn't do the trick.}  \\ \cmidrule{3-4}
                  & \mc{1}{Z}{(1)}    & \mc{1}{Z}{(2)} & \mc{1}{Z}{(3)}   \\ \cmidrule(r){2-2} \cmidrule(lr){3-3} \cmidrule(l){4-4}
                  & \mc{1}{Z}{Median} & \mc{1}{Z}{Low} & \mc{1}{Z}{High}  \\ \midrule 
 bla bla bla      &      2.74         &        31.0    &   1.2            \\ 
 ble ble ble ble  &     31.81         &         1.14   &  11.10           \\ 

 \bottomrule
 \end{tabularx}
 \end{table}

% TABLE 2
\noindent
The width of the text block is indicated by this horizontal rule:
\hrule

\begin{table}[h]
\centering
\caption{ multicolumn\{2\}\{c\}\{text\} }
\begin{tabularx}{\textwidth}{l*{3}{d{-2}}}

\toprule
                  & \mc{1}{X}{}       & \mc{2}{c}{A very long text that should be wrapped. 'p' would defeat the purpose.}  \\ \cmidrule{3-4}
                  & \mc{1}{Z}{(1)}    & \mc{1}{Z}{(2)} & \mc{1}{Z}{(3)}   \\ \cmidrule(r){2-2} \cmidrule(lr){3-3} \cmidrule(l){4-4}
                  & \mc{1}{Z}{Median} & \mc{1}{Z}{Low} & \mc{1}{Z}{High}  \\ \midrule 
 bla bla bla      &      2.74         &        31.0    &   1.2            \\ 
 ble ble ble ble  &     31.81         &         1.14   &  11.10           \\ 

 \bottomrule
 \end{tabularx}
 \end{table}

\end{document}

答案1

您可以通过对 提供一点帮助来实现tabularx;因为您想要跨越两个等宽的列,所以您告诉 LaTeX 自然宽度是保留宽度的两倍,加上 的两倍\tabcolsep

\documentclass{report}

\usepackage{array}
\usepackage{tabularx}    
\usepackage{dcolumn}
\usepackage{booktabs}

\newcommand{\mc}{\multicolumn} % handy shortcut macro
\newcolumntype{Z}{>{\centering\arraybackslash}X} % handy shortcut macro
\newcolumntype{d}[1]{D{.}{\cdot}{#1}} % and yet another handy shortcut macro

\begin{document}

% TABLE 1
\noindent
The width of the text block is indicated by this horizontal rule:
\hrule

\begin{table}[htp]
\centering
\caption{multicolumn\{2\}\{X\}\{text\} }
\begin{tabularx}{\textwidth}{l*{3}{d{-2}}}

\toprule
                  & \mc{1}{X}{}       & 
  \mc{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}X}{%
    A very long text that should span two columns, not one.
    `X' does the trick, with a little help.} \\
\cmidrule{3-4}
                  & \mc{1}{Z}{(1)}    & \mc{1}{Z}{(2)} & \mc{1}{Z}{(3)}   \\
\cmidrule(r){2-2} \cmidrule(lr){3-3} \cmidrule(l){4-4}
                  & \mc{1}{Z}{Median} & \mc{1}{Z}{Low} & \mc{1}{Z}{High}  \\
\midrule 
bla bla bla      &      2.74         &        31.0    &   1.2            \\ 
ble ble ble ble  &     31.81         &         1.14   &  11.10           \\ 

\bottomrule
\end{tabularx}
\end{table}

\end{document}

在此处输入图片描述

答案2

将长线放在 \parbox 中对我来说有效

\documentclass{report}

\usepackage{array}
\usepackage{tabularx}    
\usepackage{dcolumn}
\usepackage{booktabs}

\newcommand{\mc}{\multicolumn} % handy shortcut macro
\newcolumntype{Z}{>{\centering\arraybackslash}X} % handy shortcut macro
\newcolumntype{d}[1]{D{.}{\cdot}{#1}} % and yet another handy shortcut macro

\begin{document}

% TABLE 1
\noindent
The width of the text block is indicated by this horizontal rule:
\hrule



\begin{table}[h]
\centering
\caption{ multicolumn\{2\}\{c\}\{text\} }
\begin{tabularx}{\textwidth}{l*{3}{d{-2}}}

\toprule
                  & \mc{1}{X}{}       & \mc{2}{c}{\parbox{.5\columnwidth}{A very long text that should be wrapped. 'p' would defeat the purpose.}}  \\ \cmidrule{3-4}
                  & \mc{1}{Z}{(1)}    & \mc{1}{Z}{(2)} & \mc{1}{Z}{(3)}   \\ \cmidrule(r){2-2} \cmidrule(lr){3-3} \cmidrule(l){4-4}
                  & \mc{1}{Z}{Median} & \mc{1}{Z}{Low} & \mc{1}{Z}{High}  \\ \midrule 
 bla bla bla      &      2.74         &        31.0    &   1.2            \\ 
 \bottomrule
 \end{tabularx}
 \end{table}

\end{document}

相关内容