在表格列中换行文本

在表格列中换行文本

我有一张表格,我刚刚用在线工具创建了它,但我想让列换行。所以我只需更改\begin{tabular}\begin{tabularx},然后最后一列的垂直线距离表格约 1 厘米...我该如何修复这个间隙?

\begin{tabularx}{\textwidth}{|X|X|X|X|}
\hline
Name:                                      & Donald Trump                              & Student ID:                                         & 99999999                                     \\ \hline
\multicolumn{2}{|p{0.5\textwidth}|}{Feedback focus:}                                                 & \multicolumn{2}{p{0.5\textwidth}|}{asoidjfaopsidjfp oiajdfpo iasjdfp oiasjdfpo ij}                                \\ \hline
\multicolumn{2}{|p{0.5\textwidth}|}{askdljf ;laksjdf;l aksjdf;lkajsd;:}                          & \multicolumn{2}{l|}{as;ldifja;lsdkfja;lsdkfja; slkfj; aldkjf ;aslkdj ;alk}                         \\ \hline \hline
\multicolumn{4}{|p{\textwidth}|}{as;ldkf;lkj: aklsduhf asldkj f;alskdjf ;alskdjf ;alskdf }                                                                  \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{asldkjf;alksdj f;laksjdf:as;dlk ja;lsdk fjas;ldkfj a;slkdfj ;lakasdlk;jkdj }                                                                               \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{a;lsdkjf; lakj;l3: alkjdsf l;aksjdf;l kasjd;lf kjasd;l fkjas;ldkfjas;ldkf jas;ldkf ja;sldkfj asldkfj alksjferopijp34qjp asdf} \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{as;ldkfjal;kej ;l4: alskdjf ;lasdkjf ;alskdjf ;laskd}                                                                                                                      \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{asl;dkjf;lk4j: asl;dkjfaslkdjf eirkajp (asl;dkfj a) aslkdjf l}                                                                                             \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\end{tabularx}

给出:

给出这个

答案1

(重写了部分答案以纳入@Zarko 的评论和意见)

您的列宽计算没有考虑到需要为 (a) 列周围的空白填充和 (b) 垂直线的宽度留出空间。您需要

  • 替换所有实例

    \multicolumn{2}{|p{0.5\textwidth}|}
    

    \multicolumn{2}{|P\lengtha}|}
    
  • 并替换所有实例

    \multicolumn{4}{|p{\textwidth}|}
    

    \multicolumn{4}{|P{\lengthb}|}
    

其中\lengtha\lengthb在下面的代码中定义。P通过指令定义

\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}

\RaggedRight软件包提供的说明用于ragged2e以右对齐模式而不是完全对齐模式排版材料,同时仍允许连字符。(由于列很窄,因此很难令人满意地完全对齐列文本。最好立即转到右对齐模式。)

此外,如下指令

\multicolumn{2}{l|}{as;ldifja;lsdkfja;lsdkfja; slkfj; aldkjf ;aslkdj ;alk}

由于列类型不允许自动换行,因此会严重破坏表格的外观l。您要么在那些不允许换行的列中只使用短字符串,要么l从一开始就不要使用列类型。我的建议是将所有 替换\multicolumn{2}{l|}\multicolumn{2}{P{\lengtha}|}

关于代码的补充说明:我还定义了X列类型的一个变体,称为Y,它以右对齐模式而不是完全对齐模式排版其材料。Y列类型用于主列定义。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,ragged2e} 
% "\RaggedRight" allows hyphenation
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X} 

\newlength\lengthx % basic column width
\setlength{\lengthx}{\dimexpr0.25\textwidth-2\tabcolsep-1.25\arrayrulewidth\relax}
\newlength\lengtha % width of two-columm multicolumn
\setlength{\lengtha}{\dimexpr2\lengthx+2\tabcolsep+1\arrayrulewidth\relax}
\newlength\lengthb % width of four-columm multicolumn
\setlength{\lengthb}{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth\relax}

\begin{document}
\noindent % <--- this is necessary
\begingroup
\setlength\extrarowheight{2pt} % optional: go for a more open "look"
\begin{tabularx}{\textwidth}{|Y|Y|Y|Y|}
\hline
Name: & XYZ & Student ID: & 99999999 
\\ \hline
\multicolumn{2}{|P{\lengtha}|}{Feedback focus:} &
\multicolumn{2}{ P{\lengtha}|}{asoidjfaopsidjfp oiajdfpo iasjdfp oiasjdfpo ij} 
\\ \hline
\multicolumn{2}{|P{\lengtha}|}{askdljf ;laksjdf;l aksjdf;lkajsd;:} & 
\multicolumn{2}{ P{\lengtha}|}{as;ldifja;lsdkfja;lsdkfja; slkfj; aldkjf ;aslkdj ;alk}
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{as;ldkf;lkj: aklsduhf asl dkj f;als kdjf ;als kdjf ;alskdf } 
\\ \hline
Fully Met & Met & Partially Met & Not Met 
\\ \hline
Comments: & \multicolumn{3}{l|}{} 
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{asldkjf;alksdj f;lak sjdf:as;dlk ja;lsdk fjas;ldkfj a;s lkdfj ;lak asdlk;jkdj }            
\\ \hline
Fully Met & Met & Partially Met & Not Met 
\\ \hline
Comments: & \multicolumn{3}{l|}{}     
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{a;lsdkjf; lakj;l3: alkjdsf l;ak sjdf;l ka sjd;lf kjasd;l fkjas;ld kfjas; ldkf jas ;ldkf ja;s ldkfj asl dkfj alk sjfe ropij p34qjp asdf} 
\\ \hline
Fully Met & Met & Partially Met & Not Met
\\ \hline
Comments: & \multicolumn{3}{l|}{} 
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{as;ldkfjal;kej ;l4: alskdjf ;lasdkjf ;alskdjf ;laskd} 
\\ \hline
Fully Met & Met & Partially Met & Not Met
\\ \hline
Comments: & \multicolumn{3}{l|}{}
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{asl;dkjf;lk4j: asl;dkjfaslkdjf eirkajp (asl;dkfj a) aslkdjf l}
\\ \hline
Fully Met & Met & Partially Met & Not Met 
\\ \hline
Comments: & \multicolumn{3}{l|}{}
\\ \hline
\end{tabularx}
\endgroup
\end{document}

相关内容