20 如何替换公式中数字、字母、希腊字母、标点符号为罗马体不影响其他数学符号字体?

发布于 2026-07-26 18:29:57

排版数学公式时,想把数学公式中的数字、字母、希腊字母、sin、cos、lim等排版成times字体,积分号、sum、[]、{}、()保留原有的美观性,采取何种方法完美解决?以下是我的代码

\documentclass[
a4paper,
twoside,
titlepage,
openright,
no-math,
]{ctexbook}

\RequirePackage{amsmath,datatool,amsthm,bm} 
%\RequirePackage 是“内核级强制加载”,\usepackage 是“文档级正常加载”。
\RequirePackage{
    amssymb,
    latexsym,
    textcomp,
    mathrsfs,
    euscript,
    yhmath
}
\usepackage{mathtools}       % AMS 数学增强
\usepackage{upgreek}         % 直立希腊字母(单位、常数)
\usepackage{siunitx}         % 单位、千分号、摄氏度等
\sisetup{
    per-mode = symbol,  %分数形式的“每”用符号 /表示,而不是分式,如:m/s
    inter-unit-product = \ensuremath{{}\cdot{}}, %复合单位之间用“点乘 ·”连接,如:\SI{10}{N.m},输出10 N·m
    group-separator = {,},  %数字千分位分隔符使用英文逗号,如:\num{1234567.89},输出1,234,567.89。
    detect-all, 
    %让 siunitx自动检测当前字体/字号/粗细,跟随周围文本的:字体族(Roman / Sans)、
    %字号(\large, \small)、粗体 / 斜体;单位、数字不会“跳出”正文风格
    exponent-product = \times,    % 科学计数法用 ×
    output-decimal-marker = {.},  % 小数点用 .
}

%保护数学公式不被拉伸,正文\setstretch{1.5}会污染数学公式行距,导致带括号()的公式、矩阵丑陋。
\AtBeginDocument{
    \everymath{\linespread{1}\selectfont}
    \everydisplay{\linespread{1}\selectfont}
}

% ---------- 数学排版习惯 ----------
\allowdisplaybreaks        % 允许公式跨页
\setlength{\jot}{6pt}       % 公式行距
\everymath{\displaystyle}  % 正文公式也用 display 风格(可选)
% ---------- 自定义数学算子 ----------
\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\sgn}{sgn}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator{\Cov}{Cov}
\DeclareMathOperator{\argh}{argh}
\DeclareMathOperator*{\nut}{Nut}
% ---------- 微分 d 正体 ----------
\newcommand{\diff}{\mathop{}\!\mathrm{d}}
\newcommand{\Diff}{\mathop{}\!\mathrm{D}}
% ---------- 定理环境 ----------
\newtheorem{theorem}{定理}
\newtheorem{lemma}[theorem]{引理}
\newtheorem{corollary}[theorem]{推论}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{定义}

% ---------- 数学排版习惯 ----------
\allowdisplaybreaks        % 允许公式跨页
\setlength{\jot}{6pt}       % 公式行距
%\everymath{\displaystyle}  % 正文公式也用 display 风格(可选)

\begin{document}
    \chapter{数学公式排版}
    
    In text:
    $\lim_{n \to \infty}
    \sum_{k=1}^n \frac{1}{k^2}
    = \frac{\pi^2}{6}$.
    In display:
    \[
    \lim_{n \to \infty}
    \sum_{k=1}^n \frac{1}{k^2}
    = \frac{\pi^2}{6}
    \]
    
    % ---------- 1. 拉丁字母 ----------
    \[
    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
    \]
    \[
    A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
    \]
    
    % ---------- 2. 希腊字母 ----------
    \[
    \alpha,\beta,\gamma,\delta,\epsilon,\zeta,\eta,\theta,
    \iota,\kappa,\lambda,\mu,\nu,\xi,\pi,\rho,\sigma,\tau,
    \upsilon,\phi,\chi,\psi,\omega
    \]
    \[
    \Gamma,\Delta,\Theta,\Lambda,\Xi,\Pi,\Sigma,\Upsilon,\Phi,\Psi,\Omega
    \]
    
    % ---------- 5. 黑板粗体(数域) ----------
    \[
    \mathbb{R},\mathbb{C},\mathbb{H},\mathbb{Q},\mathbb{Z},\mathbb{N}
    \]
    
    % ---------- 6. 花体 / 手写体 ----------
    \[
    \mathcal{A},\mathcal{B},\mathcal{H},\mathcal{L},\mathcal{F}
    \]
    \[
    \mathscr{A},\mathscr{B},\mathscr{G}
    \]
    
    % ---------- 7. 哥特体(群、代数) ----------
    \[
    \mathfrak{g},\mathfrak{h},\mathfrak{su}(2),\mathfrak{so}(3)
    \]
    
    % ---------- 8. 数学算符 ----------
    \[
    \sin x,\cos x,\tan x,\log x,\ln x,\exp x
    \]
    \[
    \lim_{x\to0},\sum_{n=1}^{\infty},\prod_{k=1}^{N}
    \]
    \[
    \int_{0}^{\infty}e^{-x^2}\,dx
    \]
    
    % ---------- 9. 微分与导数 ----------
    \[
    \frac{d f}{d t},
    \frac{\partial u}{\partial t},
    \nabla f,\nabla\cdot\vec{E},\nabla\times\vec{B}
    \]
    \[
    \phi,\vec{E},\vec{B}
    \]
    
    % ---------- 10. 物理常量与单位 ----------
    \[
    c = \SI{2.998e8}{m/s},\quad
    h = \SI{6.626e-34}{J.s},\quad
    k_B = \SI{1.381e-23}{J/K}
    \]
    
    % ---------- 11. 关系符与箭头 ----------
    \[
    =,\neq,\approx,\sim,\propto,\equiv,
    \leq,\geq,\ll,\gg
    \]
    
    \begin{equation}
        \displaystyle
        \mathbf{A} =
        \begin{pmatrix}
            a_{11} & a_{12} & \cdots & a_{1n} \\
            a_{21} & a_{22} & \cdots & a_{2n} \\
            \vdots & \vdots & \ddots & \vdots \\
            a_{n1} & a_{n2} & \cdots & a_{nn}
        \end{pmatrix}
    \end{equation}
    
    Pascal's rule is
    \[
    \binom{n}{k} =\binom{n-1}{k}
    + \binom{n-1}{k-1}
    \]    
    In text:
    $\sum_{i=1}^n \quad
    \int_0^{\frac{\pi}{2}} \quad
    \oint_0^{\frac{\pi}{2}} \quad
    \prod_\epsilon $ \\
    In display:
    \[\sum_{i=1}^n \quad
    \int_0^{\frac{\pi}{2}} \quad
    \oint_0^{\frac{\pi}{2}} \quad
    \prod_\epsilon \]
    $\Bigl((x+1)(x-1)\Bigr)^{2}$\\
    $\bigl( \Bigl( \biggl( \Biggl( \quad
    \bigr\} \Bigr\} \biggr\} \Biggr\} \quad
    \big\| \Big\| \bigg\| \Bigg\| \quad
    \big\Downarrow \Big\Downarrow
    \bigg\Downarrow \Bigg\Downarrow$
    
    \[ \mathbf{X} = \left(
    \begin{array}{cccc}
        x_{11} & x_{12} & \ldots & x_{1n}\\
        x_{21} & x_{22} & \ldots & x_{2n}\\
        \vdots & \vdots & \ddots & \vdots\\
        x_{n1} & x_{n2} & \ldots & x_{nn}\\
    \end{array} \right) \]    
    \[
    \begin{pmatrix}
        a_{11}&a_{12}&\ldots&a_{1n}\\
        a_{21}&a_{22}&\ldots&a_{2n}\\
        \hdotsfor{4}\\
        a_{n1}&a_{n2}&\ldots&a_{nn}\\
    \end{pmatrix}
    \]
    和
    \[\begin{pmatrix}
        a_{11}&a_{12}&\ldots&a_{1n}\\
        a_{21}&a_{22}&\ldots&a_{2n}\\
        \vdots &\vdots & &\vdots\\
        a_{n1}&a_{n2}&\ldots&a_{nn}\\
    \end{pmatrix}\]
    \[
    \to,\rightarrow,\leftarrow,\leftrightarrow,
    \Rightarrow,\Leftarrow,\Leftrightarrow
    \]    
    % ---------- 12. 集合与逻辑 ----------
    \[
    \in,\notin,\subset,\supset,\cup,\cap,\emptyset
    \]
    \[
    \wedge,\vee,\neg,\forall,\exists
    \]
    
    % ---------- 13. 特殊物理符号 ----------
    \[
    \hbar,\ell,\wp,\aleph,\beth
    \]
    
    \[
    \begin{bmatrix}
        x_{11} & x_{12} & \ldots & x_{1n}\\
        x_{21} & x_{22} & \ldots & x_{2n}\\
        \vdots & \vdots & \ddots & \vdots\\
        x_{n1} & x_{n2} & \ldots & x_{nn}\\
    \end{bmatrix}
    \]
    
    \[
    \mathbf{H}=
    \begin{bmatrix}
        \dfrac{\partial^2 f}{\partial x^2} &
        \dfrac{\partial^2 f}
        {\partial x \partial y} \\[8pt]
        \dfrac{\partial^2 f}
        {\partial x \partial y} &
        \dfrac{\partial^2 f}{\partial y^2}
    \end{bmatrix}
    \]    
    \[
    r = \frac
    {\sum_{i=1}^n (x_i- x)(y_i- y)}
    {\displaystyle \left[
        \sum_{i=1}^n (x_i-x)^2
        \sum_{i=1}^n (y_i-y)^2
        \right]^{1/2} }
    \]    
    \[ |x| =
    \begin{cases}
        -x & \text{if } x < 0,\\
        0 & \text{if } x = 0,\\
        x & \text{if } x > 0.
    \end{cases} \]    
    % ---------- 15. 交换子与对易关系 ----------
    \[
    [A,B]=AB-BA,\quad
    [\hat{x},\hat{p}] = i\hbar
    \]
    % ---------- 16. 大运算符 ----------
    \[
    \int\!\!\int_S \vec{F}\cdot d\vec{S},
    \oint_C \vec{B}\cdot d\vec{l},
    \sum_{i=1}^{N}a_i,
    \prod_{k=1}^{n}(x-x_k)
    \]
    
    \[
    \mathbf{H}=
    \begin{bmatrix}
        \dfrac{\partial^2 f}{\partial x^2} &
        \dfrac{\partial^2 f}
        {\partial x \partial y} \\[8pt]
        \dfrac{\partial^2 f}
        {\partial x \partial y} &
        \dfrac{\partial^2 f}{\partial y^2}
    \end{bmatrix}
    \]    
    \begin{gather*}
        \int\int f(x)g(y)
        \diff x \diff y \\
        \int\!\!\!\int
        f(x)g(y) \diff x \diff y \\
        \iint f(x)g(y) \diff x \diff y \\
        \iint\quad \iiint\quad \idotsint
    \end{gather*}    
    % ---------- 17. 矩阵 ----------
    \[\left(\begin{array}{cccc}
        a_{11} & a_{12} & \cdots & a_{1n} \\
        &a_{22} & \cdots &a_{2n} \\
        & & \ddots & \vdots \\
        \multicolumn{2}{c}{\raisebox{1.3ex}[0pt]{\Huge0}}
        & &a_{nn}
    \end{array}\right)
    \]
    \[\begin{pmatrix}
        a_{11} & a_{12} & \cdots & a_{1n} \\
        &a_{22} & \cdots &a_{2n} \\
        & & \ddots & \vdots \\
        \multicolumn{2}{c}{\raisebox{1.3ex}[0pt]{\Huge0}}
        & &a_{nn}
    \end{pmatrix}
    \]    
    % ---------- 18. 分段函数 ----------
    \[
    f(x)=
    \begin{cases}
        x^2,&x\ge0\\
        -x,&x<0
    \end{cases}
    \]    
    $$\left(\dfrac{\dfrac{3}{5}}{8+x+\dfrac{6a}{2B}}+6=14\right)$$
\end{document}

使用unicode-math包加载外置字体,基本实现,但是矩阵的括号()压扁变丑、运算符sum变窄难看;使用`
usepackage[no-math]{fontspec}

查看更多

关注者
0
被浏览
45
1 个回答
Sagittarius Rover
Sagittarius Rover 8小时前
这家伙很懒,什么也没写!
使用unicode-math包加载外置字体,基本实现,但是矩阵的括号()压扁变丑、运算符sum变窄难看;使用usepackage[no-math]{fontspec}

没看明白..代码里好像也没体现...

想把数学公式中的数字、字母、希腊字母、sin、cos、lim等排版成times字体,积分号、sum、[]、{}、()保留原有的美观性

这和ctexbook,siunitx好像都没什么关系... 另外AI生成的测试代码太长了...不想看...可以试试这个range的方法...

\documentclass{article}
\usepackage[no-math]{fontspec}
\usepackage{unicode-math}
\setmathfont{latinmodern-math.otf}
\setmathfont[
  range={
    up/{num,latin,Latin,greek,Greek},
    it/{latin,Latin,greek,Greek},
    bfup/{num,latin,Latin,greek,Greek},
    bfit/{latin,Latin,greek,Greek},
    "0021,"002C,"002E,"003B,"003F
  }
]{texgyretermes-math.otf}

\begin{document}

123abcABC,.;?! +-*/

$123abcABC,.;?!\alpha\beta\Gamma\Delta$ 

\[
\iint_S \vec{F}\cdot d\vec{S},
\oint_C \vec{B}\cdot d\vec{l},
\sum_{i=1}^{N}a_i,
\prod_{k=1}^{n}(x-x_k)
\]

\[
\begin{bmatrix}
    \dfrac{\partial^2 f}{\partial x^2} &
    \dfrac{\partial^2 f}
    {\partial x \partial y} \\
    \dfrac{\partial^2 f}
    {\partial x \partial y} &
    \dfrac{\partial^2 f}{\partial y^2}
\end{bmatrix} 
=
\begin{pmatrix}
            a_{11} & a_{12} & \cdots & a_{1n} \\
            a_{21} & a_{22} & \cdots & a_{2n} \\
            \vdots & \vdots & \ddots & \vdots \\
            a_{n1} & a_{n2} & \cdots & a_{nn}
\end{pmatrix}
\]    

\end{document}

image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览