4

I have the following line in LaTeX:

The primes from $1$ to $1000$ are $2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997.$

This does not wrap around the page, instead it goes off the page:

Primes going off the page How can I get this to wrap around the page?

4
  • 4
    LaTeX are not allowed to line break after a comma by default (we do not want say f(x,y) to break). You can manually allow it by adding \allowbreak after the commas at which a break was allowed.
    – daleif
    Commented Sep 13, 2023 at 13:31
  • 2
    A couple of ideas here: Allowing line break at ',' in inline math mode?. But since numbers are taken from the upright font anyway, for this specific problem you could also just avoid math mode...
    – campa
    Commented Sep 13, 2023 at 13:32
  • 2
    In other words, you only need $...$ with negative numbers. Commented Sep 13, 2023 at 13:36
  • 1
    @JohnKormylo Not at all! What if you have 1, 2, 3 (as numbers) in the statement of a theorem?
    – egreg
    Commented Sep 13, 2023 at 15:08

3 Answers 3

15

I would argue that your approach to inputting the numbers is faulty. What you're conveying to your readers is a collection of individual numbers; this makes the commas part of the textual or sentence structure, rather than part of the mathematical structure of the information you mean to convey.

Hence, either render each number separately in (inline) math mode, as is done in the example below, or -- recommended if the math font and text font are in the same font family -- don't use (inline) math at all.

enter image description here

\documentclass{article} % or some other suitable document class
\begin{document}
The primes from $1$ to $1000$ are $2$, $3$, $5$, $7$, $11$, $13$, $17$, $19$, $23$, $29$, $31$, $37$, $41$, $43$, $47$, $ 53$, $59$, $61$, $67$, $71$, $73$, $79$, $83$, $89$, $97$, $ 101$, $103$, $107$, $109$, $113$, $127$, $131$, $137$, $139$, $149$, $151$, $157$, $163$, $167$, $173$, $179$, $181$, $191$, $193$, $197$, $199$, $211$, $223$, $227$, $229$, $233$, $239$, $241$, $251$, $257$, $263$, $269$, $271$, $277$, $281$, $283$, $293$, $307$, $311$, $313$, $317$, $331$, $337$, $347$, $349$, $353$, $359$, $367$, $373$, $379$, $383$, $389$, $397$, $401$, $409$, $419$, $421$, $431$, $433$, $439$, $443$, $449$, $457$, $461$, $463$, $467$, $479$, $487$, $491$, $499$, $503$, $509$, $521$, $523$, $541$, $547$, $557$, $563$, $569$, $571$, $577$, $587$, $593$, $599$, $601$, $607$, $613$, $617$, $619$, $631$, $641$, $643$, $647$, $653$, $659$, $661$, $673$, $677$, $683$, $691$, $701$, $709$, $719$, $727$, $733$, $739$, $743$, $751$, $757$, $761$, $769$, $773$, $787$, $797$, $809$, $811$, $821$, $823$, $827$, $829$, $839$, $853$, $857$, $859$, $863$, $877$, $881$, $883$, $887$, $907$, $911$, $919$, $929$, $937$, $941$, $947$, $953$, $967$, $971$, $977$, $983$, $991$, and $997$.
\end{document} 

Addendum: The solution to the OP's typesetting problem can be boiled down to deciding whether the commas between the numbers are part of the sentence structure -- and thus should be rendered in text mode (greatly simplifying the typesetting job as far as TeX and LaTeX are concerned), regardless of whether the numbers are rendered in text or math mode -- or are part of a math expression -- and thus should be rendered in math mode). Are there some guidelines to help decide which case applies? Yes.

  • Context. Take, say, the sentence "The first seven Fibonacci numbers are 0, 1, 1, 2, 3, 5, and 8." It's fairly clear that the commas function as ordinary separators between objects; the fact that these objects happen to be numbers is secondary. Conclusion: The sentence should be entered as

    The first seven Fibonacci numbers are 0, 1, 1, 2, 3, 5, and 8.
    
  • Another give-away is the presence of the connector word "and" in the sentence above. In English language texts (not sure about other languages), it would be quite unusual to insert the connector word "and" (with or without an "Oxford" comma) if the main purpose of the statement were not to list some numbers but to display a particular mathematical sequence involving numbers.

  • Does the ordering of the numbers matter? If a given ordering of the numbers is "nice" or "convenient" (e.g., listing the prime numbers in ascending order), but if it's not intrinsically important to list the numbers in that particular order, the commas almost certainly function mainly as visual separators of various elements within that sentence -- and hence should not be rendered in math mode. E.g., in the OP's example, it's not really important (though probably quite helpful to one's readers...) to list the prime numbers in ascending order; if they were listed in reverse or descending order, they'd still be the complete list of prime numbers, right? To conclude, there would be no need to treat the commas as part of a math expression.

