在alogorithm2e宏包中如何设置`While`后的关键字为宋体/罗马正体?

发布于 2025-11-11 19:31:56

如题,下面的mwe:

\documentclass[fontset=fandol]{ctexart}
\usepackage{graphicx} % Required for inserting images
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
 \usepackage{amsmath}
\title{试验}
\author{834640568 }
\date{July 2025}

\begin{document}

\maketitle

\section{Introduction}

\begin{algorithm}[htb]%算法3.1
\caption{p-VQD算法}
\label{algo:3.1} % 自动编号,可通过 \ref{algo:algorithm} 引用
\small
\SetAlgoLined
\KwData{哈密顿量 $H$, 时间步长 $\Delta t$,时间步数$n$,初态$|\psi_0 \rangle$,梯度范数阈值$\text{grad}_{\text{tol}}$,迭代次数上限$\text{count}_\text{{max}}$,依赖参数$\theta$的量子电路$U(\theta)$}
\KwResult{每个时间步的参数$\theta$}
    \For{第$1,2,\dots,n$个时间步}{
    对上一步的态$|\psi(\theta_t) \rangle$作用时间演化算子$e^{-iH\Delta t}$,得参照态$e^{-iH\Delta t}|\psi(\theta_t) \rangle$ \;
    初始化$\theta = \theta_t$\;%重复步骤3-8\;
    求惩罚函数的梯度$\nabla_{\theta}C(\theta)$     \;
    \While{\text{$|\nabla_{\theta}C(\theta)| > \text{grad}_{\text{tol}}$ \text{且} $\text{count} < count_{\text{max}}$}}{
    根据梯度$\nabla_{\theta}C(\theta)$,利用ADAM优化器更新参数$\theta$  \;
    $\text{count}$加1   \;
    求惩罚函数的梯度$\nabla_{\theta}C(\theta)$     \;
    }
}  
\end{algorithm}

\end{document}

但是这里的字体为斜体,希望修改为罗马正体。

查看更多

关注者
0
被浏览
83
1 个回答
Sagittarius Rover
这家伙很懒,什么也没写!

我没有细看文档,但似乎「\SetArgSty」可以修改这里的样式

\SetArgSty{<font>} sets the argument typography (by default: emph).
\documentclass[fontset=fandol]{ctexart}
\usepackage{graphicx} % Required for inserting images
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\usepackage{amsmath,amsfonts}

\begin{document}
\SetArgSty{textup}%
\section{Introduction}

\begin{algorithm}[H]
    \SetAlgoLined
    \KwData{this text}
    \KwResult{how to write algorithm with \LaTeX2e}
    \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\;
        }
    }
\end{algorithm}

\begin{algorithm}[htb]
    \SetAlgoLined
    \KwResult{每个时间步的参数$\theta$}
        \For{第$1,2,\dots,n$个时间步}{
        对上一步的态$|\psi(\theta_t) \rangle$作用时间演化算子$e^{-iH\Delta t}$,得参照态$e^{-iH\Delta t}|\psi(\theta_t) \rangle$ \;
        初始化$\theta = \theta_t$ \;
        求惩罚函数的梯度$\nabla_{\theta}C(\theta)$ \;
        \While{$|\nabla_{\theta}C(\theta)| > \text{grad}_{\text{tol}}$ 且 $\text{count} < \text{count}_{\text{max}}$}{
        根据梯度$\nabla_{\theta}C(\theta)$,利用ADAM优化器更新参数$\theta$ \;
        $\text{count}$加1 \;
        求惩罚函数的梯度$\nabla_{\theta}C(\theta)$ \;
        }
    }  
\end{algorithm}

\end{document}

嗯...因为我懒得读完全部文档...所以未必是好的实践...

撰写答案

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

发布
问题

分享
好友

手机
浏览

扫码手机浏览