6

What is the recommended approach to have inline number wrap across line boundaries? I was hoping that the \num macro took care of that, but it doesn't:

enter image description here

Code:

\documentclass{article}
\usepackage{showframe}
\usepackage{siunitx}

\begin{document} XXX is a transcendental number which has an approximate value of \num{0.425185173296160641}\dots. \end{document}

Peter Grill
  • 223,288
  • Should line breaks be allowed only after groups of three digits? Should line breaks be allowed both in the integer and the decimal part of a number, or only in the decimal part? – Mico Aug 21 '16 at 06:22
  • I know that you know what you are doing, but ... should numbers be broken up like that in normal text? – Johannes_B Aug 21 '16 at 06:43
  • @Mico: good questions. I think the ideal solution woudl allow for a line break between any group of three digits should (but perhaps not at the decimal point). – Peter Grill Aug 21 '16 at 07:03
  • 2
    @Johannes_B: Please don't let my rep fool you into thinking that I know what I am doing. To answer your question, I guess it would be better to break the number instead of going over the margin, but am open to suggestions. Suprisingly, I haven't encoutered this problem before. – Peter Grill Aug 21 '16 at 07:05

1 Answers1

7

The following example allows line breaks between digit groups by smuggling a line break penalty in group-separator. Also the surrounding \hbox is removed from the text mode number is removed, otherwise the penalties would not have an effect inside a unbreakable box.

\documentclass{article}
\usepackage{showframe}
\usepackage{siunitx}

\newcommand*{\linewrapnum}[2][]{%
  \begingroup
    \num[#1,%
      mode=text,
      group-separator=\linebreak[1]\thinspace
    ]{#2}%
    \setbox0=\lastbox
    \unhbox0\relax
  \endgroup
}

\begin{document}
XXX is a transcendental number which has an approximate value of
\linewrapnum{0.425185173296160641}\dots
\end{document}

Result

Heiko Oberdiek
  • 271,626