我正在尝试创建一个类似的表格:
_______________________________________________________
Dataset name | Example of text |
______________________________________________________|
DatasetName1 |This is an example and I like t|
|hat text goes newline and word |
|splitting is forced http://exam|
|ple/newlinesandwords.com |
______________________________________________________|
DatasetName2 |yesIliketowritelotofthingswithn|
|ospacesatall |
... and so on
垂直线只是为了方便可视化,我更喜欢没有垂直线的表格。我还喜欢在第二列中使用等宽字符,为此我使用了 texttt。我尝试过以下代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\title{PROVA}
\begin{document}
\begin{table}[h]
\centering
\caption{Datasets}
\label{my-label}
\begin{tabular}{l | p{0.4\textwidth} |}
\toprule
Dataset name & Example of Text \\ \midrule
DatasetName1 & \texttt{This is an example and I like that text goes newline and word splitting is forced http://example/newlinesandwords.com} \\ \midrule
DatasetName2 & \texttt{yesIliketowritelotofthingswithnospacesatall} \\ \midrule
\end{tabular}
\end{table}
\end{document}
但是长单词只是忽略表格边界并出去(这是获得的行为的一个例子):
Dataset name | Example of text |
______________________________________________________|
DatasetName1 |This is an example and I like |
|that text goes newline and word|
|splitting is forced |
|http://example/newlinesandwords.com
______________________________________________________|
DatasetName2 |yesIliketowritelotofthingswithnospacesatall |
如何为这些表格单元格启用无连字符的断字功能?任何提示都值得赞赏。问候
答案1
TeX 不会在单词内换行,除非在连字符处。因此,您必须在每两个字符之间插入一个可能的断点。使用宏来做到这一点并不容易。但是,该soul
包有一个(内部)宏可以做到这一点,并用于其命令,如\so
和\ul
。因此,我们可以使用它来构建一个命令来插入换行点。
\usepackage{soul}
\makeatletter
\newcommand\breakwords{%
\SOUL@setup
\def\SOUL@everyexhyphen##1{##1\linebreak[1]}%
\def\SOUL@everytoken{\the\SOUL@token\linebreak[1]}%
\SOUL@}
\makeatother
\begin{tabular}{l p{0.4\textwidth}}
\toprule
Dataset name & Example of Text \\ \midrule
DatasetName1 & \texttt{\breakwords{This is an example and I like that text goes newline and word splitting is forced http://example/newlinesandwords.com}} \\ \midrule
DatasetName2 & \texttt{\breakwords{yesIliketowritelotofthingswithnospacesatall}} \\ \midrule
\end{tabular}