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 writing a simple pseudocode, but I have a problem with a repeat..until block. Basically what happens is that the return statement that follows the end of this loop gets rendered on the same line as the until clause, which is ugly, but I can't find a way to put it on a line of its own with the correct indentation.

Here you can have an example:

\documentclass[a4paper,10pt]{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{test di Fermat}\label{alg:test-fermat}
\begin{algorithmic}[1]
\Procedure{testFermat}{$n, prove$}
    \Repeat
        \State{$a \gets$ numero casuale tra 2 e $n-1$}
        \If{$a^n \not\equiv a \bmod n$}
            \Return composto
        \EndIf
        \State{$prove \gets prove - 1$}
    \Until{$prove > 0$}
    \Return forse primo
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

I've tried to add a linebreak using \\, but this breaks the indentation. I've tried using \algorithmicindent to add indentation manually, but this displays "1.5em" instead of a whitespace. I've tried to add a \State or \Statex after the \Until, but this also breaks the indentation.

Is there a simple way to put the last \Return on a line on its own, without breaking the indentation?

By the way, this happens also with the return inside the If block, but I'm more worried about the one with the until, since it doesn't look so bad with the if.

edit: I've read this question, but it does not solve the problem.

I don't want to use varwidth because, since I have to fix the until loop, I'd have to manage by hand all indentations[starting a varwidth environment in the middle of the loop breaks other indentations].

share|improve this question
    
When I use \State before each \Return, it's put on its own line with the correct indentation. See this image of the output. Is this what you're after? –  Werner Aug 30 '12 at 14:43
    
Exactly. Thank you a lot Werner. By the way, why does this happen? Is \Return not considered a proper statement? You could reply in a new answer so that I can accept it. –  Bakuriu Aug 30 '12 at 14:59

1 Answer 1

up vote 4 down vote accepted

This was perhaps a status-by-design choice to allow people to choose between having \Return be on the same line with other statements or be placed on its own. To have it be placed on its own by default add

\algrenewcommand\Return{\State \algorithmicreturn{} }%

to your document preamble.

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage[noend]{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algrenewcommand\Return{\State \algorithmicreturn{} }%
\begin{document}
\begin{algorithm}
  \caption{test di Fermat}\label{alg:test-fermat}
  \begin{algorithmic}[1]
    \Procedure{testFermat}{$n, prove$}
      \Repeat
        \State{$a \gets$ numero casuale tra 2 e $n-1$}
        \If{$a^n \not\equiv a \bmod n$}
          \Return composto
        \EndIf
        \State{$prove \gets prove - 1$}
      \Until{$prove > 0$}
      \Return forse primo
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{document}​
share|improve this answer

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.