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

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}

enter image description here


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

enter image description here


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.

enter image description here

\documentclass{article}
\usepackage{luacode} % for "\luaexec" macro
\luaexec{ %% Set up the Lua function:
   function ttnumbers ( s )
      return ( s:gsub ( "\%d+" , "\\texttt{\%0}" ) )
   end
}
\newcommand\ttnumbersOn{\directlua{luatexbase.add_to_callback(
   "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}