在大型 tabularx 环境中,列大小无法正确调整

在大型 tabularx 环境中,列大小无法正确调整

我正在构建一个大表格(30x37,尽管我只会在 mwe 中包含几行),并努力使列间距适当。

到目前为止,虽然如果间距适当,文本足够小,可以适合页面,但列本身的尺寸小于文本和水平空间(如图所示)。出于某种原因,当宽度设置较低(尝试 5in)时,水平线不会填满页面,但列间距更合适。无论我调整第 3 列的大小(如图所示)还是仅使用 Y 列(定义为 X 列,文本居中),这种情况都会持续存在,如果我将所有 Y 列更改为 X 列,这种情况也会持续存在。无论我将宽度定义为 \textwidth、\hsize、\linewidth 还是 10.5in,似乎都不会改变它。

我做错了什么?

\documentclass[landscape]{article}
\usepackage[letterpaper,margin=.25in]{geometry}

\usepackage{amsmath,amssymb,amsthm,multicol,tabularx}

%center columns = Y
\newcolumntype{Y}{>{\centering\arraybackslash}X} 
%multi-columns that are left-aligned
\newcommand{\mc}[2]{\multicolumn{#1}{l}{#2}} %e.g., \mc{4}{VAP}
%multi-rows that are centered
\newcommand{\mr}[2]{\multicolumn{#1}{*}{#2}} %e.g., \mr{5}{BPI}

\setlength{\parindent}{0em}

\begin{document}

    \begin{tiny}
\begin{tabularx}{\linewidth}{*{2}{|Y}|p{5em}|*{34}{Y|}} %{|*{37}{Y|}}
 &  &  & \mc{4}{Era 1}       & \mc{12}{Era 2}                       & \mc{14}{Era 3}                           & \mc{4}{Era 4}       \\ 
 &  &  & F06 & S07 & F07 & S08 & F08 & S09 & F09 & S10 & F10 & S11 & F11 & S12 & F12 & S13 & F13 & S14 & F14 & S15 & F15 & S16 & F16 & S17 & F17 & S18 & F18 & S19 & F19 & S20 & F20 & S21 & F21 & S22 & F22 & S23 \\ \hline
 Loc    &   A123    &   Long name   &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &   x   &       &       &       &       &       &       &       \\  \hline
\end{tabularx}
    \end{tiny}

\end{document}

上面的代码产生了这个

答案1

我不认为你想要tabularx这里,因为这里没有换行符

在此处输入图片描述

\documentclass[landscape]{article}
\usepackage[letterpaper,margin=.25in]{geometry}

\usepackage{amsmath,amssymb,amsthm,multicol,tabularx}

%center columns = Y
\newcolumntype{Y}{>{\centering\arraybackslash}X} 
%multi-columns that are left-aligned
\newcommand{\mc}[2]{\multicolumn{#1}{l}{#2}} %e.g., \mc{4}{VAP}
%multi-rows that are centered??? you used \multicolumn not row ???
\newcommand{\mr}[2]{\multicolumn{#1}{c}{#2}} %e.g., \mr{5}{BPI}

\setlength{\parindent}{0em}

\begin{document}

X\dotfill X

\setlength\tabcolsep{3pt}
    \begin{tiny}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}*{2}{|c}|l|*{34}{c|}@{}} %{|*{37}{Y|}}
 &  &  & \mc{4}{Era 1}       & \mc{12}{Era 2}                       & \mc{14}{Era 3}                           & \mc{4}{Era 4}       \\ 
 &  &  & F06 & S07 & F07 & S08 & F08 & S09 & F09 & S10 & F10 & S11 & F11 & S12 & F12 & S13 & F13 & S14 & F14 & S15 & F15 & S16 & F16 & S17 & F17 & S18 & F18 & S19 & F19 & S20 & F20 & S21 & F21 & S22 & F22 & S23 \\ \hline
 Loc    &   A123    &   Long name   &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &   x   &       &       &       &       &       &       &       \\  \hline
\end{tabular*}
    \end{tiny}

\end{document}

答案2

使用该包的替代方法tabularray

\documentclass[landscape]{article}
\usepackage[letterpaper,margin=.25in]{geometry}


\usepackage{tabularray}

\begin{document}

\noindent\begin{tblr}{
cells={font=\tiny},
colspec={XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX},
columns={c},
column{3}={l},
colsep={1pt},
vlines
}
 &  &  & \SetCell[c=4]{c} Era 1    &&&  & \SetCell[c=12]{c} Era 2 &&&&&&&&&&&                      & \SetCell[c=14]{c} Era 3 &&&&&&&&&&&&&                          & \SetCell[c=4]{c} Era 4 &&&       \\ 
 &  &  & F06 & S07 & F07 & S08 & F08 & S09 & F09 & S10 & F10 & S11 & F11 & S12 & F12 & S13 & F13 & S14 & F14 & S15 & F15 & S16 & F16 & S17 & F17 & S18 & F18 & S19 & F19 & S20 & F20 & S21 & F21 & S22 & F22 & S23 \\ \hline
 Loc    &   A123    &   Long name   &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &       &   x   &       &       &       &       &       &       &       \\  \hline
\end{tblr}

\end{document}

答案3

让我们将我的评论扩展为一个答案:

%---------------------------------------------------------------%
%   fix for tabularx when it has more than 23 columns and page  %
%   layout is determined by the package geometry with option    %
%   margin=<width>.                                             %
%   fix provided me David Carlisle in chat 2018.07.25 at 0:43   %
%---------------------------------------------------------------%
\documentclass[landscape]{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage[margin=20mm]{geometry}
\usepackage{tabularx}
% fix -----------------------------------------------------------%
\makeatletter
\def\TX@endtabularx{%
   \expandafter\expandafter\expandafter
     \TX@find@endtabularxa\csname end\TX@\endcsname
     \endtabularx\TX@\endtabularx\TX@find@endtabularxa
  \expandafter\TX@newcol\expandafter{\tabularxcolumn{\TX@col@width}}%
  \let\verb\TX@verb
  \def\@elt##1{\global\value{##1}\the\value{##1}\relax}%
  \edef\TX@ckpt{\cl@@ckpt}%
  \let\@elt\relax
  \TX@old@table\maxdimen
  \TX@[email protected]\TX@target%<<<<< smaller initial guess for big table
  \global\TX@cols\@ne
  \TX@typeout@
    {\@spaces Table Width\@spaces Column Width\@spaces X Columns}%
  \TX@trial{\def\NC@rewrite@X{%
          \global\advance\TX@cols\@ne\NC@find p{\TX@col@width}}}%
  \loop
    \TX@arith
    \ifTX@
    \TX@trial{}%
  \repeat
  {\let\@footnotetext\TX@ftntext\let\@xfootnotenext\TX@xftntext
    \csname tabular*\expandafter\endcsname\expandafter\TX@target
      \the\toks@
    \csname endtabular*\endcsname}%
  \global\TX@ftn\expandafter{\expandafter}\the\TX@ftn
  \ifnum0=`{\fi}%
   \expandafter\expandafter\expandafter
   \TX@find@endtabularxbb
    \expandafter\end\expandafter{\TX@}%
    \endtabularx\TX@\endtabularx\TX@find@endtabularxb
}
% end fix -------------------------------------------------------%
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcommand{\mc}[2]{\multicolumn{#1}{l}{#2}}


\begin{document}
\begingroup
\tiny
\setlength\tabcolsep{3pt}
\centering
\begin{tabularx}{\linewidth}{*{2}{|Y}|p{5em}|*{34}{Y|}} %{|*{37}{Y|}}
    &   &   & \mc{4}{Era 1} & \mc{12}{Era 2}                       & \mc{14}{Era 3}                           & \mc{4}{Era 4}       \\
    &     &     & F06 & S07 & F07 & S08 & F08 & S09 & F09 
    & S10 & F10 & S11 & F11 & S12 & F12 & S13 & F13 & S14 
    & F14 & S15 & F15 & S16 & F16 & S17 & F17 & S18 & F18 
    & S19 & F19 & S20 & F20 & S21 & F21 & S22 & F22 & S23 \\ 
    \hline
Loc &A123 & Long name 
                &     &     &     &     &     &     & 10      
    &     &     &     &     &     &     &     &     &   & 20     
    &     &     &     &     &     &     &     &     &       
    &     & x   &     &     &     &     &     &            \\  
    \hline
\end{tabularx} 
\endgroup
\end{document}

在此处输入图片描述

相关内容