我正在尝试为包含数学和算法块的文档设置字体。我正在使用 XCharter 字体和软件包algorithm2e
。出于某种原因,我无法正确排版。下面是 MWE 来演示我的问题。
\documentclass{article}
% \usepackage{XCharter} % needed for math fonts, uncommenting will break fonts in the algorithm environment
\usepackage[xcharter,bigdelims,slantedGreek,vvarbb,scaled=1.07]{newtxmath} % math, iso 31-compliant(ish)
\usepackage[no-math]{fontspec}
\setmainfont{XCharter}
\usepackage{algorithm2e}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
a + b = \int_0^{+\infty} \frac{\partial u}{\partial x} \times \alpha' \, \mathrm{d} \boldsymbol{l}
\end{equation*}
\begin{equation*}
\alpha + \upbeta = \int_0^{+\infty} \frac{\partial \upPi}{\partial \Theta} \times \epsilon' \, \mathrm{d} \boldsymbol{\gamma}
\end{equation*}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
如果我不使用该XCharter
包,那么微分(使用 d)将使用 Computer Modern 排版。
如果我确实使用它,微分会被正确排版,但算法块的渲染效果会很糟糕。
我究竟做错了什么?
答案1
由于字体设置不标准,需要您的帮助:
\documentclass{article}
\usepackage[xcharter,bigdelims,slantedGreek,vvarbb,scaled=1.07]{newtxmath} % math, iso 31-compliant(ish)
\usepackage{fontspec}
\setmainfont{XCharter}
\usepackage{amsmath}
\usepackage{algorithm2e}
\usepackage{metalogox}
\AtBeginDocument{%
\DeclareSymbolFont{operators}{OT1}{XCharter-TLF}{m}{n}%
\SetSymbolFont{operators}{bold}{OT1}{XCharter-TLF}{b}{n}%
}
\begin{document}
\begin{equation*}
a + b = \int_0^{+\infty} \frac{\partial u}{\partial x} \times \alpha' \, \mathrm{d} \boldsymbol{l}
\end{equation*}
\begin{equation*}
\alpha + \upbeta = \int_0^{+\infty} \frac{\partial \upPi}{\partial \Theta} \times \epsilon' \, \mathrm{d} \boldsymbol{\gamma}
\end{equation*}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeXe}
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}