Make single-dollar equation wrap

equations

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?

Best Answer

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.