Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I’m using the packages algorithmicx and algpseudocode to write algorithms in my paper. It’s more convenient and beautiful to specify comments than with the algorithmic.

But I encounter an error while using “Require and Ensure.” The line's number is always wrong.

MWE

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}

\begin{algorithm}[ht]
\caption{My first algorithm}
\label{alg:algorithm1}

\begin{algorithmic}[1]
\Require~~\\
 If there are multiple lines here, line's number will be wrong.\\
 This error doesn't happen in package\{algorithmic\}
\Ensure~~\\
 If I write $\setminus\setminus$ here, line's number will be wrong too.
\Statex
\If{$i = 1 \to n$} \Comment{This line's number should be 1}
  \State ${\textit{HU}}_{i} \gets {\textit{NGU}}_{i} + {HU}_{i}$ \Comment{Comment}
\EndIf

\end{algorithmic}
\end{algorithm}
\end{document}

Output

Pdf image is here

Question

I need to write two lines in item “Require.” How can I tackle this line number problem?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

algorithmicx maintains the algorithmic environment as a list. You could therefore use the \Statex command to insert an unnumbered item, similar to inserting an empty line:

enter image description here

\documentclass{article}
\usepackage{algorithm,algpseudocode}% http://ctan.org/pkg/{algorithms,algorithmicx}
\begin{document}

\begin{algorithm}[ht]
  \caption{My first algorithm}\label{alg:algorithm1}
  \begin{algorithmic}[1]
    \Require
      \Statex If there are multiple lines here, line's number will be wrong.
      \Statex This error doesn't happen in package \verb|algorithmic|.
    \Ensure
      \Statex If I write \verb|\\| here, line's number will be wrong too.
    \Statex
    \If{$i = 1 \to n$} \Comment{This line's number should be 1}
      \State ${\textit{HU}}_{i} \gets {\textit{NGU}}_{i} + {HU}_{i}$ \Comment{Comment}
    \EndIf
  \end{algorithmic}
\end{algorithm}
\end{document}
share|improve this answer
    
Your answer is what I'm looking for. Thanks! By the way, how to add a horizontal line before line 1 (i.e., add a split line between Ensure statement and real algorithm) –  Lijie Xu Feb 18 '13 at 6:34
    
@LijieXu: That would be an entirely different question to this one. I suggest asking a follow-up question. Read the linked post carefully. –  Werner Feb 18 '13 at 6:40
    
Thanks for your advice. While I'm trying to post this new question, I find tex.stackexchange.com/questions/52456/… has solved this problem. –  Lijie Xu Feb 18 '13 at 6:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.