如何将表格中的所有单元格对齐到 \RaggedRight

如何将表格中的所有单元格对齐到 \RaggedRight

我使用以下xltabular包在 LaTeX 中创建了此表:

\begin{xltabular}{\linewidth}{X X X X X X X}
\toprule
Activity & \multicolumn{2}{X}{1. Feb 10, 2022} & \multicolumn{2}{X}{2. April 26, 2022} & \multicolumn{2}{X}{3. June 6, 2022}\\
\cmidrule(lr){2-3}
\cmidrule(lr){4-5}
\cmidrule(lr){6-7}
& Score & Goal & Score & Goal & Score & Goal\\
\midrule\endfirsthead
\toprule
Activity & \multicolumn{2}{X}{1.} & \multicolumn{2}{X}{2. April 26, 2022} & \multicolumn{2}{X}{3. June 6, 2022}\\
\midrule\endhead
Push-ups & 20\newline Level 1 & 25\newline Level 2 & 29\newline Level 2 & 40\newline Level 4 & 30\newline Level 3 & 40\newline Level 4\\
\addlinespace
Crunches & 24\newline Level 1 & 30\newline Level 2 & 40\newline Level 2 & 30\newline Level 3 & 26\newline Level 3 & 40\newline Level 3\\
\addlinespace
Plank & 1:00\newline Level 1 & 1:30\newline Level 1 & 1:13\newline Level 1 & 1:30\newline Level 2 & 2:01\newline Level 3 & 3:00\newline Level 3\\
\addlinespace
Vertical jump & 40 & 50 & 36 & 50 & 40 & 50\\
\addlinespace
Sit, reach, and hold & 16 & 17 & 16 & 17 & 17 & 18\\
\bottomrule
\end{xltabular}

默认情况下,所有单元格中的文本都是对齐的。我想从ragged2e包中将其更改为 \RaggedRight。我该怎么做?

答案1

您可以修改X列类型,以便 (a) 将其内容排版为右侧不规则,并且 (b) 允许可变宽度。(在下表中,第一列比其余六列宽约 18%。)

请注意,您的代码中有一个疏忽:而不是

\multicolumn{2}{X}{1. Feb 10, 2022}

它应该是

\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep\relax}X}{1. Feb 10, 2022}`

其它指令也一样。实际上,还有一个更简单的解决方案:由于标题文本行足够短,\multicolumn因此可以使用列类型。l


在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{xltabular,ragged2e,booktabs}
\newcolumntype{L}[1]{>{\RaggedRight\hsize=#1\hsize}X}

\begin{document}

\begin{xltabular}{\linewidth}{@{} L{1.15} *{6}{L{0.975}} @{}}

\toprule
Activity & \multicolumn{2}{l}{1. Feb 10, 2022} 
         & \multicolumn{2}{l}{2. April 26, 2022} 
         & \multicolumn{2}{l}{3. June 6, 2022}\\
         \cmidrule(lr){2-3} 
         \cmidrule(lr){4-5} 
         \cmidrule(l){6-7}
         & Score & Goal 
         & Score & Goal 
         & Score & Goal \\
\midrule
\endhead

\bottomrule
\endfoot


Push-ups & 20\newline Level 1 & 25\newline Level 2 
         & 29\newline Level 2 & 40\newline Level 4 
         & 30\newline Level 3 & 40\newline Level 4 \\
\addlinespace
Crunches & 24\newline Level 1 & 30\newline Level 2 
         & 40\newline Level 2 & 30\newline Level 3 
         & 26\newline Level 3 & 40\newline Level 3 \\
\addlinespace
Plank    & 1:00\newline Level 1 & 1:30\newline Level 1 
         & 1:13\newline Level 1 & 1:30\newline Level 2 
         & 2:01\newline Level 3 & 3:00\newline Level 3 \\
\addlinespace
Vertical jump & 40 & 50 
              & 36 & 50 
              & 40 & 50 \\
\addlinespace
Sit, reach, and hold 
              & 16 & 17 
              & 16 & 17 
              & 17 & 18 \\

\end{xltabular}

\end{document}

相关内容