136

This question led to a new package:
typewriter

I'm giving my students an old exam and barely remembered to change the date at the top. A colleague and I joked about how long we could reuse the same exam, and it made me wonder if I could use LaTeX to create an exam that looked like it was written in 1963 on a typewriter.

Here's what I'd like to see:

  1. Typewriter font even in math mode. (Several ways to accomplish this; is there a "best" way?)

  2. Slight inconsistencies in the printing of each letter. I attempt this below adapting code taken from here. Each word has a random vertical shift, though I'd like to see it reduced to each letter having its own vertical shift. Other typewriter-like irregularities would be great.

  3. It would be great if math-mode delimeters \left(, \right}, etc., can be created in a piece-meal fashion as they did in days of old.

  4. Fraction bars, tops of square-root symbols -- any horizontal line, really -- is made up of a series of en-dashes that don't quite match up.

Here is what I've come up with so far:

\documentclass[10pt]{article}% This is a document class providing more font size options
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage[textwidth=7.5in,textheight=9.5in]{geometry}
\pagestyle{empty}

% thanks to Bruno Le Floch: https://tex.stackexchange.com/q/9331/4012
% and in his comments to https://tex.stackexchange.com/a/29458/4012
\usepackage[first=-1,last=1]{lcg}% you can play around with these values
\makeatletter
\newcommand{\globalrand}{\rand\global\cr@nd\cr@nd}
\makeatother

\newcommand{\randomvshift}[1]{\globalrand\raisebox{\value{rand}pt}{#1}}

%%% thanks to Martin Scharrer: https://tex.stackexchange.com/q/11598/4012
\makeatletter
\def\typewriter#1{%
    \@typewriter#1 \@empty
}
\def\@typewriter#1 #2{%
   \randomvshift{#1}\space
   \ifx #2\@empty\else
    \expandafter\@typewriter
   \fi
   #2%
}
\makeatother
\begin{document}\tt

\typewriter{Math 101 Fall 1963}

\begin{enumerate}
\item   \typewriter{$\frac{\texttt{d}}{\texttt{dx}}\texttt{(x)}^2$}

\item \typewriter{State the Fundamental Theorem of Calculus.}

\end{enumerate}

\end{document}
CarLaTeX
  • 62,716
GregH
  • 3,499
  • 3
    Did people typset math with a typewriter in 1963? I only know math texts from that time typed with a typewriter where all math symbols added carefully handwritten. So maybe all math should simulate handwritten math symbols (also with slight inconsistencies). – student Dec 15 '16 at 16:18
  • 1
    I'm a bit young for this (but don't often get to say that any more). But wouldn't you want the vertical offset to be consistent for each character? Just checked somethign and it does seem so (in the example I found every "y" was above the baseline by about 0.1ex) – Chris H Dec 15 '16 at 16:19
  • 1
    @student -- there did exist gadgets that provided extra "keys" with symbols to avoid (most) handwritten inclusions. see the section on "typewriter" composition at http://tex.stackexchange.com/a/244126 – barbara beeton Dec 15 '16 at 16:29
  • @ChrisH Yes, it would be best if all "e's" had the same shift. I'd love that. – GregH Dec 15 '16 at 17:24
  • 2
    @student I used 1963 because it is well known around here that our school president graduated from here in 1963. But yes, that may be going back too far. I have a Springer text, copyright 1977, that is all typewriter-written. – GregH Dec 15 '16 at 17:26
  • I have a 1997 data book on my desk that's typewritten with handwritten Greek letters. – Chris H Dec 15 '16 at 17:30
  • See http://tex.stackexchange.com/q/122970/5763 – Martin Schröder Dec 16 '16 at 13:47
  • 1
    Perhaps someone could provide a definition for mimeograph purple. Only the stencil would come from the typewriter itself. – dedded Dec 16 '16 at 21:21
  • anyway to deactivate it and reactivate in one document? like switch on and off for certain parts? –  May 26 '21 at 10:58

1 Answers1

190

This is now available as the typewriter package on ctan and texlive etc


Improved version with some Greek and mathematics, and avoiding small numbers being written using 2e-5 notation into the pdf (and crashing the pdf reader) This version assumes that the CM Unicode opentype fonts are available.

2nd update now works with texlive 2016 stable release, and also a texlive 2015 updated to luatex 0.95 (Previous version only seemed to work in a texlive 2017 test release).

Slightly updated code available as a package here

https://github.com/davidcarlisle/dpctex/tree/master/typewriter

enter image description here

\documentclass{article}

% luaotfload exlicitly loaded for latex formats before 2017/01/01 \usepackage{luaotfload}

% load cmuntt here not from lua (for everyone except me, it seems) \font\cmuntt = cmuntt at 12pt \cmuntt \edef\cmunttid{\fontid\cmuntt}

\expandafter\let\expandafter%\csname @percentchar\endcsname \directlua{ local cbl = luatexbase.callback_descriptions('define_font') % print('\string\n======' .. cbl[1] .. '===\string\n') original_fontloader = luatexbase.remove_from_callback('define_font', cbl[1]) luatexbase.add_to_callback( 'define_font', function(name, size, i) if (name=='cmtt10x') then % this works in my dev version but for older setups % make sure cmuntt.otf has been loaded before we mess % up the font loader. % f = original_fontloader('cmuntt.otf',size) f = font.getfont(\cmunttid) f.name = 'cmtt10x' f.type = 'virtual' f.fonts = {{name = 'cmuntt', size = size}} for j, v in pairs(f.characters) do local gr = 0.4math.random() local gr2 = 0.4math.random() v.commands = { { 'lua', 'r1 = 0.01*math.random(-10, 10) pdf.print(string.format( " q %f %f %f %f 0 0 cm ", math.cos(r1), - math.sin(r1), math.sin(r1), math.cos(r1) ))' }, {'special', 'pdf: ' .. gr2 .. ' g'}, {'push'}, {'right', math.random(-20000,20000)}, {'down', math.random(-20000,20000)}, {'char', j}, {'pop'}, {'lua', 'pdf.print(" Q ")'}, {'down', math.random(-20000,20000)}, {'special', 'pdf: ' .. gr .. ' g'}, {'char', j}, {'special', 'pdf: 0 g'} } end % end of for return f else return original_fontloader(name, size, i) end % end of if end, % end of function 'define font' ) % end of add_to_callback }

\def\sqrt#1{^^^^221a\overline{#1}}

\begin{document} $\relax$

\font\myfont= cmtt10x at 12pt \myfont \font\myfonts= cmtt10x at 7pt \let\selectfont\relax

\textfont0=\myfont \scriptfont0=\myfonts \scriptscriptfont0=\myfonts \textfont1=\myfont \textfont2=\myfont \textfont3=\myfont

\section{Introduction}

ABCD one two theee four five

TTTTTTTooooooWWWWW

\begin{enumerate} \item red yellow blue green \item black blue purple \end{enumerate}

[some greek text θ]

and in math $x^2-\cos θ$

[\left(\frac{x^2}{\sqrt{1+y}}\right)]

\raggedleft typeset by egreg design services \end{document}


Original:

enter image description here

This introduces randomness on every use of each letter of the document. It requires luatex. (math is possible but not yet)

\documentclass{article}

\directlua { local cbl = luatexbase.callback_descriptions('define_font') original_fontloader = luatexbase.remove_from_callback('define_font', cbl[1]) luatexbase.add_to_callback( 'define_font', function(name, size, i) if (name=='cmtt10x') then f = font.read_tfm('cmtt10', size) f.name = 'cmtt10x' f.type = 'virtual' f.fonts = {{name = 'cmtt10', size = size}} for j, v in pairs(f.characters) do local gr = 0.4math.random() local gr2 = 0.4math.random() v.commands = { {'lua', ' r1 = 0.05+0.1*math.random() pdf.print (" q " .. math.cos(r1) .. " " .. - math.sin(r1) .. " " .. math.sin(r1) .. " " .. math.cos(r1) .. " 0 0 " .. " cm " ) ' }, {'special', 'pdf: ' .. gr2 .. ' g'}, {'push'}, {'right', math.random(-30000,30000)}, {'down', math.random(-30000,30000)}, {'char', j}, {'pop'}, {'lua', 'pdf.print(" Q ")'}, {'down', math.random(-30000,30000)}, {'special', 'pdf: ' .. gr .. ' g'}, {'char', j}, {'special','pdf: 0 g'} } end % end of for return f else return original_fontloader(name, size, i) end % end of if end, % end of function 'define font' ) }

\begin{document}

\font\myfont= cmtt10x at 12pt \myfont \let\selectfont\relax

\section{Introduction}

ABCD one two theee four five

TTTTTTTooooooWWWWW

\begin{enumerate} \item red yellow blue green \item black blue purple \end{enumerate}

\raggedleft typeset by egreg design services \end{document}

This emulates a really poor typewriter with dodgy print hammers that wobble and move around as each key is hit:-)

muzimuzhi Z
  • 26,474
