# How could one have typewriter (monospaced) numbers throughout a document (LuaLaTeX)

• I would like to have all numbers in a typewriter (monospace) font throughout my document, but maintain the usual serif font for everything else.
• See column A in the picture below (which was kindly provided by user egreg.)
• How could I achieve this?
• I want to use using LuaLaTeX.
• Currently, I am not using the fontspec package and I am utilizing the default \rmfamily as my main font.

• Your objective isn't clear. In the title, you employ the term monospaced, but in the body of the posting you use the term teletyped. These terms are not synonyms. While it's true that "teletype" numerals all take up the same width (indeed, the same width as all other printable characters do) and hence can be called "monospaced", the converse is not true. Numerals can either take up the same amount of space ("fixed width") or be proportionately spaced. This is true regardless of whether the numerals are uppercase/lining or lowercase/oldstyle. In short, please clarify your objective. – Mico Mar 1 at 11:49
• @Mico Your comment on the difference between the terms monospaced and teletyped (I do not know the latter term) is not clear to me. You mention that both have fixed width glyphs but then talk about proportional spacing. What would be an example of a font / numerals that are one but not the other? – schtandard Mar 1 at 13:43
• @SeSodesa Please specify whether you want column A or column B in this picture – egreg Mar 1 at 14:33
• @schtandard - My apologies for not being sufficiently clear in the distinctions I made. Let me try again. (a) All teletype font faces are monospaced; in particular, the glyphs of the arabic numerals subset take up the same space. (b) For a non-teletype font face, it does not follow that its numerals are necessarily not monospaced. Indeed, for font faces such as CM Roman and CM Sans, their numerals are monospaced by default. – Mico Mar 1 at 14:48
• @Mico I think that the idea is on the verge of perversion… – egreg Mar 1 at 17:05

Here's a solution which works with and without the fontspec package.

The solution consists of (a) a Lua function called ttnumbers, which uses Lua's powerful string.gsub function to encase all instances of numerals found in the input stream in \texttt directives, and (b) two utility Latex macros, called \ttnumbersOn and \ttnumbersOff, which activate and deactivate the ttnumbers function. Note that the action of the \ttnumbersOn and \ttnumbersOff directives takes effect not on the line where the respective macros are encountered, but only on the next line.

\documentclass{article}
\usepackage{luacode} % for "\luaexec" macro
\luaexec{ %% Set up the Lua function:
function ttnumbers ( s )
return ( s:gsub ( "\%d+" , "\\texttt{\%0}" ) )
end
}
"process_input_buffer", ttnumbers , "ttnumbers" )}}
\newcommand\ttnumbersOff{\directlua{luatexbase.remove_from_callback(
"process_input_buffer", "ttnumbers" )}}

\begin{document}
%% roman text font
1234567890 vs.\
\ttnumbersOn
1234567890
\ttnumbersOff

% math font
$1234567890$ vs.\
\ttnumbersOn
$1234567890$
\ttnumbersOff

% sans-serif text font
\sffamily
1234567890 vs.\
\ttnumbersOn
1234567890
\end{document}

• This seems to break any calls to \input{<filename with number>} with an error Missing \endcsname inserted.. For example, \input{01-introduction.tex} becomes \input{\texttt{01}-introduction.tex}, which LuaLaTeX seems to dislike. – SeSodesa Mar 1 at 17:05
• @SeSodesa — You’re absolutely correct — and I’m entirely unapologetic. Indeed, I’m certain that there are many other circumstances where this approach will also misfire. That’s precisely why I provided the \ttnumbersOff macro as well — to let you suspend operation of the Lua function. Since you provided exactly zero background as to how you might use the numerals-substitution procedure, I’m simply in no position to provide something more robust. – Mico Mar 1 at 17:25
• That is fair. I might ask another question later on how this might be improved to facilitate the use of the mentioned \input calls and citations. This approach does break pretty much every feature useful in writing articles or theses, after all, as it is unnecessarily difficult to avoid inserting numbers into your .bib file, for example, if you wish to differentiate between two different articles from the same author. – SeSodesa Mar 1 at 18:44
• @SeSodesa - Thanks for the accept vote. :-) I must confess that I simply don't understand the entire premise of your query. The numerals of the Computer Modern and Latin Modern serif and sans-serif fonts are monospaced -- why not declare victory right there and then? :-) LaTeX and TeX are supposed to enable creating of fine typography. Unquestionably, mixing-and-mismatching roman and teletype glyphs is pretty much the antithesis of fine typesetting. – Mico Mar 1 at 19:17

With lualatex you can setup combo fonts. But imho this is rather bad typography.

\documentclass{article}
\usepackage{combofont}
\setupcombofont{ttnumber-regular}
{
{file:lmroman10-regular.otf:\combodefaultfeat} at #1pt,
{file:lmmono10-regular.otf:mode=node;} at #1pt
}
{
{},
0x30-0x39
}

\setupcombofont{ttnumber-bold}
{
{file:lmroman10-bold.otf:\combodefaultfeat} at #1pt,
{file:lmmonolt10-bold:mode=node; } at #1pt
}
{
{},
0x30-0x39
}

\DeclareFontFamily{TU}{ttnumber}{}
\DeclareFontShape {TU}{ttnumber}{m}{n} {<->combo*ttnumber-regular}{}
\DeclareFontShape {TU}{ttnumber}{b}{n}{<->combo*ttnumber-bold}{}
\renewcommand\rmdefault{ttnumber}
\begin{document}
some text 1234567890 \bfseries some text 1234567890

\end{document}


• Thank you for this package! I had similar problems to solve in the past and it was mostly a rather difficult approach compared to this. – Alexander Mar 4 at 13:33

With ConTeXt this can be achieved with the font fallback mechanism where you can replace certain letters with letters from another font.

\definefallbackfamily [teletypenumbers] [rm] [Latin Modern Typewriter]
[range=digitsnormal,
force=yes]

\definefontfamily [teletypenumbers] [rm] [Latin Modern Roman]

\setupbodyfont[10pt]

\starttext

Number test: 0123456789

\setupbodyfont [teletypenumbers]

Number test: 0123456789

\stoptext