将伪代码的某些部分加粗

将伪代码的某些部分加粗

我只想让一些伪代码(如下)在我的算法中脱颖而出。所以我用 取消了所有关键字(if、end for 等)的粗体
\AtBeginEnvironment{algorithmic}{\let\textbf\relax}。现在我如何才能使这段代码加粗?我正在发布需要加粗的部分。

\documentclass[11pt]{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\AtBeginEnvironment{algorithmic}{\let\textbf\relax}
\usepackage{amsmath}
\usepackage[fleqn]{mathtools}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{fontspec}
\usepackage{bm}
\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\alglinenumber[1]{
{\sf\footnotesize\addfontfeatures{Colour=888888,Numbers=Monospaced}#1}}
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
  \State //Explo. Loop
  \For{\texttt{$i$ from 1 to $S$}}
    \State //Take an exploration step for bacterium {$i$}
    \If {$E_t$ = $M_t$}
        \Let {$\theta(i,j+1,k)$} random position
        \State {$J_{last}$} = {$J(\theta(i,j+1,k))$}
        \If {$J_{last}$ < $J(i,j,k)$}
            \Let {$J(i,j+1,l)$} {$J_{last}$}
        \EndIf
        \State end //if
        \State {$E_t$} = 0
    \EndIf
    \State end //if
\EndFor
\State end //Explo. Loop
\State // Exploitation Loop
\For{\texttt{i from 1 to $S$}}
    \If {$E_r$ = $M_r$} let bacterium undergo:
        \State  (Eq. 7)
        \State  (Eq. 8)
        \State  (Eq. 9)
    \EndIf
    \State end //if
\EndFor
\State end //Explo. Loop
\end{algorithmic}
\end{algorithm}
\end{document}

答案1

我认为以那种方式删除不是一个好主意\textbf,但让所有环境都变成粗体则是一个更糟糕的想法。

算法本身就已经很突出了,粗体关键词可以帮助读者找到关键位置。

但是,如果您确实想遵循这条路径,只需添加\bfseries\boldmath到您的环境中即可。

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[fleqn]{mathtools}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{fontspec}
\usepackage{bm}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

\makeatletter
\AtBeginEnvironment{algorithmic}{\let\textbf\@firstofone}
\makeatother

\setsansfont{Latin Modern Sans} % or \addfontfeatures won't work

\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\alglinenumber[1]{%
  {\sffamily\footnotesize\addfontfeatures{Colour=888888,Numbers=Monospaced}#1}}

\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]\bfseries\boldmath
  \State //Explo. Loop
  \For{\texttt{$i$ from 1 to $S$}}
    \State //Take an exploration step for bacterium {$i$}
    \If {$E_t$ = $M_t$}
        \Let {$\theta(i,j+1,k)$} random position
        \State {$J_{last}$} = {$J(\theta(i,j+1,k))$}
        \If {$J_{last}$ < $J(i,j,k)$}
            \Let {$J(i,j+1,l)$} {$J_{last}$}
        \EndIf
        \State end //if
        \State {$E_t$} = 0
    \EndIf
    \State end //if
\EndFor
\State end //Explo. Loop
\State // Exploitation Loop
\For{\texttt{i from 1 to $S$}}
    \If {$E_r$ = $M_r$} let bacterium undergo:
        \State  (Eq. 7)
        \State  (Eq. 8)
        \State  (Eq. 9)
    \EndIf
    \State end //if
\EndFor
\State end //Explo. Loop
\end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

相关内容