20

I want to put an entire document into monospace font for various reasons. This is nominally fairly easy; you can say \renewcommand{\familydefault}{\ttdefault}. However, there at least two problems with this approach (and possibly more). Thus, my question is really three separate ones:

  1. So far, I'm having a problem with the spaces not working as they should; this seems to be fixed by \obeyspaces, but I am unsure of how well this really works. The only references to it I could find were on this site, and they didn't really explain that command well. Could you please provide a reference for this or at least explain what the command does and what its limitations are?
  2. My other problem is that sometimes LaTeX generates symbols (such as smart quotes), and these do not obey the monotype spacing. LaTeX even seems to generate these when the symbols are in the font used. Is there any way of finding out when this happens, and preferrably of telling LaTeX to use existing characters when possible? Update: This is not a problem at all; see below.
  3. I have found these by myself, but it seems quite likely that there are other problems as well. Are there other things I should look out for, like accented characters? Bold seems to work correctly (at least in the Courier font), but will all such formatting?

Update: In case it is relevant (which, now that I think of it, it almost certainly is), I am using XeTeX and fontspec. I would prefer to keep this setup, but would be open to not using fontspec or to using pdflatex instead.

Update 2: The "smart quotes" problem was actually a problem with spaces after punctuation. I turned on French spacing, but this just emphasizes the need for part three of my question.

3
  • If you want the whole document set in monospace with spaces and newlines preserved, LaTeX seems the wrong tool for the job.
    – Chel
    Sep 9, 2012 at 6:40
  • I never said I want newlines preserved, as I do not in fact want that. If it seems like my question implies that, then please tell me where and I'll revise the question. The thing is, I do want other features of LaTeX, like various kinds of lists, smart quotes, references, links, index, glossary, text formatting, larger sections, etc. Most of these I could get in a traditional word processor, but given my expected usage, LaTeX seems like a better tool. Additionally, although I am not currently planning to use it, the document will be updated regularly and may require math mode in the future.
    – Daniel H
    Sep 9, 2012 at 6:51
  • 2
    May I ask why you want everything monospaced? Monospace is fine for source code, but IMHO makes your eyes bleed if you have to read several pages. If you really like it I think it's fine, but most likely not if others are to read it. Sep 9, 2012 at 8:36

2 Answers 2

13

The easiest (but not the most flexible) way is to use memoir with the ms option. Then you get something that look like a good, old typewriter manuscript.

memoir also hard code the monospace font to be cmtt, se line 12643 in the memoir.cls. If you want another monospaced font, you need to change at least the lines:

\renewcommand{\familydefault}{cmtt}
\renewcommand{\rmdefault}{cmtt}
\renewcommand{\sfdefault}{cmtt}
\renewcommand{\bfdefault}{m}
\renewcommand{\itdefault}{n}
\renewcommand{\sldefault}{n}
\renewcommand{\scdefault}{n} 

Changing to scaled luximono:

\usepackage[scaled=0.85]{luximono}
\input{glyphtounicode}
\pdfgentounicode=1
\renewcommand{\familydefault}{ul9}
\renewcommand{\rmdefault}{ul9}
\renewcommand{\sfdefault}{ul9}
\renewcommand{\bfdefault}{m}
\renewcommand{\itdefault}{n}
\renewcommand{\sldefault}{n}
\renewcommand{\scdefault}{n}

I also recommend the package csquotes to ensure correct quoting marks.

EDIT: I forgot that you preferred XeLaTeX and it sems that memoir and the ms-option does not work with XeLaTeX.

Here is a new (near) MWE:

\documentclass[english,ms,11pt]{memoir}
  \usepackage{babel}
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenx}

  \usepackage{lmodern}
% Comment the line above and uncomment the following
% ten line to change monotype font.

%\usepackage[scaled=0.85]{luximono}
%\input{glyphtounicode}
%\pdfgentounicode=1
%\renewcommand{\familydefault}{ul9}
%\renewcommand{\rmdefault}{ul9}
%\renewcommand{\sfdefault}{ul9}
%\renewcommand{\bfdefault}{m}
%\renewcommand{\itdefault}{n}
%\renewcommand{\sldefault}{n}
%\renewcommand{\scdefault}{n}

\usepackage[autostyle=true]{csquotes} 
\usepackage{lipsum,kantlipsum} 

\begin{document}

\lipsum[2]

\end{document}

Computer Modern

Computer Modern

Scaled Luximono
enter image description here

5
  • 1
    I don't use French; I just mentioned the \frenchspacingcommand because it removes excess space after punctuation. That excess space wasn't an exact multiple of the character width.
    – Daniel H
    Sep 9, 2012 at 18:01
  • The problem appears to be with fontspec;it works fine when that package is not included (but only in Computer Modern, of course); as CM is not an option due to lack of bold support in CMTT, I'll try it with PDFLaTeX
    – Daniel H
    Sep 11, 2012 at 0:01
  • In both PDFLaTeX and XeLaTeX, memoir seems to resist all attempts to change the font. I'll see if it provides its own functionality for doing so, but I doubt I'll have much success. If I need some sort of magic configuration options to make memoir allow me to change the font, could you please put them in your answer?
    – Daniel H
    Sep 11, 2012 at 0:10
  • After further experimentation, the ms option of memoir seems like it would be perfect, except that it does not seem to render in any font other than Computer Modern Typewriter no matter what I try. I have not been able to find other people complaining about this issue or saying that it does not exist. If nothing else turns up, I will probably see if I can reverse engineer what they do in memoir.
    – Daniel H
    Sep 11, 2012 at 4:19
  • It's still not allowing me to change the font; are you sure the \usepackage{lmodern} is doing anything on your setup? If I can't get it to work, reverse engineering memoir's ms mode seems like the best option, but I would prefer not to do that because I almost certainly don't understand LaTeX internals enough.
    – Daniel H
    Sep 11, 2012 at 18:06
