0

I have a table created with \begin{tabu} whose columns contain lengthy strings that it won't wrap because they are not English text, they are actually programming language identifiers. How can I enable them to break on additional characters other than whitespace and hyphen (such as colons or underscores), or even at any character (if none of those additional characters are present).

A demonstration of my problem:

\documentclass{article}
\usepackage{tabu}
\begin{document}
\begin{tabu}{|X[l]|X[l]|X[l]|}
\hline
ThisIsAVeryLongStringWhichINeedToBreakAcrossMultipleLines & Yet:Another:Very:Long:String:Which:Must:Be:Broken:Up & break\_up\_this\_string\_too\_please \\
\hline
\end{tabu}
\end{document}
4
  • see for example tex.stackexchange.com/questions/66593/… Commented Nov 30, 2015 at 11:19
  • @DavidCarlisle, I tried using the \zzz{ defined in that question (I wrapped each cells contents in it), but it doesn't make any change to the line wrapping inside \begin{tabu}. I also tried \protect\zzz{ but that doesn't do anything for me either Commented Nov 30, 2015 at 21:34
  • @DavidCarlisle, I found another answer of yours that appears to do what I want, thanks tex.stackexchange.com/a/208989/54565 Commented Dec 5, 2015 at 21:53
  • Oh OK, we should close as duplicate then, I guess. glad to help:-) Commented Dec 5, 2015 at 21:56

1 Answer 1

0

I found a solution on Stack Overflow:

\makeatletter
\def\hyphenatestring#1{\xHyphen@te#1$\unskip}
\def\xHyphen@te{\@ifnextchar${\@gobble}{\sw@p{\hskip 0pt plus 1pt\xHyphen@te}}}
\def\sw@p#1#2{#2#1}
\makeatother

Having defined the above, if I then wrap the text within the columns in \hyphenatestring{, it will hyphenate them at any character.

This is not quite what I wanted (my preference was to hyphenate at certain characters, e.g. :, and then hyphenate at any position only if that was not going to work). However, this will do for now.


UPDATE The above solution has a problem - it removes spaces from the string. I found a much better solution, which doesn't do that. It is David Carlisle's \foo macro he defines here

Not the answer you're looking for? Browse other questions tagged .