调整表格宽度

调整表格宽度

快速问题:如何确保所有三个表格具有相同的宽度?理想情况下,整个页面都是相同的,而不修改输入文件的内容:

\begin{table}[!tbp]
\begin{center}
\caption{} \vspace{-.2cm}
\captionsetup{labelformat=empty,justification=justified,singlelinecheck=off, width=0.9\linewidth}

\label{table_firststage} 

\begin{subtable}{\textwidth} 
{\scriptsize \setlength{\tabcolsep}{6pt}  
\input{Tables/tab_fs.tex}}  
\end{subtable} 
\begin{subtable}{\textwidth} 
{\scriptsize \setlength{\tabcolsep}{8.5pt}   \vspace{2mm}
\input{Tables/tab_rf.tex}}  \end{subtable} 
\begin{subtable}{\textwidth} 
{\scriptsize \setlength{\tabcolsep}{8pt}   \vspace{2mm}
\input{Tables/tab_2sls.tex}} 
\end{subtable}

\captionsetup{labelformat=empty,justification=raggedright,singlelinecheck=off, width=0.9\linewidth} \vspace{-2mm} 
\begin{subtable}{\textwidth}
 \scriptsize \raggedright \hspace{22pt}
\input{Tables/PanelD.tex} 
\end{subtable} 
\end{center}
\normalsize 
\begin{minipage}{\textwidth} 
    {\footnotesize \textbf{Notes:} }
\end{minipage}
\end{table}

提前致谢!

答案1

是的,这无需触碰您的文件即可实现,只要您的文件内容足够相似即可(以下操作\subcaption在试运行期间禁用,您可能需要禁用更多命令)。

首先,我们必须考虑如何才能实现目标。我们的选择有:

  • 缩放字体大小(坏的想法,这会导致整个文档的大小不一致)
  • 缩放\tabcolsep(仍然很糟糕,结果看起来很糟糕,但比调整字体大小要好)

这里用一个小循环实现了后者。正如我所说,结果看起来很糟糕,但至少对于我从你的片段中拼凑起来的小 MWE 来说,它是可行的。不过我并不推荐这样做,我编写它只是为了编写它,仅此而已。

\begin{filecontents}[overwrite]{\jobname1.tex}
\subcaption*{Panel A: First stage}
\begin{tabular}{l*{6}{c}}
  \hline\hline
  & \multicolumn{4}{c}{log recruits and things p.c.} \\
  \cmidrule(lr){2-5}
  & (1) & (2) & (3) & (4) \\
  Here reg results \\
  \hline\hline
\end{tabular}
\end{filecontents}

\begin{filecontents}[overwrite]{\jobname2.tex}
\subcaption*{Panel B: Second stage}
\begin{tabular}{l*{6}{c}}
  \hline\hline
  & \multicolumn{4}{c}{log recruits p.c.} \\
  \cmidrule(lr){2-5}
  & (1) & (2) & (3) & (4567) \\
  Here reg results \\
  \hline\hline
\end{tabular}
\end{filecontents}

\begin{filecontents}[overwrite]{\jobname3.tex}
\subcaption*{Panel C: Third stage}
\begin{tabular}{l*{6}{c}}
  \hline\hline
  & \multicolumn{4}{c}{log recruits p.c.} \\
  \cmidrule(lr){2-5}
  & (1) & (2) & (three is a number, I guess) & (4) \\
  Here reg results \\
  \hline\hline
\end{tabular}
\end{filecontents}

\begin{filecontents}[overwrite]{\jobname4.tex}
\subcaption*{Panel D: Fourth stage}
\begin{tabular}{l*{6}{c}}
  \hline\hline
  & \multicolumn{4}{c}{log recruits p.c.} \\
  \cmidrule(lr){2-5}
  & (1) & (2) & (3) & (4) \\
  Here reg results and a bit of more text \\
  \hline\hline
\end{tabular}
\end{filecontents}

\documentclass{article}

\usepackage[showframe]{geometry}

\usepackage{subcaption}
\usepackage{booktabs}