David Carlisle
  • 757,742
  • 17
    My ”tesi di laurea“ was typed with a much better typewriter. – egreg Dec 16 '16 at 09:01
  • 10
    Epic indentation is epic. – Henri Menke Dec 16 '16 at 09:04
  • 3
    Man, had I been given an exam subject like this, I would have quit math. Oh wait, I did. – Right leg Dec 16 '16 at 09:12
  • @egreg math added for you – David Carlisle Dec 16 '16 at 10:53
  • 35
    MY THESIS WILL USE THIS. – Paulo Cereda Dec 16 '16 at 11:10
  • 4
    @DavidCarlisle, is there a way to control how bad the typewriter is? I mean, egreg's design services has a really bad old typewriter, poorly maintained. But maybe carlisle's design services has an old one too, but a good one. ;) – Guilherme Zanotelli Dec 16 '16 at 11:33
  • 2
    @GuilhermeZanotelli yes see the arbitrary scale factors on the random number gen eg the rotation of one of the over-printed characters is r1 = 0.01*math.random(-10,10) make that 0.001* and you wil have a tenth of the angle in each case, while debugging I had it as 1* just to check something is happening (but you can't read it all then) similarly the two random numbers used for gray colour are set to arbitrary values. 0 is black 1 is white so you can limit the range and make that range blacker or whiter – David Carlisle Dec 16 '16 at 11:50
  • 2
    @GuilhermeZanotelli my thesis (1985) was set on an IBM golfball typewriter so had no wobbly print hammers at all. – David Carlisle Dec 16 '16 at 11:50
  • @DavidCarlisle -- are you saying the golf ball wasn't wobbly? – barbara beeton Dec 16 '16 at 13:07
  • How to compile this? I tried it with lualtex, but it resulted in ! LuaTeX error [\directlua]:1: attempt to concatenate field '?' (a nil value) stack traceback: [\directlua]:1: in main chunk. – Julia Dec 16 '16 at 13:26
  • Sigh - looks great but I also can't get to run on my Windows machine. I get \myfont=cmtt10x at 12pt not loadable: metric data not found or bad. Thought that I had the computer modern fonts installed, but I also don't know how to confirm. Any help out there? (Running LuaTeX Version 0.95.0.) – GregH Dec 16 '16 at 13:51
  • 2
    Absolutely beautiful -- except superscript size needs to be the same as the text size. – Scott Seidman Dec 16 '16 at 13:58
  • 3
    @ScottSeidman see comment above, I had (access to) a fancy typewriter:-) – David Carlisle Dec 16 '16 at 13:59
  • Got it! How about erasure smears and white-out outlines? I had the fancy white-out film that you typed on. – Scott Seidman Dec 16 '16 at 14:01
  • @DavidCarlisle I have tried it. Got different error this time: Font cmuntt.otf not found.' Looked online for a copy of this font, placedcmuntt.otfin the same directory as my file. Now I get the oldFont \myfont=cmtt10x at 12pt not loadableagain. I did back up into my log file and found I also have the commentattempt to index global 'f' (a nil value). ` error. – GregH Dec 16 '16 at 15:08
  • Not here - but my TL2015 apparently has luatex v0.80.0 – Chris H Dec 16 '16 at 16:12
  • @GregH answer updated with a version tested with luatex 0.95 as in public texlive 2016 . – David Carlisle Dec 16 '16 at 20:54
  • 1
    @DavidCarlisle, will do though I'm away from that machine for the weekend and can't remember what versions I've got on my personal machines. – Chris H Dec 16 '16 at 21:07
  • 1
    @DavidCarlisle -- I was able to typeset your top example with TL 2016 and current updates. Nice, typically devious, work. To my eye the minus sign and the horizontal rules for the square root look a little thin, though. :) – musarithmia Dec 16 '16 at 22:02
  • @musarithmia fix for that on the way:-) – David Carlisle Dec 16 '16 at 22:57
  • 1
    @DavidCarlisle The update works for me, marked this as the "correct" answer, though would enjoy other's take, too. FWIW, commands such as \pi don't display for me (no errors, no blank space either) but I can copy/paste a pi symbol (from MS Word). This worked too for a theta, though capital Sigma for summations didn't render; \sum renders as a capital P. Overall: Well done & thanks! – GregH Dec 17 '16 at 11:58
  • @GregH \sum will work if you get the version from github as will all the greek commands \muppi from unicode-math – David Carlisle Dec 17 '16 at 12:06
  • @DavidCarlisle How can one add characters that are affected by your code? I defined an "underline leader" to mimic underlining by repeatedly pressing "_", via \def\underlineleader{\leaders\hbox to .35em{\hss\_\hss}\hfill\ }. However, the lines are "too nice"; they are typeset regularly, rather than with the "typewriter." It would be fun to typeset all vertical & horizontal rules in a table in a similar fashion. – GregH Dec 17 '16 at 14:52
  • 1
    @GregH I'll add some more bits to the typewriter package on github. use \string_ not \_ see the version of \overline I put there already – David Carlisle Dec 17 '16 at 15:00
  • 1
    @GregH package in github has support for tables and usual commands such as \alpha makes this output – David Carlisle Dec 17 '16 at 16:13
  • 2
    @DavidCarlisle The tables are perfect. (I wonder if Knuth is chuckling "I created TeX to get away from this very look...") – GregH Dec 17 '16 at 18:57
  • @DavidCarlisle I tried the version from github which worked very well. My lualatex is 0.80.0. – Julia Dec 18 '16 at 17:18
  • @Julia thanks for letting me know:-) – David Carlisle Dec 18 '16 at 18:34
  • The couple of minor tweaks that I would suggest, (remembering back to when it wasn't unusual to see internal exams prepared this way), would be that where Greek letters were made with over-typing, e.g. Theta would be 0- there was often variability of the (miss)alignment of the two components both vertically and horizontally. Bold was often attempted with double typing. Also some typewriters had a) some damaged hammers and poor paper alignment/retention. I love the effort that has gone into this! – Steve Barnes Dec 21 '16 at 08:07
  • Is there a way to simulate typewriter exponents that are full-sized characters typed on a line above? – Evpok Feb 09 '18 at 11:45
  • @Evpok probably:-) (not today though) – David Carlisle Feb 09 '18 at 12:20
  • I even discovered a typo. – AlexG Feb 14 '18 at 11:24
  • Strangely, I can't compile a simple file with LuaTeX, Version 1.0.4 (TeX Live 2017) on Ubuntu 16.04. I have installed the oft versions of the fonts, run fc-cache -f -v and I still get the error Font \cmuntt=file:cmuntt.otf at 12pt not loadable: metric data not found or bad. \font\cmuntt = file:cmuntt.otf at 12pt \cmuntt. Suggestions as to what I should try? – thymaro Feb 15 '18 at 13:48
  • @thymaro cmuntt.otf is the typewriter font from the Computer Modern Unicode collection. You need to install cmunicode in texlive or miktex, whichever you use. – David Carlisle Feb 15 '18 at 13:53
  • hmm, the error persists, even after rebuilding formats and updating the fmdb. LuaLaTeX says the error is on line 28 in typewriter.sty \font\cmuntt = file:cmuntt.otf at 12pt \cmuntt – thymaro Feb 15 '18 at 14:00
  • @thymaro it is nothing to do with the format, do you have cmuntt.otf installed? If you don't have it, luatex can not use it, it should be at a location such as /usr/local/texlive/2017/texmf-dist/fonts/opentype/public/cm-unicode/cmuntt.otf – David Carlisle Feb 15 '18 at 14:12
  • that's exactly where I found it. – thymaro Feb 15 '18 at 14:21
  • 1
    sorry can't really help (and comments under this question not a good format) make a simple luatex document using cmuntt.otf either directly or via fontspec (no need for the typewriter package) and if it doesn't load the font you could ask a new question about that with a log file etc that people can debug – David Carlisle Feb 15 '18 at 14:24
  • I've taken the liberty of assigning better pretty-printing to the Lua code block. Feel free to revert. – Mico Apr 02 '18 at 20:49
  • @Mico That inserts code window breaks though – percusse Apr 02 '18 at 21:05
  • @percusse - What breaks? – Mico Apr 02 '18 at 21:06
  • @Mico Now the code is broken into 3 pieces – percusse Apr 02 '18 at 21:07
  • @Mico I think I'll revert, as percusse says this means you can't copy the text in one operation, – David Carlisle Apr 02 '18 at 21:14
  • Is possible to introduce (micro)-randomness in common Computer Modern family? Small variations in serif for example and similar. I am not a typographer, but according to some university course on math typsetting, there is goood reason for using these imperfections. Unfortunately, I have never found any article about the topic. Are there any? – struct Jul 14 '18 at 09:27
  • @JanFilip not yet but as support for the (new) opentype variable font features start to appear, then perhaps. – David Carlisle Jul 15 '18 at 10:54
  • Is there a way how to get it when its released? I mean if there is some channel which I have to follow to get news about it? – struct Jul 15 '18 at 11:28
  • @JanFilip you say "it" as if there was some specific thing, I just meant that someone could perhaps write something, so the same places as any tex news: here, the ctan-announce mailing list, etc – David Carlisle Jul 15 '18 at 11:59
  • I am not used to LuaTex, but I love this font. Is it easy to port to / use within xetex? – Tim Feb 15 '19 at 02:58
  • 1
    @Tim no not possible at all, sorry, this is using Lua to programmatically distort the characters on each use – David Carlisle Feb 15 '19 at 07:46
  • Thanks for your package! However, I have downloaded the newest version and \in, \sum, \subseteq, \oplus seems not to work. Would you do me a favor to add a minimal working example to these symbols? – Asigan Dec 07 '23 at 06:56