算法中的自定义编号

算法中的自定义编号

我想自定义算法中的编号。这是一个简短的 MWE

\documentclass[article]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc} 
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}


\usepackage{algorithm,algorithmicx,algpseudocode}

\begin{document}
\begin{algorithm}[h]
\begin{algorithmic}
\Require data ($X, y$); number of runs $K$; nominal level $q \in [0,1]$
\State Choose sequence $\{q_k\}_{k=1}^{K} \in [0,1]$ such that $q = \sum_{k=1}^{K} q_k$.
\For  {$k \in \{1,\dots,K\}$} 
    \State Apply knockoff/knockoff+ with nominal level $q_k$ and obtain selection set $\hat{\mathcal{S}}_{q_k}$.
\EndFor
\State Result: $K$ different selection sets $\hat{\mathcal{S}}_{q_1},\dots, \hat{\mathcal{S}}_{q_K}$.
\State Aggregate the selection sets by taking their union
\begin{align}
\hat{\mathcal{S}}_{q}^{U} = \bigcup_{k=1}^{K} \hat{\mathcal{S}}_{q_k}.
\end{align} 
\Ensure Aggregated set $\hat{\mathcal{S}}_{q}^{U}$ 
  \end{algorithmic}
\end{algorithm}
\end{document}

更具体地说,我只想枚举以下用蓝色标记的行。更准确地说,我不想枚举由 \begin 产生的算法的,我只是想在那三行前面放置一个“1、2 和 3”,以清楚地表明这是 3 个步骤(而不是 3 行代码)。

在此处输入图片描述

答案1

定义了两个新命令 --\Staten和-- ,用于向和\Forn添加增量前缀(带点) 。\State\For

每次之后必须重置使用的计数器(enumstate)(如果需要)algorithm

d

% !TeX TS-program = pdflatex

\documentclass[article]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc} 
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\usepackage{algorithm}
\usepackage{algpseudocode} % loads algorithmicx

%% ******************************************* added <<<<<<<<<<<<<<
\newcounter{enumstate}
\stepcounter{enumstate}

\newcommand{\Staten}{\item[\theenumstate.]\stepcounter{enumstate}}% numbered state with dot
\algnewcommand\algorithmicforn{\hspace*{-3ex}\theenumstate.\stepcounter{enumstate} \ \textbf{for}} % numbered for with dot
\algdef{SE}[FOR]{Forn}{EndForn}[1]{ \algorithmicforn\  #1\ \algorithmicdo}{\algorithmicend\ \algorithmicfor}%   
%% *******************************************

\begin{document}

\begin{algorithm}[h]
    \begin{algorithmic}
    \Require data ($X, y$); number of runs $K$; nominal level $q \in [0,1]$
    \Staten Choose sequence $\{q_k\}_{k=1}^{K} \in [0,1]$ such that $q = \sum_{k=1}^{K} q_k$.
    \Forn  {$k \in \{1,\dots,K\}$} 
        \State Apply knockoff/knockoff+ with nominal level $q_k$ and obtain selection set $\hat{\mathcal{S}}_{q_k}$.
    \EndForn
        \State Result: $K$ different selection sets $\hat{\mathcal{S}}_{q_1},\dots, \hat{\mathcal{S}}_{q_K}$.
    \Staten  Aggregate the selection sets by taking their union
        \begin{align}
            \hat{\mathcal{S}}_{q}^{U} = \bigcup_{k=1}^{K} \hat{\mathcal{S}}_{q_k}.
        \end{align} 
        \Ensure Aggregated set $\hat{\mathcal{S}}_{q}^{U}$ 
    \end{algorithmic}
\end{algorithm}

\setcounter{enumstate}{1} % resert counter <<<<<<<<<<<<<

\begin{algorithm}[h]
    \begin{algorithmic}
        \Require data ($X, y$); number of runs $K$; nominal level $q \in [0,1]$
        \Staten Choose sequence $\{q_k\}_{k=1}^{K} \in [0,1]$ such that $q = \sum_{k=1}^{K} q_k$.
        \Forn  {$k \in \{1,\dots,K\}$} 
        \State Apply knockoff/knockoff+ with nominal level $q_k$ and obtain selection set $\hat{\mathcal{S}}_{q_k}$.
        \EndForn
        \State Result: $K$ different selection sets $\hat{\mathcal{S}}_{q_1},\dots, \hat{\mathcal{S}}_{q_K}$.
        \Staten  Aggregate the selection sets by taking their union
        \begin{align}
            \hat{\mathcal{S}}_{q}^{U} = \bigcup_{k=1}^{K} \hat{\mathcal{S}}_{q_k}.
        \end{align} 
        \Ensure Aggregated set $\hat{\mathcal{S}}_{q}^{U}$ 
    \end{algorithmic}
\end{algorithm}

\end{document}

相关内容