4

I am trying to break a very very long number, 128 in length, to fit into a table cell so it won't go over the page.

I've looked up methods using longtabu, tabular with p{5cm} commend, \makecell, \parbox{}, \multicolumn with p{5cm}, but non of those works. It seems like they only line break at "space" but does not line break one whole word, in my case is number.

I know it is possible to manually break line with \\ or \newline, but this doesn't seem practical in the long-run as I need to manually change this as the format of my document may change depending on the requirements given to me.

The only way I have in mind now, is to screenshot the long numbers into a figure, and insert it into the table. But if there is a better solution, I would like to use it.

Code that I use to longtabu attempt

\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}

Using p{}

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

Herbert solution at Break long word in tabular

1
  • thanks for all of your answers, tried Mico and Bernard's they worked! Commented Feb 11, 2019 at 21:45

3 Answers 3

10

You could load the xurl package and encase the long number in a \url{...} "wrapper".

(The following code uses a tabularx environment since I'm not familiar with longtabu.)

enter image description here

\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}
3
  • Very nice (+1)!! Is it compatible with longtable? Because, I am not able to use this nice example in longtable environment :-/. Commented Feb 12, 2019 at 8:26
  • 1
    @Raaja - I can see no reason why the xurl package/\url macro setup can't be used in other settings. Since longtable doesn't allow the X column type (at least not directly), one would need to replace the X column with a p column with a suitably chosen width. (Aside: The X column type is, in fact, nothing but a p column, with LaTeX performing the width-related calculations.)
    – Mico
    Commented Feb 12, 2019 at 10:46
  • I didn't knew this info. I will play with it, If I must need further clarificatoins, I will ping you via chat ;-). Commented Feb 12, 2019 at 10:56
6

Another solution with seqsplit, xltabular, which combines the functionalities of longtable and tabularx, and makecell to define some vertical padding of cells in the table:

\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} 

enter image description here

3

The problem is that the number is not in the list of words that tex uses to determine possible break points of words, but one can manually add them by placing \- at each point where the number can be broken across lines:

\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}

You must log in to answer this question.

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