6

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.

  • 1
    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 – Chris H Mar 11 '20 at 14:16
  • Not sure why the typewriter one wasn't merged at the time but an older typewriter link is https://tex.stackexchange.com/questions/344214/use-latex-to-simulate-old-typewriter-written-texts – David Carlisle Mar 11 '20 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 – Chris H Mar 11 '20 at 15:11
  • 1
    @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 – David Carlisle Mar 11 '20 at 15:13
  • @DavidCarlisle and ChrisH, thank you for your answers, but neither of the three examples change the font of the character, so each "e" still is basically the same throughout the whole document. – Niels Wer Mar 11 '20 at 15:19
  • @NielsWer the point is they do something to each character, in those cases changing the grey level and rotating a bit but the same basic mechanism can change what is done, to select a different font, all the work of splitting up the words and acting on each character is the same – David Carlisle Mar 11 '20 at 15:25
  • 1
    Have you seen https://tex.stackexchange.com/q/29425 ? Studying fonts with this feature may help you. – Thérèse Mar 11 '20 at 16:13
  • Without your fonts we're not going to get very far with testing the latest version. I have an idea that works slightly differently – Chris H Mar 12 '20 at 10:25
  • I have a solution that selects a random font for each character, but I'm having trouble with the accented characters – Chris H Mar 12 '20 at 11:27
  • @ChrisH With my tire it is impossible to copy the text from the pdf, the letters are all random. And with your solution it most likely would be easier to change the Metafont characters, since one only has one document, not about 30. I would greatly appreciate to see your solution. – Niels Wer Mar 12 '20 at 12:01
  • @NielsWer I ended up having to rewrite it to parse the German characters. – Chris H Mar 12 '20 at 12:25

2 Answers2

3

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{tgadventor}
\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: output of above LaTeX code, with each character in a different font

Chris H
  • 8,705
  • With my selection of default fonts it looks rather like a ransom note; hopefully yours will sit better together. And as for the keming... – Chris H Mar 12 '20 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? – Niels Wer Mar 13 '20 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 – Chris H Mar 13 '20 at 11:29
0

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.

Don Hosek
  • 14,078