12

Monospaced fonts in the "old TeX world" usually don't allow for stretching and shrinking spaces. Here's the beginning of cmtt10.pl, which is the human readable version of the metric file cmtt10.tfm:

(FAMILY CMTT)
(FACE O 352)
(CODINGSCHEME TEX TYPEWRITER TEXT)
(DESIGNSIZE R 10.0)
(COMMENT DESIGNSIZE IS IN POINTS)
(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)
(CHECKSUM O 33772436170)
(FONTDIMEN
   (SLANT R 0.0)
   (SPACE R 0.524996)
   (STRETCH R 0.0)
   (SHRINK R 0.0)
   (XHEIGHT R 0.430555)
   (QUAD R 1.049991)
   (EXTRASPACE R 0.524996)
   )

Similarly for pcrr8t.pl (used when \fontfamily{pcr} is selected, that is, Courier):

(FAMILY UNSPECIFIED)
(FACE F MRR)
(CODINGSCHEME EXTENDED TEX FONT ENCODING - LATIN)
(DESIGNSIZE R 10.0)
(COMMENT DESIGNSIZE IS IN POINTS)
(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)
(CHECKSUM O 2142215632)
(FONTDIMEN
   (SLANT R 0.0)
   (SPACE R 0.6)
   (STRETCH R 0.0)
   (SHRINK R 0.0)
   (XHEIGHT R 0.425989)
   (QUAD R 1.0)
   (EXTRASPACE R 0.6)
   (PARAMETER D 8 R 0.562)
   (PARAMETER D 9 R 0.628992)
   (PARAMETER D 10 R 0.75299)
   (PARAMETER D 11 R 0.156995)
   (PARAMETER D 12 R 0.804993)
   (PARAMETER D 13 R 0.25)
   (PARAMETER D 14 R 0.6)
   (PARAMETER D 15 R 0.0)
   (PARAMETER D 16 R 1.2)
   )

Thus some input such as

\setbox0=\hbox spread 3pt{\ttfamily abc def}

(which is a miniature example of how TeX does justification stretching or shrinking interword spaces) results in the message

Underfull \hbox (badness 10000)

because there's no stretching in the spaces when \ttfamily is in force.

The situation is completely different in XeTeX, since the monospaced fonts I checked do allow for interword space stretching and shrinking: Latin Modern Mono, TeX Gyre Cursor, Courier. Let's make a comparison at the standard 10pt size

  • TeX Gyre Cursor: 6pt plus 3pt minus 2pt
  • Latin Modern Mono: 5.25pt plus 2.625pt minus 1.75pt
  • Courier: 6.00096pt plus 3.00047pt minus 2.00032pt
  • CMU Typewriter Text: 5.25pt plus 2.625pt minus 1.75pt

The data tell the normal width of the interword space and the amount of stretching and shrinking available.

Let's do a comparison, compiling the following document first with pdflatex and then with xelatex

\documentclass[draft]{article}
\usepackage{ifxetex}
\ifxetex
  \usepackage{fontspec}
  \setmonofont{Latin Modern Mono}
\else
  \usepackage{lmodern}
\fi
\usepackage{lipsum}
\textwidth=10cm
\begin{document}
\ttfamily
\noindent\lipsum[2]
\end{document}

Here's the result with pdflatex

enter image description here

Here's the result with xelatex

enter image description here

(The different size is due to how the images have been produced, they are just the same size in print.)

We can notice a big difference. In the first case the engine isn't able to do proper justification, in the second image the text appears justified and hyphenated.

Conclusion

If you need to reproduce a document prepared with a typewriter, the setup

\documentclass{article}

\usepackage{fontspec}
\usepackage{ragged2e}
\setmainfont[Ligatures=TeX]{Latin Modern Mono}% choose another one if you prefer

\begin{document}
% Uncomment the following line for standard spaces after periods
%\frenchspacing
% Uncomment the following line for ragged right setup
%\setlength{\RaggedRightRightskip}{0pt plus 4em}\RaggedRight

with XeLaTeX seems the best option.

Here's the result when applying both \frenchspacing and \RaggedRight:

enter image description here

7
  • 2
    Wouldn't it be more authentic to a typewriter not to use Ligatures=TeX? Or are there reasons to use them anyway?
    – cgnieder
    Sep 9, 2012 at 13:39
  • @cgnieder Possibly. That's only in order to make input of dashes easier, but in a fixed width font one should use only a hyphen. Maybe for ¡ and ¿, depending on the keyboard facilities.
    – egreg
    Sep 9, 2012 at 13:41
  • This has at least the problem of the extra spaces after the period (see especially the alignment of the is pretium on the third line and aliquet on the fourth.
    – Daniel H
    Sep 9, 2012 at 18:07
  • @DanielH It depends on the typewriting school; I'll add the result from applying \RaggedRight and \frenchspacing
    – egreg
    Sep 9, 2012 at 19:32
  • @egreg Hi Egreg. Do you know if it is possible with Xelatex to have a « monospaced justification ». I mean not a stretching space between words but a monospace space to have justified text and hyphenation but each lines and letters aligned in a vertical grid...
    – krshk
    Jul 10, 2016 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .