24

I am using ACM format for writing a paper. I use the following algorithm sample that is taken from this form. I want to add input and output before MyProcedure. I tried many ways but all fails. Can someone guide me? Thank you

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
    \begin{algorithm}
    \caption{My algorithm}\label{euclid}
    \begin{algorithmic}[1]
     % Input:
     % Output: 
    \Procedure{MyProcedure}{}
    \State $\textit{stringlen} \gets \text{length of }\textit{string}$
    \State $i \gets \textit{patlen}$
    \BState \emph{top}:
    \If {$i > \textit{stringlen}$} \Return false
    \EndIf
    \State $j \gets \textit{patlen}$
    \BState \emph{loop}:
    \If {$\textit{string}(i) = \textit{path}(j)$}
    \State $j \gets j-1$.
    \State $i \gets i-1$.
    \State \textbf{goto} \emph{loop}.
    \State \textbf{close};
    \EndIf
    \State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
    \State \textbf{goto} \emph{top}.
    \EndProcedure
    \end{algorithmic}
    \end{algorithm}
5
  • Input x,y with:\Procedure{Algo name}{$x,y$}. Or \Comment{Input: } and \Comment{Output:} Commented Feb 27, 2017 at 6:58
  • 1
    Here is an example: tex.stackexchange.com/a/215651/124842 Commented Feb 27, 2017 at 8:13
  • @Bobyandbob Would you like to write up an answer, or is that question a duplicate? (I didn't look at it.) Commented Jun 3, 2017 at 21:46
  • @TorbjørnT. No the link is an alternativ with algorithm, but Steven do not respond to comments. Nevertheless i added some ideas. Commented Jun 4, 2017 at 10:07
  • Does the answer fits your question? Commented Feb 23, 2018 at 23:27

2 Answers 2

25

Answer:

To add input and output before MyProcedure a possibility is to add the text before \begin{algorithmic} with starred hspace \hspace*{}(see 6.3.3 Horizontal Space) and the indentation \algorithmicindent (see 4.1 Blocks and loops). LaTeX normally removes horizontal space at the beginning of a line, to preserve this space, use the starred version.

 ...
 \hspace*{\algorithmicindent} \textbf{Input} \\
 \hspace*{\algorithmicindent} \textbf{Output} 
 \begin{algorithmic}[1]
 ...

enter image description here

Complete full MWE: A minimal working example (MWE) starts with \documentclass and ends with \end{document}.

To complete the MWE and to avoid the following error:

! Undefined control sequence. \BState ->\State \algbackskip l.24 \BState

You have to use the following definition (the solution is from @Werner see: Undefined control sequence.\BState ->\State \algbackskip \BState):

\makeatletter
% Reinsert missing \algbackskip
\def\algbackskip{\hskip-\ALG@thistlm}
\makeatother

MWE:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
% Reinsert missing \algbackskip
\def\algbackskip{\hskip-\ALG@thistlm}
\makeatother

\begin{document}
    \begin{algorithm}
    \caption{My algorithm}\label{euclid}
    \hspace*{\algorithmicindent} \textbf{Input} \\
    \hspace*{\algorithmicindent} \textbf{Output} 
    \begin{algorithmic}[1]
    \Procedure{MyProcedure}{}
%    \Procedure{MyProcedure}{$x,y$}
%     % Input:
%     \Comment{Input: x}
%     % Output:
%     \Comment{Output:y}
    \State $\textit{stringlen} \gets \textit{length of } \textit{string}$
    \State $i \gets \textit{patlen}$
    \BState \emph{top}:
    \If {$i > \textit{stringlen}$} \Return false
    \EndIf
    \State $j \gets \textit{patlen}$
    \BState \emph{loop}:
    \If {$\textit{string}(i) = \textit{path}(j)$}
    \State $j \gets j-1$.
    \State $i \gets i-1$.
    \State \textbf{goto} \emph{loop}.
    \State \textbf{close};
    \EndIf
    \State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
    \State \textbf{goto} \emph{top}.
    \EndProcedure
    \end{algorithmic}
    \end{algorithm}
\end{document} 

enter image description here

Alternative: You can add the input and output after MyProcedure with \Procedure{MyProcedure}{$x,y$} ...

enter image description here

or \Comment{Input: x}/\Comment{Output: y}.

enter image description here

5
  • How could I add comments in your code, I have tried \tcp*{comment} which did not work Commented Nov 2, 2021 at 23:26
  • With \Comment{Your comment} for example. See MWE. Commented Nov 15, 2021 at 11:00
  • What does MWE stand for? Commented Nov 15, 2021 at 12:10
  • 2
    A minimal working example (MWE), shown in the answer. You just have to remove %. Does it help?(\Comment{Your comment}) Commented Nov 15, 2021 at 13:04
  • yes sir thank you Commented Nov 15, 2021 at 15:01
10

If one is using the algorithmic environment, one can make \REQUIRE and \ENSURE to print Input and Output by adding in the preamble:

\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
2
  • 9
    Isn't it a good idea to keep \REQUIRE and \ENSURE as it are, and define custom commands for \INPUT and \OUTPUT? Commented Jun 6, 2019 at 20:12
  • 2
    I agree with the author since this is the exact same thing recommended in the section [3.14.3 Customization] in the documentation for the library: texdoc.org/pkg/algorithms Commented Apr 20, 2022 at 15:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.