# Randomly reusing a custom font for the same letters (On first iteration use custom font no 1, on second iteration use custom font no 2, and so on)

I have some custom fonts that I have imported to my Font Book (I am using a macOS machine and am using XeTeX).

I can import those custom fonts with the answer written in Package for changing font with each letter?, but in that answer, it seems that a letter would use a custom font, but it doesn't change when the letter is repeated. I am looking for an answer that would change a letter as its use is iterated.

What do I mean by iterated? Say we are using Helvetica, Courier, and Comic Sans MS. And we are trying to write the sentence "Lorem amet consectetur elit".

• The e in Lorem would use the Helvetica font
• The e in amet would use the Comic Sans MS font
• The e in consectetur would use the Courier font
• The e in elit would use the Helvetica font

The order does not have to follow [Helvetica, Courier, and Comic Sans MS], I would like it to be random. In other words:

• The e in Lorem could use the Comic Sans MS font
• The e in amet could use the Courier font
• The e in consectetur could use the Helvetica font
• The e in elit could use the Courier font

is viable. What I aim to achieve is a document in which the letters of a sentence would randomly use a font style for a letter, and would change as the letter is used again. It does not follow a rigid way of using fonts.

It seems that that case has been answered in Randomized fonts for handwritten look (see the example image, particularly the äöüß part.)

With those answers, I tried to integrate the first answer (fontspec import ttf) into the second answer but it doesn't seem to work out, and would instead just use the default font.

In that image above, is the result of my integration of those two answers. I did \usepackage{fontspec}, \setmainfont{My-Custom-Font}, and set \pgfmathdeclarerandomlist{fontlist}{{lmr}{lmdh} into \pgfmathdeclarerandomlist{fontlist}{{my-custom-font}{lmdh}. my-custom-font is not the name of the font, just a placeholder.

It does not seem to be working, hence I'm asking this question here, I hope that I would get a viable answer, thank you so much.

PS: I tried to import my font like it was told in Package for changing font with each letter?, and it did work. I'm just confused as to how I can import my custom font like how I did in the first answer, and into a changing-font-as-it-iterates pattern like written in the second answer, where it seems that it imports the fontcode instead of the fontname (https://www.overleaf.com/learn/latex/font_typefaces).

Once again, thank you so much. I hope I am clear in explaining my issue.

## 1 Answer

It's not difficult to adapt the answer given in this answer:

to use fontspec instead of the PSNFSS naming system used by the old fonts. We just need to change \rndfont command to use the name of the font family defined using \newfontfamily:

\newcommand*{\rndfont}{\pgfmathrandomitem\z{fontlist}\csname\z\endcsname}


Then we change the random list to include those names:

\pgfmathdeclarerandomlist{fontlist}{{helvetica}{comic}{courier}}


Here's the complete code:

\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\setmainfont{Helvetica}
\newfontfamily\helvetica{Helvetica}
\newfontfamily\comic[Scale=MatchUppercase]{Comic Sans MS}
\newfontfamily\courier{Courier}

\usepackage{censor}

\makeatletter

\DeclareTextFontCommand{\textrndfont}{\rndfont}
\newcommand*{\rndfont}{\pgfmathrandomitem\z{fontlist}\csname\z\endcsname}
\pgfmathdeclarerandomlist{fontlist}{{helvetica}{comic}{courier}}

\def\stringend{\$}

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


• Thank you for the comment. Is there anyway that I could apply \randomfont to all texts? Because by default the randomness of the font selection is the point of it, and using \randomfont for each time I would want to use it would seem to be counter productive. Thank you. – Roger Stach Feb 15 at 3:28
• @RogerStach No, there's no way for it to work that way, given how to code is designed. – Alan Munn Feb 15 at 3:36