3

I have a code that shows how fonts look like. The code compiles in 10 seconds with only Latin Modern examples. But if I add the examples for math fonts for Termes, Pagella, XCharter and others it takes minutes to complete the compilation (I stopped 2:10 for this code).

Why does it take so long and how can I prevent it? Working with lualatex and this code hardly usable due to the long compilation time.

The example is not minimal but should be enough to reproduce the problem.

% !TeX encoding=utf8
% !TeX program = lualatex

\documentclass{article}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{unicode-math}
\usepackage{tabularray}
\usepackage{xcolor}

\begin{document}
\newcommand{\fontstring}{Sphinx of black quartz, judge my vow.}
\newcommand{\mathstring}{$f(u,v) = \iiint \left[u\nabla^{2}v+\left(\nabla  u,\nabla  v\right)\right]\mathrm{d}^{3}V$}

\colorlet{tablerowcolor}{gray!10}
\colorlet{tablesubheadcolor}{azure3!30}


{ % start a group 
    \small\renewcommand{\arraystretch}{1.4}\sffamily
    % the table
    \begin{longtblr}[
        caption = {Font examples},
        label = {tab:doc:Font:Gallery}]
        {
            colspec = {X[1,l]>{\ttfamily}X[2,l]},
            width = 1.0\textwidth,
            row{odd} = {bg=tablerowcolor},
            row{1}   = {bg=azure3, fg=white, font=\sffamily\upshape},
            rowhead = 1,
            rowfoot = 0,
        }       
        \hline % Table Header
        Font & Example \\ 
        \hline %        
        %
        \SetCell[c=2]{l,bg=tablesubheadcolor} Latin Modern Family \\
        Latin Modern Roman   & \setmainfont{Latin Modern Roman} \rmfamily \fontstring \\
        Latin Modern Sans    & \setsansfont{Latin Modern Sans}  \sffamily\fontstring \\
        Latin Modern Mono    & \setmonofont{Latin Modern Mono}  \ttfamily \fontstring \\
        %
        \SetCell[c=2]{l,bg=tablesubheadcolor} Math Fonts \\
        %
        Latin Modern Math       & \setmathfont{Latin Modern Math}       \rmfamily \mathstring \\
%       TeX Gyre Termes Math    & \setmathfont{TeX Gyre Termes Math}    \rmfamily \mathstring \\
%       TeX Gyre Pagella Math   & \setmathfont{TeX Gyre Pagella Math}   \rmfamily \mathstring \\
%       Charter - math version  & \setmathfont{XCharter-Math.otf}   \rmfamily \mathstring \\
%       Garamond math version   & \setmathfont{Garamond-Math}       \rmfamily \mathstring \\
%       Cambria Math            & \setmathfont{Cambria Math}        \rmfamily \mathstring \\
        %       %
        \hline
        %
    \end{longtblr}  
} % close the group
\end{document}
2
  • Why do you use setmathfont inside a cell? Dec 18, 2022 at 8:09
  • Off-topic: The unicode-math package loads the fontspec package automatically. No need to load the fontspec separately as well.
    – Mico
    Dec 18, 2022 at 11:45

1 Answer 1

5

Why does it take so long and how can I prevent it?

Section 4.3, entitled "Maths [sic] 'versions'", of the user guide of the unicode-math package notes that it quickly becomes highly inefficient to use multiple \setmathfont directives midstream within a document -- as happens to be the case in the test document shown above.

Instead, it's advisable to set up "math versions" in the preamble by providing the following instructions

\setmathfont{Latin Modern Math}[version=LM]
\setmathfont{TeX Gyre Termes Math}[version=Termes]
\setmathfont{TeX Gyre Pagella Math}[version=Pagella]
\setmathfont{XCharter-Math.otf}[version=XCharter]
\setmathfont{Garamond-Math}[version=Garamond]
\setmathfont{Cambria Math}[version=Cambria]

and to run \mathversion{LM} ... \mathversion{Termes} ... etc instructions in the body of the document to switch between the math versions.

The following test program takes fewer than 8 seconds to compile on my M2-chip MacBook Air that runs MacOS 13.1 "Ventura" and MacTeX2022 with all updates applied. For comparison, it takes 1'33'' to compile the OP's test code (with the multiple midstream \setmathfont directives enabled) on my laptop. I think it's fair to call this efficiency gain impressive.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{amsmath}
\usepackage{tabularray}
\usepackage[x11names]{xcolor}
\colorlet{tablerowcolor}{gray!10}
\colorlet{tablesubheadcolor}{Azure4!30}

% set text and math fonts
\usepackage{unicode-math}
\setmainfont{Latin Modern Roman}
\setsansfont{Latin Modern Sans}  
\setmonofont{Latin Modern Mono}  

\setmathfont{Latin Modern Math}[version=LM]
\setmathfont{TeX Gyre Termes Math}[version=Termes]
\setmathfont{TeX Gyre Pagella Math}[version=Pagella]
\setmathfont{XCharter-Math.otf}[version=XCharter]
\setmathfont{Garamond-Math}[version=Garamond]
\setmathfont{Cambria Math}[version=Cambria]

% 2 test strings
\newcommand{\textstring}{Sphinx of black quartz, judge my vow.}
\newcommand{\mathstring}{$f(u,v) = 
     \iiint [u\nabla^2 v+(\nabla u,\nabla v)]\,\symup{d}^3 V$}

\begin{document}

\begingroup % limit scope of instructions on next line
\small\renewcommand{\arraystretch}{1.4}\sffamily
% the table
    \begin{longtblr}[
            caption = {Font examples},
            label   = {tab:doc:Font:Gallery}]
        {
            colspec = {X[1,l] X[2,l]},
            width = 1.0\textwidth,
            row{odd} = {bg=tablerowcolor},
            row{1}   = {bg=azure3, fg=white, font=\sffamily\upshape\bfseries},
            rowhead = 1,
            rowfoot = 0,
        }       
        \hline % Table Header
        Font & Example \\ 
        \hline %        
        %
        \SetCell[c=2]{l,bg=tablesubheadcolor} Text Fonts \\
        Latin Modern Roman   & \rmfamily \textstring \\
        Latin Modern Sans    & \sffamily \textstring \\
        Latin Modern Mono    & \ttfamily \textstring \\
        %
        \SetCell[c=2]{l,bg=tablesubheadcolor} Math Fonts \\
        %
        Latin Modern Math     & \mathversion{LM}        \mathstring \\
        TeX Gyre Termes Math  & \mathversion{Termes}    \mathstring \\
        TeX Gyre Pagella Math & \mathversion{Pagella}   \mathstring \\
        Charter Math          & \mathversion{XCharter}  \mathstring \\
        Garamond Math         & \mathversion{Garamond}  \mathstring \\
        Cambria Math          & \mathversion{Cambria}   \mathstring \\
        % 
        \hline
        %
    \end{longtblr}  
\par\endgroup

\end{document}
2
  • 1
    I need to read the docs to understand the reason, but it works. Dec 18, 2022 at 11:38
  • 1
    The package author is from Australia, where they mostly use British spellings (not pronunciation, of course 😊), so “maths” and not “math” like in American English.
    – egreg
    Dec 18, 2022 at 23:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .