66

Should numbers inside the text be placed inside a math environment?

For example, if I am writing ...indent code by 4 spaces..., should I write it this way or ...indent code by $4$ spaces...?

Perhaps the output is the same but semantically they are not and maybe LaTeX can treat them differently in some cases. Which is the right one?

For instance, how does LaTeX treat the numbers in the counters it uses for numbering the sections or pages, etc? Does it treat them as numbers in a Math environment or as numbers inside text?

Of course, if the output can never be different in any situation, then well, it just doesn't matter.

3
  • 5
    That was almost a joke; but, in general, numbers up to ten or twelve which are not used as numbers in their mathematical meaning, are better spelled out in words.
    – egreg
    Dec 19 '11 at 21:09
  • 3
    see also this question about digits in math. Dec 19 '11 at 22:28
  • If @barbarabeeton had not mentioned the other question I was about to. I'm voting to close as a duplicate (though this one has reasonable answers, I believe they are not actually different from the ones in the other question).
    – Ryan Reich
    Dec 20 '11 at 1:00
59

The answer to your question strongly depends on whether the math-mode numerals appear to be (more or less) the same as text-mode numerals, for the font you use. If the two sets of numerals happen to be exactly the same, nobody will ever be able to tell unless they have access to the source code...

However, if they differ noticeably in appearance -- say, because "oldstyle" numerals are used in text mode, which in general should not be used in mathematical expressions -- you obviously need to be careful to distinguish between the two types of numerals.

The following is an excerpt from Knuth's article "Typesetting Concrete Mathematics" (TUGBoat, 1989) [Aside: The textbook Concrete Mathematics was typeset using Concrete Roman as the text font and Euler as the math font]:

There was, however, one significant difference between typing the manuscripts for Concrete Mathematics and for The Art of Computer Programming, caused by the fact that the Euler numerals 0123456789 are distinctly different from the numerals 0123456789 in ordinary text. [...] This experience ... taught me that there is a useful and meaningful distinction between text numerals and mathematical numerals. Text numerals are used in contexts like '1776' and 'Chapter 5' and '41 ways', where the numbers are essentially part of the English language; mathematical numerals, by contrast, are used in contexts like 'the greatest common divisor of 12 and 18 is 6', where the numbers are part of the mathematics. [...] Equation numbers presented us with one of the most perplexing design questions. Should those numbers be typeset in Euler or cast in Concrete? After several experiments we hit on a solution that must be right, because it seems so obvious in retrospect: We decided to set equation numbers in an "oldstyle" variant of Concrete Roman ...

What's the upshot of all this? I'd say it's the following: even if at present you're using text and math fonts that have identical-looking numerals, you should still be willing to make the extra effort today to set those numerals that are parts of math expressions in math mode -- as long as there is a chance that you might wish to re-publish your piece one day using fonts for which the math- and text-mode numerals are no longer the same.

What, then, are instances of when you should use text-mode numerals? In addition to the examples given in the preceding quote, I'd also nominate the following: page numbers; numbers of chapters, sections etc; dates (December 25, August 1, ...), and (depending on the circumstances) equation numbers. Obviously, this short list is not meant to be exhaustive.

Addendum: I just noticed that the first two up-votes to this answer earned me my 10,000th point in the TeX.SE group. Thanks everyone!! :-)

