3

I am trying to update a thesis template I got to work non only with pdflatex, but also with lualatex (to enable larger tikz-figures, mostly). Following the received comments to Mathdesign changes \mu-sign in siunitx when used with lualatex, but not with pdflatex I wrote the following MWE:

\documentclass[a4paper,11pt,openright]{book}

\usepackage[dvipsnames]{xcolor}                     % selecting text color
\usepackage{pdfpages}                   % including pdf files

%% PACKAGE LOADING
\usepackage{ifluatex}
\ifluatex
\DeclareSymbolFont{charteraccents}{T1}{XCharter-TLF}{m}{n}
\SetSymbolFont{charteraccents}{bold}{T1}{XCharter-TLF}{b}{n}
\DeclareMathAccent{\grave}{\mathalpha}{charteraccents}{0}
\DeclareMathAccent{\acute}{\mathalpha}{charteraccents}{1}
\DeclareMathAccent{\hat}{\mathalpha}{charteraccents}{2}
\DeclareMathAccent{\tilde}{\mathalpha}{charteraccents}{3}
\DeclareMathAccent{\ddot}{\mathalpha}{charteraccents}{4}
\DeclareMathAccent{\mathring}{\mathalpha}{charteraccents}{6}
\DeclareMathAccent{\check}{\mathalpha}{charteraccents}{7}
\DeclareMathAccent{\breve}{\mathalpha}{charteraccents}{8}
\DeclareMathAccent{\bar}{\mathalpha}{charteraccents}{9}
\DeclareMathAccent{\dot}{\mathalpha}{charteraccents}{10}

\usepackage{amsmath}
\usepackage[no-math]{fontspec}
\usepackage[xcharter]{newtxmath}

\setmainfont{XCharter}[NFSSFamily=XCharter]
\defaultfontfeatures{Ligatures={TeX}}
\setsansfont[Scale=.88]{DejaVuSans}
\setmonofont[Scale=.82]{DejaVuSansMono}
\setmathrm{XCharter}
\setboldmathrm{XCharter Bold}
\else
\usepackage[utf8]{inputenc}             % for special characters in input
\usepackage[T1]{fontenc}                % modern font encoding
\usepackage[charter, cal=cmcal]{mathdesign} % main font with math support
\usepackage{XCharter}
\usepackage[scaled=.88]{berasans}       % sans serif font
\usepackage[scaled=.82]{DejaVuSansMono} % monospace font (for code)
\usepackage{amsmath}                    % advanced math typesetting
\fi
\usepackage[english]{babel}    % language specifics
\usepackage{siunitx}
 \begin{document}
    Hello World

    $100\text{ coins are 100 coins}$

    \SI{100}{\micro\meter} are 100 \textmu m

    100/\num{100}=1
 \end{document}

When compiling with pdflatex, I get the following result: enter image description here

but when compiling with lualatex, I get a slightly different result: enter image description here

The math font is obviously slightly off. What can I do to make the result of lualatex at least comparable, if not equal, to pdflatex? And are there other mistakes?

1
  • 1
    Seems that \mathrm is defaulting to CMR under lualatex. Best might be to \renewcommand\rmdefault{..} before loading newtxmath, however I am unsure of the correct font naming {..} to get you XCharter at that point; perhaps someone else can help. A few other notes: ifluatex should be replaced with the more recent iftex; newtxmath requires amsmath, so you don't need to load it separately; and newtx documentation recommends loading fontspec after newtxmath.
    – John
    Jan 7, 2022 at 13:38

1 Answer 1

2

I've made a number of adjustments to your code (and dropped some lines that weren't relevant to the example). I've added comments that I hope explain, but very briefly: i) the order in which packages are loaded matters; ii) when using newtxmath with fontspec, you must load the text fonts that get folded into math before loading newtxmath; and iii) fontspec must come after newtxmath. I put the fonts loaded by fontspec in color so it would be clear which they are.

There's more that can be done to match the scale of the lualatex fonts to the pdftex packages, but this gives a first approximation.

\documentclass[a4paper,11pt,openright]{book}
\usepackage[dvipsnames]{xcolor} % selecting text color

\usepackage[english]{babel}% <== babel should be loaded before newtx
%\usepackage{ifluatex}% <== obsolete
\usepackage{iftex}% <== use this current version instead

