# Randomized fonts for handwritten look

I created a Metafont font with a randomizer, such that each time I compile the font, it looks different. Using those fonts, I like to create handwritten looking parts in my document. I would like to use 10 to 20 different versions of my font, and each letter randomly chooses on of the fonts. Without the randomized fonts all the e's, for example, would look the same.

Three options how this might be possible come to my mind:

1. Choose a font randomly for each new letter in the document.
2. Create hundert fonts, each with only a single letter in a many variations. For each letter in the document, choose a random letter from the corresponding font.
3. Load the fonts in turn, the first character, the first font, second character the second font... and start with the 21st character from the beginning again.

I tried to use the options above, but I wasn't competent enough to get result.I use pdfLaTex on Overleaf, but if it only works with LuaLaTeX or XeLaTex, that would be great, too.

I am German, so the letters ä, ö, ü, ß, Ä, Ö, Ü (as direct imput) and € (as \euro) should also be possible to use.

EDIT:

I wrote a program using the links provided in the comments. They work fine with the normal letters, but the Umlaute don't seem to work. Below a minimal working example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{datatool}
\usepackage{pgffor}
\usepackage{pgfmath}
\usepackage{pgffor}
\usepackage{pgfmath}

\def\klein{% random non capital letter
\pgfmathrandominteger{\mytemp}{1}{26}%
\foreach\x[count=\xi] in{a,...,z}{\ifnum\xi=\mytemp\x\breakforeach\fi}%
}

\def\gross{% random capital letter
\pgfmathrandominteger{\mytemp}{1}{26}%
\foreach\x[count=\xi] in{A,...,Z}{\ifnum\xi=\mytemp\x\breakforeach\fi}%
}

\newfont{\fontA}{A}     % includes A a
\newfont{\fontB}{B}     % includes B b
\newfont{\fontAE}{AE}   % includes Ä ä
\newfont{\fontOE}{OE}   % includes Ö ö
\newfont{\fontUE}{UE}   % includes Ü ü
\newfont{\fontSS}{SS}   % includes ß

\makeatletter
\newcommand{\Buchstabe}[2]{\csdef{BT#1}{{#2}}}
\newcommand{\Bs}[1]{\ifcsdef{BT\expandafter#1}{\csuse{BT#1}}{#1}}
\def\Handschrift#1{%
\@Handschrift#1 \@empty
}
\def\@Handschrift#1 #2{%
\zz{#1}\space
\ifx #2\@empty\else
\expandafter\@Handschrift
\fi
#2%
}
\def\zz#1{\def\zzsep{}\zzz#1\relax}
\def\zzz#1{\ifx\relax#1\else\Bs{#1}\expandafter\zzz\fi}
\makeatother

\Buchstabe{A}{\fontA \gross}
\Buchstabe{B}{\fontB \gross}
\Buchstabe{Ä}{\fontAE \gross}
\Buchstabe{Ö}{\fontOE \gross}
\Buchstabe{Ü}{\fontUE \gross}

\Buchstabe{a}{\fontA \klein}
\Buchstabe{b}{\fontB \klein}
\Buchstabe{ä}{\fontAE \klein}
\Buchstabe{ö}{\fontOE \klein}
\Buchstabe{ü}{\fontUE \klein}
\Buchstabe{ß}{\fontSS \klein}

\begin{document}
\Handschrift{AB ab} % works fine

\Handschrift{äöüß ÄÖÜ} % Does not work

\end{document}


I also tried to use "a and \"a in both \Handschrift{} and \Buchstabe{}, only with the error messages.

• Related: How do I make my document look like it was written by a Cthulhu-worshipping madman?, also my answer to How to simulate old typewriter?, however neither of these picks a random glyph. Instead they do (random) things to a fixed set of glyphs Mar 11, 2020 at 14:16
• Not sure why the typewriter one wasn't merged at the time but an older typewriter link is tex.stackexchange.com/questions/344214/… Mar 11, 2020 at 14:26
• @DavidCarlisle IIRC the author of the 2nd question was originally trying to avoid lua, hence my much simpler (and much more limited) approach. At least that's why I linked the one with the pdflatex answer Mar 11, 2020 at 15:11
• @ChrisH sure, for the link now but I'm surprised the original questions were not merged, the first didn't need a luatex answer I just happened to use one, so your pdftex answer could have been usefully added as an alternative answer as the questions are basically the same Mar 11, 2020 at 15:13
• Have you seen tex.stackexchange.com/q/29425 ? Studying fonts with this feature may help you. Mar 11, 2020 at 16:13

This selects a font at random for each letter, from a list of loaded fonts and their short names, taken from Overleaf. You would need to replace this list with your own.

The parsing method is David Carlisle's taken from this answer about making the censor package handle German characters (hence the dependency on censor, which I should be able to reduce to a dependency on ifnextok but I don't currently have time

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{lmodern}
\usepackage{tgtermes}
\usepackage{tgpagella}
\usepackage{tgbonum}
\usepackage{tgschola}
\usepackage{mathptmx}
\usepackage{utopia}
\usepackage{palatino}
\usepackage{bookman}
\usepackage{charter}
\usepackage{tgheros}
\usepackage{helvet}
\usepackage{tgcursor}
\usepackage{courier}
\usepackage{censor}

\makeatletter

\DeclareTextFontCommand{\textrndfont}{\rndfont}
\newcommand*{\rndfont}{\pgfmathrandomitem\z{fontlist}\fontfamily{\z}\selectfont}
\pgfmathdeclarerandomlist{fontlist}{{lmr}{lmdh}{qtm}{qpl}{qbk}{qcs}{ptm}{put}{ppl}{pbk}{bch}{cmss}{lmss}{qag}{qhv}{phv}{cmtt}{lmtt}{qcr}{pcr}}

\def\stringend{$} \def\stringend{$} %repeated to avoid confusing my editor's syntax highlighting

\long\def\randomfont#1{%
\protected@edef\tmp{#1}%
\def~{-}\expandafter\random@Block\tmp\stringend\let~\sv@tilde}
\long\def\random@Block{\IfNextToken\stringend{\@gobble}%
{\IfNextToken\@sptoken{ \bl@t{\random@Block}}%
{\bl@t{\random@Block}}}}

\long\def\bl@t#1#2{%
\ifx\bpar#2\let\next\par\else\def\next{\textrndfont{#2}}\fi
\next#1}

\makeatother

\begin{document}

\randomfont{This text should be in random fonts äöüß.  It's quite long and should line wrap.  I wonder what happens if it has a double blank line in it.

Let's find out whether it starts a new paragraph at the beginning of this sentence. Yes}

\randomfont{This text should be in random fonts äöüß}\par

\end{document}


And here's the output:

• With my selection of default fonts it looks rather like a ransom note; hopefully yours will sit better together. And as for the keming... Mar 12, 2020 at 12:17
• I use \newfont{\myfont}{myfont} to load a new font in LaTeX, but with this method I don't seem to be able to load the fonts in the randomisiere. I can only load one font in the randomizer, and then, instead of just the letter, it outputs /m/n/15 before it. And it outputs the errors: Missing \endcsname inserted. and Extra \endcsname. Do you know what I need to change? Mar 13, 2020 at 10:14
• Font packaging and loading isn't really my area of expertise, but I don't think \newfont is the way to go Mar 13, 2020 at 11:29

Since you're using MF to generate your font, you could use TeX's native ligature/character substitution to use alternate versions of letters based on context, e.g., a different e might be used whether it follows t or h. It won't quite give you complete randomness since every instance of, e.g., te will look the same.