3

Is there an algorithm package that allows me to achieve the style of the algorithms where each number is inside a circle in an algorithm? I am interested specifically in the numbering of the steps by encircled numbers.

%%%% ijcai21.tex

\typeout{IJCAI--21 Instructions for Authors}

% These are the instructions for authors for IJCAI-21.

\documentclass{article} \pdfpagewidth=8.5in \pdfpageheight=11in % The file ijcai21.sty is NOT the same than previous years' \usepackage{ijcai21}

% Use the postscript times font! \usepackage{times} \usepackage{soul} \usepackage{url} \usepackage[hidelinks]{hyperref} \usepackage[utf8]{inputenc} \usepackage[small]{caption} \usepackage{graphicx} \usepackage{amsmath} \usepackage{amsthm} \usepackage{booktabs} \usepackage{algorithm} \usepackage{algorithmic} \urlstyle{same}

\newtheorem{example}{Example} \newtheorem{theorem}{Theorem}

\title{}

\begin{document}

\maketitle

\begin{algorithm}[tb] \caption{algorithm} \label{alg:algorithm} \textbf{Input}: Your Input\ \textbf{Output}: Your output\ \begin{algorithmic}[1] %[1] enables line numbers \STATE a = first \ \STATE b = second\ \STATE sum = first - second\

\end{algorithmic} \end{algorithm}

\end{document}

Werner
  • 603,163
  • Welcome to TeX.se! Your question is very clear, but your sample code could be made a lot more minimal as in Werner's answer. Apart from packages that aren't required for the question, your code requires a package that isn't even available on CTAN. – Alan Munn Dec 29 '20 at 17:54

1 Answers1

3

You'll have to change the formatting of the numbers to be circled:

enter image description here

\documentclass{article}

\usepackage{algorithm,algorithmic} \usepackage{circledsteps}% https://tex.stackexchange.com/q/7032/5764

\newcommand{\circlenum}[3]{\Circled{\small #1{#2}}} \algsetup{linenosize=\circlenum}

\begin{document}

\begin{algorithm} \caption{An algorithm} \begin{algorithmic}[1] \STATE First statement \STATE Second statement \STATE Third statement \end{algorithmic} \end{algorithm}

\end{document}

The construction of the line number within algorithmic is done in 4 parts:

  1. Line number font
  2. Line number representation
  3. Line number counter
  4. Line number separator

The above approach updates the font macro (1) to grab all remaining arguments and only use the counter and its representation - (2) and (3), discarding the separator (as it's not needed). That's why you only see #1 and #2 in the \circlenum macro even though it's defined to have/take 3 arguments. The last argument - the line separator - is just gobbled.


For starting the algorithmic counter at 0 rather than 1, use the following patch:

\usepackage{xpatch}
\xpatchcmd{\algorithmic}% <cmd>
  {\setcounter{ALC@line}{0}}% <search>
  {\setcounter{ALC@line}{-1}}% <replace>
  {}{}% <success><failure>
Werner
  • 603,163