\ifluatex
    %\usepackage{amsmath} <== newtx requires amsmath, need not load separately
    %
    % Load these to provide characters for math, prior to dealing with actual text fonts
    \usepackage[T1]{fontenc}%  math font encoding
    \renewcommand{\rmdefault}{XCharter-TLF}%   font family for \mathrm, numbers, \mathbf, \mathit
    \usepackage[scaled=.88]{berasans}% for \mathsf
    \usepackage[scaled=.82]{DejaVuSansMono} % monospace font for \mathtt
    %
    \usepackage[charter]{newtxmath}
    \usepackage[no-math]{fontspec}% load after newtxmath
    % 
    % Added colors so you can see which OTF fonts have loaded
    \setmainfont{XCharter}[
        NFSSFamily=XCharter,
        Color=teal,
        Extension = .otf,
        UprightFont = *-Roman,
        ItalicFont = *-Italic,
        BoldFont = *-Bold,
        BoldItalicFont = *-BoldItalic,%
    ]       
    %\defaultfontfeatures{Ligatures={TeX}}% <== this is the default setting of fontspec    
    \setsansfont[
        Scale=.88,
        Color=red, 
        Extension = .ttf, 
        UprightFont = *,
        ItalicFont = *-Oblique,
        BoldFont = *-Bold,
        BoldItalicFont = *-BoldOblique,%
        WordSpace = {0.75},% appears that word spacing is lower in the pdftex packages, but more adjustments could be done.
        ]{DejaVuSans}%
    \setmonofont[
        Scale=.82,% Scale=0.82 seems small, might 0.88 be better?
        Color=blue,
        Extension = .ttf, 
        UprightFont = *,
        ItalicFont = *-Oblique,
        BoldFont = *-Bold,
        BoldItalicFont = *-BoldOblique,%
        WordSpace = {0.75},
        ]{DejaVuSansMono}
\else
    \usepackage[utf8]{inputenc}             % for special characters in input
    \usepackage[T1]{fontenc}                % modern font encoding
    \usepackage[scaled=.88]{berasans}       % sans serif font <== loading order matters
    \usepackage[scaled=.82]{DejaVuSansMono} % monospace font (for code) <== loading order matters
    \usepackage[charter, cal=cmcal]{mathdesign} % main font with math support
    \usepackage{XCharter}
    \usepackage{amsmath}                    % advanced math typesetting
\fi
\usepackage{siunitx}

\begin{document}
    Hello World. \textsf{Hello World.} \texttt{Hello World.}

    \textit{Hello World. \textsf{Hello World.} \texttt{Hello World.}}
    
    \textbf{Hello World. \textsf{Hello World.} \texttt{Hello World.}}
    
    \textbf{\textit{Hello World. \textsf{Hello World.} \texttt{Hello World.}}}

    $100\text{ coins are 100 coins}$  
    
    \SI{100}{\micro\meter} are 100 \textmu m

    100/\num{100}=1
    
    \[f(x) = A \sin x\cos x\, \]
    
    $\mathrm{100~m}\quad \mathbf{100~m}\quad \mathsf{100~m}\quad \mathtt{100~m}$
     
 \end{document}

Under pdflatex: pdflatex version Under lualatex: luatex version

2
  • Thanks! Something I'm curious about: Why are you loading \usepackage[T1]{fontenc} not only for pdflatex, but also for lualatex? As far as I know (following f.ex. tex.stackexchange.com/q/412757/61517) it is not required at all. Or is it just to provide fonts, which then are changed to TU later?
    – arc_lupus
    Jan 8, 2022 at 11:03
  • It's loaded to support the math fonts when LuaLaTeX is used. These fonts are chosen from the current text fonts; and since math is loaded before fontspec, we need to specify the text fonts that are used and their encoding; otherwise we default to computer modern fonts or TU encodings. In particular, fontenc is needed for the sans and tt font packages, so that T1 encoded fonts are sought instead of TU versions of those fonts (which aren't available, try commenting out fontenc and look at the result). The text fonts loaded by fontspec are in the default TU (unicode) encoding.
    – John
    Jan 8, 2022 at 16:55

You must log in to answer this question.

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