TransWikia.com

Customized numeration in algorithmic

TeX - LaTeX Asked by Leo96 on June 25, 2021

I want to customize the numbering inside an algorithm. Here is a short 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}

More specifically, I want to enumerate only the following lines marked in blue. To be more precise, I don’t want the enumeration that results from beginalgorithmic, I just want to place a "1, 2 and 3" in front of the three lines there to make clear that these are 3 steps (and not 3 code lines).

enter image description here

One Answer

Two new commands --Staten and Forn-- have been defined to add an incremental prefix (with a dot) to State and For.

The counter used (enumstate) must be reset (if so desired) after each 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
algnewcommandalgorithmicforn{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}

Correct answer by Simon Dispa on June 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP