如何使用 tabularx 对齐列?

如何使用 tabularx 对齐列?

图像中显示的表格有 24 列,文本不适合单元格。而且,列未对齐,也没有沿表格的宽度分布。

在此处输入图片描述

错误图像

在此处输入图片描述

这是代码和错误

\documentclass[12pt]{article}
% Pictures & Labels
%%\usepackage[capitalise]{cleveref}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{ragged2e}
\usepackage{rotating}
\usepackage{adjustbox} %%este es para hacer ajustes automáticos de tablas
\usepackage{longtable} % tablas grandes 
\usepackage{float}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{makecell}

% Paper Style
\usepackage[letterpaper,centering]{geometry} %%paquete para cambiar diseño de pagina
\usepackage[title,toc,page]{appendix}
%%\usepackage{lipsum}
\makeatletter
% like \newgeometry, but also allows change of landscape/portrait and paper size
% to be used with caution!
\newcommand{\newgeometryfull}[1]{%
  \clearpage
  \Gm@restore@org
  \Gm@initnewgm
%  \Gm@newgmtrue
  \setkeys{Gm}{#1}%
%  \Gm@newgmfalse
  \Gm@process
  \ifnum\mag=\@m\else\Gm@magtooffset\fi
  \Gm@changelayout
  \Gm@showparams{newgeometry}}%
\makeatother

%% Allow A3 sheets - - establecemos las características del A3
\newenvironment{a3mypage}{%
    \newgeometryfull{paperwidth=431.8mm,paperheight=279.4mm,centering,hmargin=31.75mm,top=25.4 mm,bottom=25.4 mm}
    % set the correct dimension for the PDF viewer
    \pdfpageheight=\paperheight
    \pdfpagewidth=\paperwidth}
{  \restoregeometry
    % set the correct dimension for the PDF viewer
    \pdfpageheight=\paperheight
    \pdfpagewidth=\paperwidth} %termina la edicion de la hoja A3
    
\renewcommand{\theadfont}{\bfseries\color{blue}}


\begin{document}
\begin{a3mypage}
\section{Machine selecction}
{
%\small
\begin{tabularx}{\linewidth}{m{0.172\linewidth}*{23}{X}} %%tabla
\rowcolor[rgb]{0.808,0.576,0.847} \multicolumn{1}{l}{\textbf{\textcolor[rgb]{0,0,0.545}{\large TITULO:}}} & \multicolumn{23}{>{\hsize=\dimexpr23\hsize+141\tabcolsep}X}{\textbf{\textcolor[rgb]{0,0,0.545}{\large DISEÑO DE UN DESHIDRATADOR TEXTURIZADOR AL VACÍO PARA DISMINUIR EL TIEMPO DE DESHIDRATADO CONSERVANDO LAS PROPIEDADES DEL AÇAÍ}}} \\
\multirow{2}{=}{Criterios de Diseño} & Peso & \multicolumn{2}{c}{Solar} & \multicolumn{2}{c}{Bandejas} & \multicolumn{2}{c}{Tunel} & \multicolumn{2}{c}{Rodillo o Tambor} & \multicolumn{2}{c}{Rotativo} & \multicolumn{2}{c}{Lecho Fluidizado} & \multicolumn{2}{c}{Spray} & \multicolumn{2}{c}{Banda o Faja} & \multicolumn{2}{c}{Vacío} & \multicolumn{2}{c}{DIC} & \multicolumn{2}{c}{Liofilizador} \\
  & W*(\%) & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U \\
  numeros y decimales & 1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23\\
\arrayrulecolor[rgb]{0.808,0.576,0.847}\hline
\end{tabularx}


}
\end{a3mypage}
\end{document}

答案1

  • 表中的列数tabularx限制为 20。如果您想要更多,则需要使用@David Carlisle 的修复(在下面的 MWE 中添加)
  • 我擅自对您的表格进行了一点重新设计:

在此处输入图片描述

梅威瑟:

\documentclass[12pt]{article}
% Pictures & Labels
%%\usepackage[capitalise]{cleveref}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage{ragged2e}
\usepackage{rotating}
\usepackage{adjustbox} %%este es para hacer ajustes automáticos de tablas
\usepackage{float}
\usepackage{booktabs, longtable, makecell, multirow, tabularx}
\renewcommand{\theadfont}{\bfseries\color{blue}}
 
\usepackage{xparse} % for case in used LaTEX system hasn't recent version of it
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}

\makeatletter % fix which once provide me @David Carlisle on chat
\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
}
\makeatother

