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 :(

2 Answers2

7

(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}
Mico
  • 506,678
  • \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 '19 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 '19 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 '19 at 03: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 :)) –  May 15 '19 at 03:14
  • 3
    how you know this? apparently you have crystal ball :-). mine is out of order :-(. – Zarko May 15 '19 at 03:17
  • @Mico Yes forgot that. Thanks –  May 15 '19 at 03: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"? –  May 15 '19 at 03:21
  • Thanks, but I can't do manual breaking, as long as I generate latex document for automatic invoice. – Georgy Farniev May 15 '19 at 05:17
  • @GeorgyFarniev I can't consider \- as time-consuming. Just put a single \-. I don't believe your "word" goes to three lines. –  May 15 '19 at 05:18
  • @JouelV, again, I do not create document manually, I generate it from input data, let's say, received by API. seqsplit package solved my problem. – Georgy Farniev May 15 '19 at 05:22
  • 1
    @GeorgyFarniev Alright no problem –  May 15 '19 at 05:23