2

How do I word-wrap the numbers in my columns?

overflow

Here I've got a tabular environment within a table environment (the latter for captions). Elsewhere I use longtabus without a table environment, and would like to word wrap in both scenarios.

\documentclass{article}
\usepackage{array}
\newlength\mylen
\setlength\mylen{2\tabcolsep}
\addtolength\mylen{\arrayrulewidth}
\begin{document}
\begin{table}
\begin{tabular}{|%
     p{\dimexpr 0.08\linewidth-\mylen-\arrayrulewidth}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.06\linewidth-\mylen}|
     p{\dimexpr 0.06\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|} 
     \hline
     \textbf{h} & \textbf{a} & \textbf{b} & \textbf{c} &
     \textbf{d} & \textbf{e} & \textbf{f} & \textbf{g} &
     \textbf{h} & \textbf{i} & \textbf{j} & \textbf{k} \\
     & & & & & & & & & & & \\
     \hline

Then the actual row:

     & 2.072779 & 0.843488 & 0.877791 & 78.0 & 11.0 & 80.0 
     & 23.0 & 0.697509 & 0.772277 & 0.879121 & 0.003292 \\

and finally:

\hline
\end{tabular}
\end{table}
\end{document}
9
  • I've taken the liberty of streamlining and simplifying your code. Feel free to revert.
    – Mico
    Oct 12, 2020 at 5:17
  • 2
    Is it really useful to allow linebreaks within numbers? In order to make sure, your table fits into the textwidth, you could reduce the fo t size, and adjust the value of tabcolsep.
    – leandriis
    Oct 12, 2020 at 5:26
  • 2
    Unrelated to the current issue but please be aware that the tabu package (that you use for longtabu) is currently unmaintained and can result in some undesired behavior: github.com/tabu-issues-for-future-maintainer/tabu
    – leandriis
    Oct 12, 2020 at 5:53
  • @leandriis - Yeah I know that's why I'm trying to stick to tabularx; but ended up needing a longtable. - In terms of is it useful… yes it is. These are the real numbers; I suppose everything after the decimal could be made a much smaller font… or it could just overflow. - I've almost converted all my Word to LaTeX, and the overflow was a "feature" of Word's tables.
    – A T
    Oct 12, 2020 at 8:26
  • If you just want to use X type columns in a table that can break across pages, you could use the xltabular package. Regarding the table itself: do you use the geometry package in your actual document? If so, could you please include the corresponding settings in your question? Depending o the overall size of the table, e.g. the number of rows, either rotating the table to a landscape page or transposing it might be alternatives to introducing linebreaks within numbers.
    – leandriis
    Oct 12, 2020 at 9:12

2 Answers 2

1

Although the following does not directly answer the question "how to enable linebreaks inside of numbers in table cells", the following layout based on landscape from the pdflscape package could be an alternative:

enter image description here

\documentclass{article}

\usepackage{pdflscape}

\usepackage{booktabs}

\begin{document}
\begin{landscape}
\begin{table}
\centering
\begin{tabular}{|*{13}{c|}} 
     \hline
     \textbf{h} & \textbf{a} & \textbf{b} & \textbf{c} &
     \textbf{d} & \textbf{e} & \textbf{f} & \textbf{g} &
     \textbf{h} & \textbf{i} & \textbf{j} & \textbf{k} \\
     \hline
  & 2.072779 & 0.843488 & 0.877791 & 78.0 & 11.0 & 80.0 
     & 23.0 & 0.697509 & 0.772277 & 0.879121 & 0.003292 \\
     \hline
\end{tabular}
\end{table}
\end{landscape}

\begin{landscape}
\begin{table}
\centering
\begin{tabular}{*{13}{c}} 
     \toprule
     \textbf{h} & \textbf{a} & \textbf{b} & \textbf{c} &
     \textbf{d} & \textbf{e} & \textbf{f} & \textbf{g} &
     \textbf{h} & \textbf{i} & \textbf{j} & \textbf{k} \\
     \midrule
  & 2.072779 & 0.843488 & 0.877791 & 78.0 & 11.0 & 80.0 
     & 23.0 & 0.697509 & 0.772277 & 0.879121 & 0.003292 \\
     \bottomrule
\end{tabular}
\end{table}
\end{landscape}
\end{document}

If you insist on allowing linebreaks, you can make use of the seqsplit package as shown in the following example:

enter image description here

\documentclass{article}
\usepackage{array}
\newlength\mylen
\setlength\mylen{2\tabcolsep}
\addtolength\mylen{\arrayrulewidth}

\usepackage{seqsplit}
\begin{document}


\begin{table}
\begin{tabular}{|%
     p{\dimexpr 0.08\linewidth-\mylen-\arrayrulewidth}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.06\linewidth-\mylen}|
     p{\dimexpr 0.06\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.10\linewidth-\mylen}|
     p{\dimexpr 0.08\linewidth-\mylen}|} 
     \hline
     \textbf{h} & \textbf{a} & \textbf{b} & \textbf{c} &
     \textbf{d} & \textbf{e} & \textbf{f} & \textbf{g} &
     \textbf{h} & \textbf{i} & \textbf{j} & \textbf{k} \\
     & & & & & & & & & & & \\
     \hline
       & \seqsplit{2.072779} & \seqsplit{0.843488} & \seqsplit{0.877791} & \seqsplit{78.0} & \seqsplit{11.0} & \seqsplit{80.0} 
     & \seqsplit{23.0} & \seqsplit{0.697509} & \seqsplit{0.772277} & \seqsplit{0.879121} & \seqsplit{0.003292} \\
     \hline
\end{tabular}
\end{table}
\end{document}
1

Split a number digits on several lines is not very smart idea, in my opinion it is better to split and transpose table:

enter image description here

(red lines indicate text area borders)

With use of the tabularray package and its libraries, which call booktabs and siunitx package, the MWE is:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}
    \centering
\begin{tblr}{hline{2-Y}, vlines,
             colspec = {Q[c, font=\bfseries]
                        Q[c, si={table-format=2.6}] |
                        Q[c, font=\bfseries]
                        Q[c, si={table-format=2.6}]
                        },
             }
     \toprule
h   &    2.072779   &   f   &   23.0        \\
a   &    0.843488   &   g   &    0.697509   \\
b   &    0.877791   &   h   &    0.772277   \\
c   &   78.0        &   i   &    0.879121   \\
d   &   11.0        &   j   &    0.003292   \\
e   &   80.0        &       &               \\
     \bottomrule
\end{tblr}
    \end{table}
\end{document}

Edit:
you may split table on more parts, for example on four. In this case the table height is the same as at splitting numbers digits on three lines:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}
    \centering
\begin{tblr}{hline{2-Y}, vline{1-Y}=solid,
             colspec = {*{4}{Q[c, font=\bfseries]
                             Q[c, si={table-format=2.6}] |}
                        },
             }
     \toprule
h   & 2.072779  & c & 78.0  & f & 23.0      & i & 0.879121   \\
a   & 0.843488  & d & 11.0  & g &  0.697509 & j & 0.003292   \\
b   & 0.877791  & e & 80.0  & h &  0.772277 &   &           \\
     \bottomrule
\end{tblr}
    \end{table}
\end{document}

enter image description here

You must log in to answer this question.

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