我正在尝试更新一个论文模板,我不仅要使用它pdflatex
,还要使用它lualatex
(主要是为了启用更大的 tikz 图形)。根据收到的评论Mathdesign 与 lualatex 一起使用时会在 siunitx 中更改 \mu-sign,但与 pdflatex 一起使用时不会更改我写了以下 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}
但是当用 进行编译时lualatex
,我得到了稍微不同的结果:
数学字体明显有点不对。我该怎么做才能使 的结果lualatex
至少与 相当(如果不相等)pdflatex
?还有其他错误吗?
答案1
我对您的代码进行了一些调整(并删除了一些与示例无关的行)。我添加了注释,希望能解释清楚,但非常简短:i)加载包的顺序很重要;ii)使用newtxmath
with时fontspec
,必须在加载之前加载折叠到 math 中的文本字体newtxmath
;iii)fontspec 必须在之后newtxmath
。我将加载的字体放在fontspec
彩色中,这样就可以清楚地看到它们是什么。
还可以做更多的事情来将 lualatex 字体的比例与 pdftex 包相匹配,但这给出了一个初步的近似值。
\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}