The digit 0 in our documents, such as this one (external PDF link), is slashed “the wrong way” in source code blocks (lstlisting).

How can we get it slashed “the right way” (from bottom-left to top-right) in the monospace font??


Bonus for also getting the zero in the regular (sans- and serif) text to slashed “the right way”.

Update: these are the commands used to define the style (once, context in line 168ff)…

        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},

% and use it in the document

… and get the output (context in line 52ff):

 task main() {
  OnFwd(OUT_AB, 75);
  while (Sensor(IN_1) == 0) {}

(Yes, the “listings in Python” and “language=c” is a bit confusing, sorry about that, organic growth of the code.)

  • Is there a font with the zero you prefer? – musarithmia Feb 25 '15 at 17:43
  • 4
    this probably won't be any help, but may provide amusement: a tugboat article "Oh, oh, zero!" by chuck bigelow. – barbara beeton Feb 25 '15 at 17:47
  • 3
    It's a font designer's decision. There is no "rights" and "wrongs" here. There is a nice reading about it in TUGboat 34 (2):168–181. – yo' Feb 25 '15 at 17:49
  • @barbarabeeton Indeed, it amused me! – Gonzalo Medina Feb 25 '15 at 17:51
  • 2
    @mirabilos Why don't you look through the typewriter fonts in the LaTeX Font Catalog online? If you click on each one there is a sample of all the numerals. Try this: \usepackage[T1]{fontenc} \usepackage[ttdefault=true]{AnonymousPro} – musarithmia Feb 25 '15 at 18:14

The TUGboat article linked by Barbara Beeton provides an in-depth study of the ambiguous-zero problem. In LaTeX's default typeface, Computer Modern, the zero in the typewriter font is narrow and round, as opposed to a squarish capital "Oh". But you may have your pick of other monospaced typefaces.

The LaTeX Font Catalog provides a list of all the monospaced fonts that come with a full TeXLive distribution, so if you have LaTeX installed on any operating system, you can probably any of those fonts. Each page in the font catalog includes an image of the numerals.

In most cases, as the catalog shows, you simply call a font package in the preamble. Most of the typewriter font packages only redefine the default typewriter family and so do not affect the other fonts in use.

The document you linked to uses Nimbus Sans for the main body text (I might recommend TeX Gyre Heros, which is based on it). To match it with a monospaced font with the slashed zero from bottom left to top right, you could use the Anonymous Pro package like so. (I will not try to reproduce all the formatting of the original.)


% Set the necessary font encoding and input encoding

% Set the default sans-serif family and make it the default for body text
\usepackage{tgheros} % or \usepackage[scaled]{helvet} for URW Nimbus Sans

% Set the default monospaced (teletype) family

% Use a package to allow verbatim code listings
% These will be set in the default monospace family (\ttdefault)



Dieses Programm geht davon aus, dass Du die Motoren mit den Anschlüssen A und B und den Sensor mit dem Anschluss 1 verbindest. 
Wenn Du andere Anschlüsse verwenden möchtest, musst Du das Programm entsprechend anpassen (wenn Du magst, überlege Dir, wie).

task main () {
    SetSensorTouch (IN_1);
    OnFwd (OUT_AB , 75);
    while ( Sensor (IN_1) == 0) {}
    Off( OUT_AB );


enter image description here

  • In the end, I went with this (and Inconsolate, AnonymousPro has the same size as the surrounding text and integrates better, but is not narrow enough for some existing lines). I only had a problem when one listing contains out of two lines with identical prefix+space (JAVA_OPTS="${JAVA_OPTS} foo" and JAVA_OPTS="${JAVA_OPTS} bar"), then the copy/paste text would be out of order, but when trying to make a minimal working example from it, it would suddenly work… I just split the listing to fix it. – mirabilos Nov 27 '15 at 15:06

EDITED to better demonstrate the technique, using txtt as the ttfont.

FIX for lstlisting and \verb (and \texttt and \ttfamily) given in FOLLOW UP at end:

Here, I show how a simple \scalebox can be used to invert the slash through a defined macro \0. I also show how making the 0 active allows one to use the reversed version of the zero inside verbatim environments.

Obviously, one would not want 0 active all the time, but turning it on for verbatim environments is a viable way to remedy the issue cited by the OP. Alternately, in normal text, one could just invoke the reflected image as \0.

\renewcommand*\familydefault{\ttdefault} %% Only if the base font of the document is to be typewriter style
Default look of zero: 0
\verbatim 0123456789

Setting zero to its mirror image

Revised look of zero: 0
\verbatim 0123456789

\catcode`0=12% RESET MEANING OF 0

0 restored to original, but\\
I can still call on \0 at will.

enter image description here


Using egreg's answer at email symbol while using package listings, I can automagically replace the reverse 0 with its mirror image in lstlisting (by using literate) and in \verb arguments (through a patch) as such.

EDITED to fix \ttfamily and \texttt, as well. Note, though, that use of ttfamily should be grouped, or the active 0 could break other things.

% THIS REPLACES 0 with \0 IN lstings
\lstset{literate={0}{\0}1{0\ }{\0\ }2}
\renewcommand*\familydefault{\ttdefault} % makes base font typewriter style
% FIX FOR \texttt AND \ttfamily
\renewcommand\ttfamily{\svttfamily\catcode`0=\active }
\catcode`0=12 %
Here is the font's normal 0 (which is reverse sense)\par
But it is fixed (un-reversed) in lstlisting:
' 0000 having 1=1--
' or 1 in (select @@version)--
' union all select @@version 0.0--
' OR 'unusual' = 'unusual'
as well as in verb:~\verb|declare @s0 varchar(8000)|


%Now we try it in texttt: \texttt{foo0bar} or this {\ttfamily foo0bar},
texttt: \texttt{foo0bar}

or back to 0

ttfamily: {\ttfamily foo0bar}

or back to 0 again


I didn't break verb, did I? \verb|declare @s0 varchar(8000)|

enter image description here

By commenting out the \familydefault redefinition, the result is basically what the OP is asking for: normal text unaltered in \rmfamily, but in listings, verbatim, ttfamily, and texttt, the reverse 0 is un-reversed.

enter image description here

  • Scary, but interesting. I just amended the question with the actual code. I understand the general idea of how this works. From what I see, I would need to make the \def0{\0} to be only active inside a lstlisting block. Sorry for the lots of newbie questions, but, how do I do that? (Let’s keep the main document intact and only change the listings here; especially if the main document body contains a 0 in italicised text it would probably look weird otherwise, with this method.) – mirabilos Feb 27 '15 at 10:33
  • @mirabilos In general, a \catcode(backtick)0=\active before the verbatim block and a \catcode(backtick)0=12 after it would be required. The \def0{\0} has to occur just once, at a point when 0 is active. I am not a user of lstlisting, however, I think they may have a means to do what I described within the specifications/options to a lstlisting block. I concur that this should only occur for verbatim blocks, not only for the italic reason you cite, but also because it destroys the ability to use 0 as a numerical argument in a macro. – Steven B. Segletes Feb 27 '15 at 11:46
  • @mirabilos Using an answer from egreg, I have revised my answer to show how to automatically reverse the sense of 0 in lstlistings and \verb. – Steven B. Segletes Feb 28 '15 at 3:35
  • In the end, this comment made me switch to the other answer (use a better font), as I’ve got troubles with zeros in tickets and tables and other fun… but still, amazing answer, and thanks for trying. – mirabilos Nov 27 '15 at 14:26

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.