2

In algorithmic, I would like to write a loop that will look like the following:

   repeat $n$ times:
      [command]
   end

I found this question, explaining how to do this in algorithms2e, but it does not work with algorithmic.

EDIT: This is what I do now:

\documentclass{article}

\usepackage{algorithm} \usepackage{algorithmic}

\begin{document} \begin{algorithmic}[1] \WHILE{Repeated at most $T$ times} \STATE Do Stuff \ENDWHILE \end{algorithmic}

\end{document}

  • 1
    Please provide a full MWE (showing an algorithm with a normal loop for example that you want to change into a repeat n loop), expecially for pseudocode questions it is important to be very clear about the setup. – Marijn Feb 15 '23 at 11:58
  • @Marijn added example – Erel Segal-Halevi Feb 15 '23 at 12:06
  • 1
    We need something that can be compiled on https://texlive.net/run . ;-) – projetmbc Feb 15 '23 at 12:08
  • @ErelSegal-Halevi as projetmbc mentioned, this code is not compilable. Please add the documentclass and packages, begin/end document, and anything else needed to actually compile the code. Then we can be sure that we have the exact same setup as you, which guarantees that a solution also works for you. Moreover, it saves time for people trying to answer if they don't have to add boilerplate code themselves. – Marijn Feb 15 '23 at 12:12
  • @Marijn edited, thanks – Erel Segal-Halevi Feb 16 '23 at 09:20

2 Answers2

3

You can define a new block using algpseudocode: \RepeatN:

\documentclass{article}
\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}

\algdef{SE}[REPEATN]{RepeatN}{End}[1]{\algorithmicrepeat\ #1 \textbf{times}}{\algorithmicend}

\begin{algorithmic} \RepeatN{$n$} \State Do stuff \End \end{algorithmic}

\end{document}

For backwards compatibility with algorithmic you can use algcompatible:

\documentclass{article}
\usepackage{algcompatible}
\usepackage{algorithm}

\begin{document}

\algdef{SE}[REPEATN]{REPEATN}{END}[1]{\algorithmicrepeat\ #1 \textbf{times}}{\algorithmicend}

\begin{algorithmic} \REPEATN{$n$} \STATE Do stuff \END \end{algorithmic}

\end{document}

enter image description here

DG'
  • 21,727
  • This solution uses algpseudocode, but the project I work on uses algorithmic. When I add \usepacakge{algpseudocode} I get \algorithmic already defined. If I remove algorithmic, then some other parts of the code (e.g. \STATE) stop working. – Erel Segal-Halevi Feb 16 '23 at 09:22
  • algpseudocode is a layout for algorithmicx which is a more customizable replacement of algorithmic. The main difference is that only the first letter of the command is capitalized... – DG' Feb 16 '23 at 09:50
  • @ErelSegal-Halevi this illustrates the importance of a full compilable MWE, now you got an answer that you can't easily use because you didn't add your exact setup to your question at first. – Marijn Feb 16 '23 at 09:58
  • @Marijn You are right. I did not know that there are two versions of algorithmic in two different packages. – Erel Segal-Halevi Feb 16 '23 at 10:02
  • 1
    I added a version that is backwards compatible to algorithmic – DG' Feb 16 '23 at 10:06
3

For comparison a version that uses only the internals of the algorithmic package:

\documentclass{article}

\usepackage{algorithm} \usepackage{algorithmic}

\makeatletter \newcommand{\REPEATN}[1]{\ALC@it\algorithmicrepeat% \ #1 \textbf{times}\begin{ALC@rpt}}% \newcommand{\ENDREPEAT}{\end{ALC@rpt}\ALC@it\algorithmicend}% \makeatother

\begin{document} \begin{algorithmic}[1] \WHILE{Repeated at most $T$ times} \STATE Do Stuff \ENDWHILE \REPEATN{$n$} \STATE Do Stuff \ENDREPEAT \end{algorithmic}

\end{document}

enter image description here

You can see that the solution of DG' with algcompatible is easier to use.

Marijn
  • 37,699