14

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??

screenshot

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)…

\newcommand{\listingsinpython}{
 \definecolor{listinggray}{gray}{0.9}
 \definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
 \lstset{
        backgroundcolor=\color{lbcolor},
        tabsize=4,
        rulecolor=,
        language=python,
        basicstyle=\ttfamily,
        upquote=true,
        aboveskip={1.5\baselineskip},
        columns=fixed,
        showstringspaces=false,
        extendedchars=true,
        breaklines=true,
        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
        frame=single,
        showtabs=false,
        showspaces=false,
        showstringspaces=false,
        keywordstyle=\color[rgb]{0,0,1},
        commentstyle=\color[rgb]{0.133,0.545,0.133},
        stringstyle=\color[rgb]{0.627,0.126,0.941},
 }
}

% and use it in the document
\listingsinpython

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

\begin{lstlisting}[language=c]
 task main() {
  SetSensorTouch(IN_1);
  OnFwd(OUT_AB, 75);
  while (Sensor(IN_1) == 0) {}
  Off(OUT_AB);
 }
 \end{lstlisting}

(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
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.)

\documentclass{article}

% Set the necessary font encoding and input encoding
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

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

% Set the default monospaced (teletype) family
\usepackage[ttdefault=true]{AnonymousPro}

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

\begin{document}

\section*{Programm}

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).

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

\end{document}

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
9

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.

\documentclass{article}
\renewcommand*\ttdefault{txtt}
\renewcommand*\familydefault{\ttdefault} %% Only if the base font of the document is to be typewriter style
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\newcommand\0{\scalebox{-1}[1]{0}}
\begin{document}
Default look of zero: 0
\begin{verbatim}
\verbatim 0123456789
\end{verbatim}

Setting zero to its mirror image
\catcode`0=\active
\def0{\0}

Revised look of zero: 0
\begin{verbatim}
\verbatim 0123456789
\end{verbatim}

\catcode`0=12% RESET MEANING OF 0

0 restored to original, but\\
I can still call on \0 at will.
\end{document}

enter image description here

FOLLOW UP:

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.

\documentclass{article}
\usepackage{marvosym,listings,etoolbox}
% THIS REPLACES 0 with \0 IN lstings
\lstset{literate={0}{\0}1{0\ }{\0\ }2}
% THIS REPLACES 0 WITH \0 IN verb
\patchcmd{\verb}{\dospecials}{\dospecials\atspecial}{}{}
\def\atspecial{\begingroup\lccode`~=`0%
  \lowercase{\endgroup\let~}\0%
  \catcode`0=\active}
%
\renewcommand*\ttdefault{txtt}
% COMMENT THE NEXT LINE TO TURN OFF texttt AS DEFAULT FONT FAMILY
\renewcommand*\familydefault{\ttdefault} % makes base font typewriter style
\usepackage[T1]{fontenc}
\usepackage{graphicx}
% DEFINE \0 AS MIRROR OF 0
\newcommand\0{\scalebox{-1}[1]{0}}
% FIX FOR \texttt AND \ttfamily
\let\svttfamily\ttfamily
\let\svtexttt\texttt
\catcode`0=\active
\def0{\0}
\renewcommand\ttfamily{\svttfamily\catcode`0=\active }
\renewcommand\texttt{\bgroup\ttfamily\texttthelp}
\def\texttthelp#1{#1\egroup}
\catcode`0=12 %
%
\begin{document}
Here is the font's normal 0 (which is reverse sense)\par
But it is fixed (un-reversed) in lstlisting:
\begin{lstlisting}[framexleftmargin=0mm,
                   basicstyle=\ttfamily\small,
                   breaklines,
                   columns=fullflexible]
' 0000 having 1=1--
' or 1 in (select @@version)--
' union all select @@version 0.0--
' OR 'unusual' = 'unusual'
\end{lstlisting}\par
as well as in verb:~\verb|declare @s0 varchar(8000)|

\noindent\hrulefill

%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

\noindent\hrulefill

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

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.