当乳胶中使用多列的特定行太长时,有什么方法可以调整列宽?

当乳胶中使用多列的特定行太长时,有什么方法可以调整列宽?

我正在使用乳胶进行学术写作,想知道是否有任何方法可以在下面的乳胶文档中美观地显示表格。

\documentclass[fleqn,12pt,a4]{article}

\usepackage{adjustbox}
\usepackage{threeparttable}


\begin{document}


\begin{table}
\centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{l*{4}{c}} \hline\hline 
                    &\multicolumn{1}{c}{(1)}         &\multicolumn{1}{c}{(2)}         &\multicolumn{1}{c}{(3)}         &\multicolumn{1}{c}{(4)}         \\
\hline \\ \multicolumn{5}{l}{Panel A: Outcome variable name is VERY VERY VERY VERY VERY VERY VERY VERY  LONG  }\\ \hline
Treatment &    0.121        &     0.142   &      0.201    &    0.198   \\
 &    (0.151)        &    (0.242)   &      (0.301)    &   (0.298)   \\

\hline\hline
\end{tabular}
\end{adjustbox}
\end{table}



\end{document}

当我编译这个文件时,我得到了这个。

在此处输入图片描述 我想自动调整列 (1) -- (4) 的宽度,使其宽度相等。有什么办法吗?我想保留它,adjustbox因为它对我的工作流程非常方便(从R或生成表格stata并将它们导入我的 latex 文件中)。

我正在使用 MacTeX-2022 和 pdflatex。

答案1

您需要允许在包含长字符串的单元格中出现换行。

如果您选择实施以下解决方案,您还需要指定所需或目标整个表格的宽度。在下表中,目标宽度设置为0.7\textwidth。根据长字符串的长度,您可能需要适当增加或减少此宽度。

在此处输入图片描述

\documentclass[12pt,a4paper]{article}
\usepackage{tabularx} % for 'tabularx' environment and 'X' column type
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{P}{>{\raggedright}p{\targetwidth}}
\newlength\targetwidth
\usepackage{booktabs} % for well-spaced horizontal rules

\begin{document}

\begin{table}
\setlength\targetwidth{0.7\textwidth} % choose a suitable width
\centering
\begin{tabularx}{\targetwidth}{@{} l CCCC @{}} 
\toprule
& (1) & (2) & (3) & (4) \\
\midrule
\multicolumn{5}{@{}P@{}}{Panel A: Outcome variable name is a VERY VERY VERY VERY VERY VERY VERY LONG STRING}\\ 
\midrule
Treatment &  0.121   &   0.142   &   0.201   &  0.198   \\
          & (0.151)  &  (0.242)  &  (0.301)  & (0.298)  \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

答案2

不要使用adjustbox来使表格宽度与文本宽度一样宽,因为这会导致字体大小不一致。您可以改用tabularx

我展示了两个实现;在第二个中,我假设您有几个称为“Panel X”的部分,因此我们可以设置悬挂缩进。

\documentclass[fleqn,12pt,a4paper]{article}

\usepackage{tabularx}
\usepackage{booktabs}

\newcommand{\panel}[3]{%
  % #1 = number of columns to span
  % #2 = name of the panel
  % #3 = text
  \multicolumn{#1}{@{}p{\textwidth}@{}}{%
    \settowidth{\leftskip}{Panel M: }%
    \makebox[0pt][r]{\makebox[\leftskip][l]{Panel #2:}}%
    #3%
  }%
}

\begin{document}

\begin{table}[htp]
\centering

\begin{tabularx}{\textwidth}{
  @{}
  l
  *{4}{>{\centering\arraybackslash}X}
  @{}
}
\toprule
& (1) & (2) & (3) & (4) \\
\midrule
\multicolumn{5}{@{}p{\textwidth}@{}}{Panel A: Outcome variable name is 
  VERY VERY VERY VERY VERY VERY VERY VERY  LONG} \\
\midrule
Treatment &  0.121  &  0.142  &  0.201  &  0.198  \\
          & (0.151) & (0.242) & (0.301) & (0.298) \\
\bottomrule
\end{tabularx}

\end{table}


\begin{table}[htp]
\centering

\begin{tabularx}{\textwidth}{
  @{}
  l
  *{4}{>{\centering\arraybackslash}X}
  @{}
}
\toprule
& (1) & (2) & (3) & (4) \\
\midrule
\panel{5}{A}{Outcome variable name is 
  VERY VERY VERY VERY VERY VERY VERY VERY  LONG} \\
\midrule
Treatment &  0.121  &  0.142  &  0.201  &  0.198  \\
          & (0.151) & (0.242) & (0.301) & (0.298) \\
\bottomrule
\end{tabularx}

\end{table}

\end{document}

在此处输入图片描述

答案3

您可以使用p{...}创建一个固定列。任何超出该列长度的文本将被换行。它也可以与 一起使用,\multicolumn效果相同。

在下面的解决方案中,我使用了 ,threeparttable因为您在序言中已经使用了它。我还添加了makecell。即使使用了较大的拉伸,我也会保持带括号和不带括号的相应数字之间的基本间距,这可能是一个小小的优势。

在此处输入图片描述

\documentclass[fleqn,12pt,a4paper]{article}
\usepackage{threeparttable}
\usepackage{booktabs}
\usepackage{array}
\usepackage{makecell}

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}


\begin{document}
\begin{table}
    %%% Settings for makecells
    \renewcommand\cellalign{tc}
    \renewcommand\cellset{\renewcommand\arraystretch{1}}
    \renewcommand\cellgape{\gape[b]}
    %%% Settings for vertical spacing 
    \renewcommand{\arraystretch}{2.25}
    %     correction for array stretch
    \setlength\extrarowheight{-3pt}
    %%% TABLE
    \begin{threeparttable}
        \caption{This is the caption}
        \begin{tabular}{
                p{\dimexpr\linewidth-10\tabcolsep-8.8cm}
                *4{C{2.2cm}}
            }
            \toprule
            & (1) & (2) & (3) & (4) \\
            \midrule
            \multicolumn{5}{p{\dimexpr\linewidth-2\tabcolsep}}{%
                Panel A: Outcome variable name is VERY VERY VERY
                VERY VERY VERY VERY VERY LONG} \\
            \hline
            Treatment
            & \makecell{0.121\\(0.151)}
            & \makecell{0.142\\(0.242)}
            & \makecell{0.201\\(0.301)}
            & \makecell{0.198\\(0.298)} \\
            \bottomrule
        \end{tabular}
        \strut\footnotesize
        Comments and annotations under the table
    \end{threeparttable}
\end{table}
\end{document}

相关内容