[Tex/LaTex] How to compile the same input file for each of LaTeX font packages installed in our machine

fontsprogrammingshell-escape

enter image description here

Inspired by my question How to create a PDF presentation for each available Beamer theme automatically?, now I want to compile my input file for each available LaTeX font package installed in my machine.

Listing each font name in \foreach as follows does not seem a good idea as it will not accommodate all available packages as well as it is not up-to-date.

% the file name of this code is inputfile.tex
% compile it with pdflatex -shell-escape inputfile.tex

\documentclass[preview,border=12pt,multi]{standalone}
\usepackage{filecontents}

\begin{filecontents*}{template.tex}
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{\loadfont}
\usepackage{pgffor}
\usepackage{amsmath}
\usepackage{mathrsfs}
\begin{document}
\foreach \x in {1,...,26}{\char\numexpr\x+64\relax\char\numexpr\x+96\relax\space}
\begin{align*}
\sin (\alpha \pm\beta) &=\sin\alpha \cos \beta \pm \cos\alpha \sin\beta\\
\mathscr{L}\left\{f(t)\right\} &= F(s)\\
\vec{F} &= m\frac{\textrm{d}\vec{v}(t)}{\textrm{d}t}\\
\int_a^b f(x)\, \textrm{d}x &= F(b) -F(a)\\
\vec \nabla \cdot \vec \nabla \times A &= \vec\nabla  \times  \nabla U
\end{align*}
\end{document}
\end{filecontents*}

\usepackage{graphicx}
\usepackage{pgffor}

\def\dojob#1{\immediate\write18{pdflatex -jobname=template-#1 "\def\noexpand\loadfont{#1} \noexpand\input{template}"}}


\begin{document}
\foreach \x in {bera,palatino,mathpazo,helvet,libertine}
{%
    \dojob{\x}%
    \preview
    \section{\x}
    \includegraphics{template-\x}
    \endpreview
}
\end{document}

enter image description here

A list of all installed packages (the list includes not only font packages but also other packages) can be done easily with this method How to print the list of packages installed on TeX Live to a file?. However, I have no idea to filter the list and remove the non-font packages.

How to compile the same input file for each of LaTeX font packages installed in our machine?

Best Answer

Here is my solution, assuming you have access to a UNIX¹ system with a TeX installation.

Inside the working directory of your project

  1. Create a main subdirectory holding the files of your document.
  2. Edit the preamble of your document, so that it includes a statement input{fontsetup}.
  3. For each font package you want to test, create a subdirectory holding a single file fontsetup.tex responsible for setting up the font you want.
  4. In your directory project, create a build.sh shell script that you will run to produce your outputs, as described below.

The build.sh script may look like, where I assume your main document file is galley.tex and that your packages are 'lmodern mathpple'

#!/bin/sh
pkg_list='mathpple lmodern'
for pkg in "$pkg_list"; do
  env TEXINPUTS="main:$pkg:." tex -jobname $pkg galley.tex
done

Depending on your system, you may replace the pkg_list assignment by

pkg_list=`find . -type d \( -name main -prune -o -print \)`

which saves you the trouble from maintaining the list.

Now you still have to prepare directories for each package you want to test—and update the pkg_list variable. To help you create the following fontsetup.sh script in your project directory:

#!/bin/sh
for pkg in "$@"; do
   install -d $pkg
   printf '\\usepackge{%s}\n' $pkg > $pkg/fontsetup.tex
done

Give the execute bit to the scripts and give it a try with

$ ./fontsetup.sh mathpple lmodern
$ ./build.sh

The first command creates a directory for each package and a canonical fontsetup.tex file. The second command processes each package.

Having a different file for each font package is nice, because it allows you to add font-specific customisations.

¹ I mean FreeBSD of course!

Related Question