表格中很长的单词会自动换行

表格中很长的单词会自动换行

我正在尝试分解一个非常长的数字(长度为 128),以适合表格单元格,这样它就不会超出页面。

我查找了使用longtabu、 tabular with p{5cm}commend、\makecell\parbox{}\multicolumnwith 的方法p{5cm},但这些方法都不起作用。它们似乎只在“空格”处换行,而不会换行整个单词,在我的情况下是数字。

我知道可以使用\\或手动换行\newline,但从长远来看这似乎并不实用,因为我需要手动更改它,因为我的文档的格式可能会根据给我的要求而改变。

我现在唯一能想到的办法就是把长数字截成图,然后插入到表格中。但如果有更好的解决方案,我愿意使用它。

我用来longtabu尝试的代码

\begin{longtabu} to \textwidth {
    |X[1,c]
    |X[10,l]|}
    \hline
    \textbf{Head 1} & 
    \textbf{Head 2} 
    \\ \hline
    12:18:52  & this is a description for the event \\\hline
    12:18:52  & this is a description for the event \\\hline
    12:18:52  & this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event \\\hline
    12:18:52  & 11111111111111111111111111111111111111222222222222222222222222222222222222222223333333333333333333333333333333333344444444444444444444455555555555555555555555555555555666666666666666666666666666777777777777777777777778888888888888888\\\hline
\end{longtabu}

使用p{}

\begin{tabular}{|p{1cm}|p{5cm}|}
  This text will be wrapped & Some more text \\\hline
  this & 11111111111111111111111111111111111111222222222222222222222222222222222222222223333333333333333333333333333333333344444444444444444444455555555555555555555555555555555666666666666666666666666666777777777777777777777778888888888888888\\\hline
\end{tabular}

赫伯特溶液拆分表格中的长单词

答案1

您可以加载xurl包并将长数字装入\url{...}“包装器”中。

tabularx(由于我不熟悉,因此以下代码使用了环境longtabu。)

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,xurl}
\urlstyle{same}
\begin{document} 
\noindent
\begin{tabularx}{\textwidth}{@{} lX @{}}
\hline
\textbf{Head 1} & \textbf{Head 2} \\ 
\hline
12:18:52  & this is a description for the event \\
\hline
12:18:52  &
\url{11111111111111111111111111111111111111222222222222222222222222222222222222222223333333333333333333333333333333333344444444444444444444455555555555555555555555555555555666666666666666666666666666777777777777777777777778888888888888888}\\
\hline
\end{tabularx}
\end{document}

答案2

另一个解决方案是 seqsplit,它结合了和xltabular的功能,并定义表格中单元格的一些垂直填充:longtabletabularxmakecell

\documentclass{article}
\usepackage{seqsplit, makecell, xltabular}

\begin{document}

{\setcellgapes[b]{3pt}
\setcellgapes[t]{2pt}\makegapedcells
\noindent\begin{xltabular}{\textwidth}{|c|X|}%|
    \hline
    \textbf{Head 1} &
    \textbf{Head 2}
    \\ \hline
    12:18:52 & this is a description for the event \\\hline
    12:18:52 & this is a description for the event \\\hline
    12:18:52 & this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event this is a description for the event \\\hline
    12:18:52 & \seqsplit{ 111111111111111111111111111111111111112222222222222222222222222222222222222222233333333333333333333333333333333333444444444444444444444%
    55555555555555555555555555555555666666666666666666666666666777777777777777777777778888888888888888} \\\hline
\end{xltabular}}

\end{document} 

在此处输入图片描述

答案3

问题在于,数字不在 tex 用来确定单词可能的断点的单词列表中,但可以通过\-在每个数字可以跨行断点处放置它们来手动添加它们:

\documentclass{article}

\usepackage[english]{babel}

\begin{document}

\begin{tabular}{|p{1cm}|p{5cm}|}
  This text will be wrapped & Some more text \\\hline
  this & 1\-111\-111\-1111\-111\-111\-11\-111\-111\-111\-111\-111\-111\-122\-22\-222\-22\-222\-22\-22\-222\-22\-22\\\hline
\end{tabular}

\end{document}

相关内容