使用 LaTeX 模拟老式打字机书写的文本

使用 LaTeX 模拟老式打字机书写的文本

这个问题导致了一个新的方案的出现:
typewriter

我给学生们出了一份旧试卷,但几乎忘了更改顶部的日期。我和一位同事开玩笑说我们可以重复使用同一份试卷多久,这让我想知道我是否可以使用 LaTeX 创建一份看起来像是 1963 年用打字机写的试卷。

以下是我希望看到的:

  1. 即使在数学模式下,打字机字体也是如此。(有几种方法可以实现这一点;有没有“最佳”方法?)

  2. 每个字母的打印略有不一致。我尝试在下面改编代码这里。每个单词都有随机的垂直位移,但我希望将其减少到每个字母都有自己的垂直位移。其他类似打字机的不规则性会很棒。

  3. 如果数学模式分隔符\left(\right}等可以像过去一样以零碎的方式创建,那就太好了。

  4. 分数线,即平方根符号的顶部 - 实际上是任何水平线 - 都是由一系列不太匹配的短破折号组成。

以下是我目前想到的:

\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}

答案1

它现在可作为typewriterctan 和 texlive 等软件包使用


改进的版本包含一些希腊语和数学内容,并避免使用 2e-5 符号将小数字写入 pdf(并导致 pdf 阅读器崩溃)此版本假定 CM Unicode opentype 字体可用。

第二次更新现在适用于 texlive 2016 稳定版本,并且 texlive 2015 已更新至 luatex 0.95(以前的版本似乎仅在 texlive 2017 测试版本中运行)。

此处提供稍微更新的代码包

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

在此处输入图片描述

\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.4*math.random()
                    local gr2 = 0.4*math.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}

原来的:

在此处输入图片描述

这会在文档中每个字母的每次使用中引入随机性。它需要 luatex。(数学是可行的,但目前还不行)

\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.4*math.random()
                    local gr2 = 0.4*math.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}

这模拟了一个真的很穷打字机的打印锤不太稳定,每次敲击键都会摇晃并移动:-)

相关内容