3

How to break VERY long word in latex? For example:

\begin{center}
\begin{longtable}{|p{8cm}|p{3cm}|p{3cm}|p{3cm}|}
\hline
1 & 2 & 3 \\ \hline
11111111111111111111111111111111111111111111111111111111111 & 2  & 3 \\ \hline
\end{longtable}
\end{center}

11111111111111111111111111111111111111111111111111111111111 overlaps next table cells in it's row. Thanks in advance.

UPDATE: Turns out that seqsplit doesn't work for me, as it removes existing spaces between words. I forgot to mention that text may contain space and I didn't notice that it eliminates spaces :(

  • welcome to tex.se! you should use babel for used language. if word hasn't defined hyphenation, you should insert them manually (with \-). as you show in code fragment, you haven't real word, so the meaning of series of digits with this can change its meaning. be careful with hyphenation in such a cases. – Zarko May 15 at 3:10
  • 4
    This isn't exactly the same situation but the long string that needs to be broken isn't a word: How to break long words after n chars (long genomic sequences) – barbara beeton May 15 at 3:21
  • @barbarabeeton - Many thanks for mentioning the seqsplit appoach; I've updated my answer to incorporate your suggestion. – Mico May 15 at 4:37
6

(updated the answer to mention the seqsplit approach)

I have two suggestions:

  • load the xurl package and encase the very long "word" in an \url directive, and

  • load the seqsplit package and encase the very long "word" in a \seqsplit directive.

enter image description here

It looks like the \seqsplit approach manages to pack its contents a tad more tightly than the \url approach does.

In the following code, I replaced longtable with tabular since the example isn't really specific to the longtable package and its machinery.

\documentclass{article}
\usepackage{xurl,seqsplit,array}
\setlength{\extrarowheight}{3pt} % optional
\begin{document}
\begin{tabular}{|p{8cm}|c|c|}
\hline
A & B & C \\ 
\hline
\urlstyle{same}% default is '\urlstyle{tt}'
\url{11111111111111111111111111111111111111111111111111111111111} & 2  & 3 \\ 
\seqsplit{11111111111111111111111111111111111111111111111111111111111}
& 2  & 3 \\ 
\hline
\end{tabular}
\end{document}
  • \seqsplit will most likely use the main font, while \url may use a monospace font; that could account for the difference in packing density. – barbara beeton May 15 at 15:43
  • @barbarabeeton - Thanks. My answer made sure to include the directive \urlstyle{same}; the font *should" be the same in both cells. – Mico May 15 at 15:46
3

One can do manual breaking.

\documentclass{article}
\usepackage{longtable}
\begin{document}
\begin{longtable}{|p{8cm}|p{3cm}|p{3cm}|p{3cm}|}
\hline
1 & 2 & 3 \\ \hline
111111111111111111111111111111111111111111111\-11111111111111 & 2  & 3 \\ \hline
\end{longtable}
\end{document}

enter image description here

  • also with use of tabular can be to wide (OP not provide any information about its document layout)... use of the longtable has sense, if the table can not be placed on one page ... OP question is not very clear :-( – Zarko May 15 at 3:13
  • @Zarko Yes, longtable or tabular can't fix the too-wide issue. But this particular table is not long enough (but I know the main table the OP is working on is longer :)) – user156344 May 15 at 3:14
  • 3
    how you know this? apparently you have crystal ball :-). mine is out of order :-(. – Zarko May 15 at 3:17
  • 1
    @Zarko Hmm I have none. I just take the OP's code and put \- to a random position in the 1111111111111111... Or am I misunderstanding the word "crystal ball"? – user156344 May 15 at 3:21
  • 1
    @GeorgyFarniev Alright no problem – user156344 May 15 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.