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.

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

| improve this answer | |
  • 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 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 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 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.