\makeatletter
\providecommand\@gobblethree[3]{}
\newdimen\tabscaler@maxresiduum
\tabscaler@maxresiduum=100sp % must be at moste 100sp off
\newdimen\tabscaler@target
\newdimen\tabscaler@last
\newdimen\tabscaler@this
\newdimen\tabscaler@tcs@last
\newdimen\tabscaler@tcs@this
\newcount\tabscaler@loops
\protected\def\tabscaler@trialrun#1%
  {%
    \tabcolsep=#1\relax
    % this is exactly as wide as the last used tabcolsep, so reduce it a bit
    \ifdim\tabcolsep=\tabscaler@tcs@this
      \advance\tabcolsep-1sp
    \fi
    \tabscaler@last=\tabscaler@this
    \tabscaler@tcs@last=\tabscaler@tcs@this
    \tabscaler@tcs@this=\tabcolsep
    \sbox\z@{\input{\tabscaler@name}\unskip}%
    \tabscaler@this=\wd\z@
  }
\protected\def\tabscaler@loop
  {%
    \advance\tabscaler@loops\@ne
    \typeout{tabscaler loop: \the\tabscaler@loops}%
    % break criteria:
    %   1. The table is not bigger than target
    %   2. Residuum is below \tabscaler@maxresiduum
    \ifdim\tabscaler@this>\tabscaler@target
    \else
      \ifdim\dimexpr\tabscaler@target-\tabscaler@this<\tabscaler@maxresiduum
        \expandafter\expandafter\expandafter\@gobblethree
      \fi
    \fi
    \tabscaler@trialrun
      {%
        \fpeval
          {%
            (\tabscaler@target - \tabscaler@last)
            / (\tabscaler@this - \tabscaler@last)
            * (\tabscaler@tcs@this - \tabscaler@tcs@last)
            + \tabscaler@tcs@last
          }%
        pt%
      }%
    \tabscaler@loop
  }
\NewDocumentCommand \tabscaler { O{\linewidth} m }
  {%
    \begingroup
      \tabscaler@target=\dimexpr#1\relax
      \edef\tabscaler@name{\unexpanded{#2}}%
      % locally disable \subcaption
      \RenewDocumentCommand\subcaption{som}{\ignorespaces}%
      \tabscaler@tcs@this=-1pt % or else the z@ point isn't correct
      \tabscaler@trialrun\z@
      \ifdim\tabscaler@this>\tabscaler@target
        \PackageError{tabscaler}
          {Target dimension is smaller than natural width}
          {Giving up on this. Make your table smaller.}%
      \fi
      \tabscaler@trialrun\p@
      \tabscaler@loops=\z@
      \loop\ifdim\tabscaler@this=\tabscaler@last
        \advance\tabscaler@loops\@ne
        \typeout{tabscaler preloop: \the\tabscaler@loops}%
        \tabscaler@trialrun{10\tabscaler@tcs@this}%
      \repeat
      \tabscaler@loop
      \expandafter
    \endgroup
    \expandafter
    \begingroup
      \expandafter\tabcolsep\the\tabcolsep\relax
      \input{#2}%
    \endgroup
  }
\makeatother

\begin{document}
\begin{table}[!tbp]
\begin{center}
\caption{}
\captionsetup{labelformat=empty,justification=justified,singlelinecheck=off, width=0.9\linewidth}

\label{table_firststage}

\begin{subtable}{\textwidth}
{\scriptsize \setlength{\tabcolsep}{6pt}\tabscaler{\jobname1}\par}
\end{subtable}
\begin{subtable}{\textwidth}
{\scriptsize \setlength{\tabcolsep}{8.5pt}   \vspace{2mm}
\tabscaler{\jobname2}}
\end{subtable}
\begin{subtable}{\textwidth}
{\scriptsize \setlength{\tabcolsep}{8pt}   \vspace{2mm}
\tabscaler{\jobname3}}
\end{subtable}

\captionsetup{labelformat=empty,justification=raggedright,singlelinecheck=off, width=0.9\linewidth} \vspace{-2mm}
\begin{subtable}{\textwidth}
 \scriptsize \raggedright \hspace{22pt}
\tabscaler{\jobname4}
\end{subtable}
\end{center}
\normalsize
\begin{minipage}{\textwidth}
    {\footnotesize \textbf{Notes:} }
\end{minipage}
\end{table}
\end{document}

在此处输入图片描述

相关内容