% Paper Style
\usepackage[letterpaper,centering]{geometry} %%paquete para cambiar diseño de pagina
\usepackage[title,toc,page]{appendix}
%%\usepackage{lipsum}
\makeatletter
% like \newgeometry, but also allows change of landscape/portrait and paper size
% to be used with caution!
\newcommand{\newgeometryfull}[1]{%
  \clearpage
  \Gm@restore@org
  \Gm@initnewgm
%  \Gm@newgmtrue
  \setkeys{Gm}{#1}%
%  \Gm@newgmfalse
  \Gm@process
  \ifnum\mag=\@m\else\Gm@magtooffset\fi
  \Gm@changelayout
  \Gm@showparams{newgeometry}}%
\makeatother

%% Allow A3 sheets - - establecemos las características del A3
\newenvironment{a3mypage}{%
 \newgeometryfull{paperwidth=431.8mm,paperheight=279.4mm,centering,hmargin=31.75mm,top=25.4 mm,bottom=25.4 mm}
    % set the correct dimension for the PDF viewer
    \pdfpageheight=\paperheight
    \pdfpagewidth=\paperwidth}
{  \restoregeometry
    % set the correct dimension for the PDF viewer
    \pdfpageheight=\paperheight
    \pdfpagewidth=\paperwidth} %termina la edicion de la hoja A3


\begin{document}
\begin{a3mypage}
\section{Machine selecction}

\small
\begin{tabularx}{\linewidth}{m{0.12\linewidth}*{23}{X}} %%tabla
\rowcolor[rgb]{0.808,0.576,0.847} 
\multicolumn{1}{l}{\textbf{\textcolor[rgb]{0,0,0.545}{\large TITULO:}}} 
    & \multicolumn{23}{p{\dimexpr\linewidth-0.12\linewidth-4\tabcolsep}}{
        \large\bfseries\textcolor[rgb]{0,0,0.545} {DISEÑO DE UN DESHIDRATADOR TEXTURIZADOR AL VACÍO PARA DISMINUIR 
        EL TIEMPO DE DESHIDRATADO CONSERVANDO LAS PROPIEDADES DEL AÇAÍ}} \\
\multirow{2}{=}{Criterios de Diseño} 
    & Peso  & \mcc[2]{Solar}    & \mcc[2]{Bandejas}         & \mcc[2]{Tunel}  & \mcc[2]{Rodillo o Tambor} 
            & \mcc[2]{Rotativo} & \mcc[2]{Lecho Fluidizado} & \mcc[2]{Spray} & \mcc[2]{Banda o Faja} 
            & \mcc[2]{Vacío}    & \mcc[2]{DIC}              & \mcc[2]{Liofilizador} \\
    \cmidrule(l{1pt}r{1pt}){2-2}    
    \cmidrule(l{1pt}r{1pt}){3-4}\cmidrule(l{1pt}r{1pt}){5-6}\cmidrule(l{1pt}r{1pt}){7-8}\cmidrule(l{1pt}r{1pt}){9-10}
    \cmidrule(l{1pt}r{1pt}){11-12}\cmidrule(l{1pt}r{1pt}){13-14}\cmidrule(l{1pt}r{1pt}){15-16}\cmidrule(l{1pt}r{1pt}){17-18}
    \cmidrule(l{1pt}r{1pt}){19-20}\cmidrule(l{1pt}r{1pt}){21-22}\cmidrule(l{1pt}r{1pt}){23-24}
    & W*(\%) & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U \\
  numeros y decimales & 1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23\\
    \arrayrulecolor[rgb]{0.808,0.576,0.847}
    \bottomrule%\hline
\end{tabularx}
\end{a3mypage}
\end{document}

附录:

在表格重新设计中我做了以下工作:

  • 将新命令定义\mcc为的缩写multicolumn{<...>}{c}{<...>}
  • 在表格中添加了\cmidrule(l1pt}{r{1pt}){...}表示具有公共标题的列对。
  • 删除包colortbl并改为table在加载包时引入选项xcolor,即现在加载为\usepackage[table]{xcolor}
  • xparse当您的 LaTeX 系统 (Overleaf) 不包含 LaTeX 的最新更改时,在序言中也加载包。这样就可以\mcc在旧版 LaTeX 中使用定义,而不会损害其新版本。
  • 提供的代码经过 MiKTeX 和 Overleaf 测试。

经过此更改并添加对表中列数的修复后,您的文档可以正常编译,没有任何警告或错误。

得到的结果仍然可以稍微改进。有些列比其他列宽,因为它们的标题比定义的X列长。所以你可能会喜欢下面的解决方案,与第一个解决方案相比,它有以下变化:

  • 较长的(多字)列标题分为两行
  • 单元格内容通过使用列类型居中C,其定义为\newcolumntype{C}{>{\centering\arraybackslash}X}
  • 之间的分离\cmidrule增加了,2pt现在更加明显
  • 表格中使用正常字体大小
\documentclass[12pt]{article}
% Pictures & Labels
%%\usepackage[capitalise]{cleveref}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage{ragged2e}
\usepackage{rotating}
\usepackage{adjustbox} %%este es para hacer ajustes automáticos de tablas
\usepackage{float}
\usepackage{booktabs, longtable, makecell, multirow, tabularx}
\renewcommand{\theadfont}{\bfseries\color{blue}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}

\makeatletter % fix which once provide me @David Carlisle on chat
\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
}
\makeatother

% Paper Style
\usepackage[letterpaper,centering]{geometry} %%paquete para cambiar diseño de pagina
\usepackage{pdflscape}

\usepackage[title,toc,page]{appendix}

\usepackage{lipsum}

\makeatletter
% like \newgeometry, but also allows change of landscape/portrait and paper size
% to be used with caution!
\newcommand{\newgeometryfull}[1]{%
  \clearpage
  \Gm@restore@org
  \Gm@initnewgm
%  \Gm@newgmtrue
  \setkeys{Gm}{#1}%
%  \Gm@newgmfalse
  \Gm@process
  \ifnum\mag=\@m\else\Gm@magtooffset\fi
  \Gm@changelayout
  \Gm@showparams{newgeometry}}%
\makeatother

%% Allow A3 sheets - - establecemos las características del A3
\newenvironment{a3mypage}{%
    \newgeometryfull{paperwidth=431.8mm,paperheight=279.4mm,centering,hmargin=31.75mm,top=25.4 mm,bottom=25.4 mm}
    % set the correct dimension for the PDF viewer
    \pdfpageheight=\paperheight
    \pdfpagewidth=\paperwidth}
    {  
    \restoregeometry
    % set the correct dimension for the PDF viewer
    \pdfpageheight=\paperheight
    \pdfpagewidth=\paperwidth} %termina la edicion de la hoja A3


\begin{document}
\lipsum

\begin{a3mypage}
\section{Machine selecction}

\begin{tabularx}{\linewidth}{m{0.1\linewidth}*{23}{C}} %%tabla
\rowcolor[rgb]{0.808,0.576,0.847} 
\multicolumn{1}{l}{\textbf{\textcolor[rgb]{0,0,0.545}{\large TITULO:}}} 
    & \multicolumn{23}{p{\dimexpr\linewidth-0.1\linewidth-4\tabcolsep}}{
        \large\bfseries\textcolor[rgb]{0,0,0.545} {DISEÑO DE UN DESHIDRATADOR TEXTURIZADOR AL VACÍO PARA DISMINUIR 
        EL TIEMPO DE DESHIDRATADO CONSERVANDO LAS PROPIEDADES DEL AÇAÍ}} \\
\multirow{2}{=}{Criterios de Diseño} 
    & Peso  & \mcc[2]{Solar}    & \mcc[2]{Bandejas} & \mcc[2]{Tunel}  & \mcc[2]{\makecell{Rodillo\\ o Tambor}} 
            & \mcc[2]{Rotativo} & \mcc[2]{\makecell{Lecho\\ Fluidizado}} 
                                                    & \mcc[2]{Spray} & \mcc[2]{\makecell{Banda\\ o Faja}} 
            & \mcc[2]{Vacío}    & \mcc[2]{DIC}              & \mcc[2]{Liofilizador} \\
    \cmidrule(l{2pt}r{2pt}){2-2}
    \cmidrule(l{2pt}r{2pt}){3-4}\cmidrule(l{2pt}r{2pt}){5-6}\cmidrule(l{2pt}r{2pt}){7-8}\cmidrule(l{2pt}r{2pt}){9-10}
    \cmidrule(l{2pt}r{2pt}){11-12}\cmidrule(l{2pt}r{2pt}){13-14}\cmidrule(l{2pt}r{2pt}){15-16}\cmidrule(l{2pt}r{2pt}){17-18}
    \cmidrule(l{2pt}r{2pt}){19-20}\cmidrule(l{2pt}r{2pt}){21-22}\cmidrule(l{2pt}r{2pt}){23-24}
    & W*(\%) & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U & S & U \\
  numeros y decimales & 1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23\\
    \arrayrulecolor[rgb]{0.808,0.576,0.847}
    \bottomrule%\hline
\end{tabularx}
\end{a3mypage}
\end{document}

产生

在此处输入图片描述

相关内容