In practice, it may be up to the author to clarify to to their readers what's going on. If the list of numbers is, in fact, a mathematical sequence (in which the ordering matters), don't hesitate to surround the sequence by curly braces, or whatever typesetting convention may apply for your region and language.

8

An old-style implementation:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\lon}[1]{% list of numbers
  \ifmmode
    \if@display
      \ERROR
    \else
      \lon@{#1}%
    \fi
  \else
    $\lon@{#1}$%
  \fi
}
\newcommand{\lon@}[1]{%
  \begingroup
  \lon@activate\mathcode`,="8000 #1
  \endgroup
}
\newcommand{\lon@activate}{%
  \mathchardef\lon@comma=\mathcode`,
  \begingroup\lccode`~=`,\lowercase{\endgroup
    \def~}{{\lon@comma}\penalty0 \hspace{0.3em plus 0.2em minus 0.1em}}
}
\makeatother

\begin{document}

The primes from $1$ to $1000$ are \lon{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 
307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 
499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 
607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 
823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 
937, 941, 947, 953, 967, 971, 977, 983, 991, 997}.

We have an inline formula $X=\{\lon{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}\}$

We have an inline formula \(X=\{\lon{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}\}\)

\[
\lon{1,2,3}
\]

\end{document}

The \lon (list of numbers) command cannot be used in displays, where it doesn't make sense.

A new-style implementation.

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\lon}{m}
 {% #1 = list of numbers
  \mode_if_math:TF
   {
    \legacy_if:nTF {@display} { \ERROR } { $ \scan_stop: \robin_lon:n { #1 } \scan_stop: $ }
   }
   {
    \robin_lon:n { #1 }
   }
 }

\cs_new_protected:Nn \robin_lon:n
 {
  $ \clist_use:nn { #1 } { {,}\penalty0 \hspace{0.3em plus 0.2em minus 0.1em} } $
 }

\ExplSyntaxOff

\begin{document}

The primes from $1$ to $1000$ are \lon{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 
307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 
499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 
607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 
823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 
937, 941, 947, 953, 967, 971, 977, 983, 991, 997}.

We have an inline formula $X=\{\lon{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}\}$

We have an inline formula \(X=\{\lon{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}\}\)

\[
\lon{1,2,3}
\]

\end{document}

enter image description here

5

You already got good LaTeX advice here by Mico, so I feel free to add here a way to do it in ConTeXt lmtx, with the help of penalties.

To easily be able to turn it on and off, we define below Pmath, a new instance of mathematics. We use the setups on the first few lines that set the penalty after the mathpunctuation atom class to 9999 (by default no such breaks are allowed). This does indeed enable breaks after the comma (there is also \setmathprepenalty in case one wants to enable spaces before a class), but to the price of a large penalty. In this example, this is sufficient, since there are no other places to break.

To use this new instance of math we do \m[Pmath] instead of \m.

\startsetups MyPunctuationMath
  \setmathpostpenalty \mathpunctuationcode 9999
\stopsetups

\definemathematics
  [Pmath]
  [setups=MyPunctuationMath]

\starttext

\startbuffer
The primes from $1$ to $1000$ are \m{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}.

\blank

The primes from $1$ to $1000$ are \m[Pmath]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}.
\stopbuffer

\getbuffer

\blank

\showmakeup[penalty]
\getbuffer

\stoptext

breaking after comma

Not to write everything twice, we put the stuff in a buffer, and with \showmakeup[penalty] we show also the penalties. As you see they are now 9999 after the commas.

You must log in to answer this question.

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