5
  • Great answer! However I'm wondering what to do about negative text numerals? Unfortunately, - doesn't give minus but a hyphen-minus and the only correct ways to get a mathematical minus is to use the Unicode-symbol (with appropriate engine and packages loaded) or $-$. Any idea on how to achieve that in a better way?
    – ljrk
    Aug 28 '20 at 12:12
  • @ljrk - Please provide an example of what you believe to be a negative-valued text numeral. My view is that text-mode numerals should be positive integers. E.g., page numbers run from 1, 2, 3 and so on; same for the day of the month, the number of flavors that used to be sold by a certain ice cream brand, and so on. If you have a negative number or a number with a decimal component, it's probably not a text numeral at all but, instead, the outcome of some calculation, which should be displayed as a math-mode numeral. E.g., "2-4=-2" should be typeset in math mode: $2-4=-2$.
    – Mico
    Aug 28 '20 at 12:42
  • 1
    I'm talking about CPU rings of the x86 computer architecture. Initially there were the rings 0 to 3 but later additional rings where added below 0, thus dubbed "ring -1" and "ring -2". It's an odd niche use case for sure. Currently I'm using ring~\num{-1} (with siunitx set up in "detect" mode) or ring~\textminus 1, but I'm not sure whether this is the proper way to do it.
    – ljrk
    Aug 28 '20 at 14:20
  • 1
    @ljrk - Wow, that's definitely a use case I wasn't even aware of until now. Another, somewhat debatable, use case for negative text-mode numbers is temperatures expressed in degrees Fahrenheit or Celsius. E.g., one might write "Boy, it's extremely cold today: -40 degrees!" as ... cold today: $-$40 degrees! (For sure, though, if I were writing about Kelvin-scale temps, I'd use only lining numerals.) For your use case, I might employ lining numerals as well, just to make absolutely clear to your readers that the positive and negative ring numbers are used in a very precise, non-casual sense.
    – Mico
    Aug 28 '20 at 14:43
  • 1
    Yeah, it's so odd that I only really thought about it after studying and researching in this field for over years now. Using $-$40 may result in the minus being set in math font, which may or may not look unpleasant (in my font it has quite a different stroke width); also I'm a bit unsure about the operator spacing. Also, with fontspec set to OldStyle Nums, the 40 would be set into old style, rather than lining. I probably combine tex.stackexchange.com/a/8992/90407 and tex.stackexchange.com/a/79157/90407 to get consistent and reader-friendly typography. Thanks for the input!
    – ljrk
    Aug 28 '20 at 15:30
15

I am always using the \num{} from the siunitx package. Here are some examples from the manual:

\num examples

4
  1. Numbers should always be given in $$, the single reason for this is that if you later choose a different font, which handles numbers in a different manner you will see the difference quite distinctively. (There are cases were this is not the case, for instance dates, thanks egreg)

  2. Counters have nothing to do with math-mode. You need also to set them in mathmode if using them as output tokens.

If having specific counters as \thepage or \thesection etc. you need to find out what that command does. It is up to you to make them behave as you like. See the following code which utilizes the kpfonts package.

\documentclass{article}
\usepackage{kpfonts}
\begin{document}
\mathversion{boldsf}
\makeatletter
\meaning\thesection
\meaning\thepage
\meaning\@arabic
$2$
2
\thepage
\def\thepage{$\arabic{page}$}
\thepage
\meaning\thepage

\end{document}

In the output you will see what the command is expanded to, and also how to redefine it.

5
  • 9
    This is not true: dates should be in the text font.
    – egreg
    Dec 19 '11 at 21:02
  • I completely agree. I didn't elaborate on that! Have added.
    – zeroth
    Dec 19 '11 at 21:04
  • 1
    Yes, counters on their own have nothing to do but I was asking when LaTeX automatically uses them for numbering pages or sections. How does it treat them? For example if it uses the counters as text I don't want to be putting all numbers in math mode because then a number I am using for counting may look different from those numbering the sections or the pages. Dec 19 '11 at 21:04
  • That depends on the command, i have updated my answer.
    – zeroth
    Dec 19 '11 at 21:11
  • 2
    I'd say that also page numbers should be in the main text font. But a choice depends mostly on the type of text: in a text with very little math, I'd probably typeset also "proper numbers" (in tables, for instance) as lowercase (old style), if the main font uses them.
    – egreg
    Dec 19 '11 at 21:42
2

My understanding is that the only time it matters is when you're using a math font like Euler that comes with its own set of digits instead of just "borrowing" its digits from the text font. And even then, the important thing is just to be consistent: you wouldn't want a document where some of the page numbers are in Euler and some are in Palatino or whatever text font you're using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.