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 trying to write some simple pseudocode but I just can't get it to work. It's pseudocode from another paper and I want to change it a little and put it in my paper.

enter image description here

This is the pseudocode I want to write. I tried algorithmicx, algorithm2e and pseudocode but I can't manage to do it with any of them. Are there easier packages for writing pseudocode? I spent the whole morning trying to write an other piece of pseudocode using algorithm2e and it took way too long and the result still looks bad.

And now I get an error that is just ridiculous.

    ! Argument of \algocf@If has an extra }.
    <inserted text> 
                    \par 
    l.98            \If
                 {all variables assigned}

This is my code (it should be the same as the image):

    \begin{algorithm}
    \begin{algorithmic}
        \Loop 
            propagate() - propagate unit clauses
            \If {not conflict} 
                \If {all variables assigned} 
                    \return SATISFIABLE
                \Else
                    decide() - pick a new variable and assign it
                \EndIf
            \Else
                analyse() - analyze comflict and add a conflict clause
                \If{top-level conflict found} 
                    \return UNSATISFIABLE
                \Else
                    backtrack() - undo assignments until conflict clause is unit
                \EndIf
            \EndIf
        \EndLoop
    \end{algorithmic}
    \caption{miniSAT\label{lss}}
    \end{algorithm}

sigh, so frustrating, I hate latex so much why isn't there an alternative.

Anyway, if you can help me, thanks a lot, I'm really desperate at this point.

share|improve this question
1  
Please, make this into a minimal example, adding a document class and the packages you load for the algorithms. –  egreg Oct 11 '12 at 14:41
    
@egreg Having \label{lss} within \caption, can that affect the compilation? –  azetina Oct 11 '12 at 14:46
    
@azetina Not at all. The problem is that I don't understand what package provides that syntax for pseudocode. From the error message it seems that algorithm2e is involved, but its syntax is completely different (and it doesn't provide algorithmic). –  egreg Oct 11 '12 at 14:49

2 Answers 2

up vote 6 down vote accepted

It's not very clear what you're using; there's probably a package conflict and also wrong syntax. This produces an output that should be what you want:

\documentclass{article}

\usepackage{algorithm,algorithmic}

\begin{document}

\begin{algorithm}
\begin{algorithmic}
  \LOOP 
    \STATE  propagate() - propagate unit clauses
    \IF {not conflict} 
      \IF {all variables assigned} 
        \RETURN SATISFIABLE
      \ELSE
        \STATE decide() - pick a new variable and assign it
      \ENDIF
    \ELSE
      \STATE analyse() - analyze comflict and add a conflict clause
      \IF{top-level conflict found} 
        \RETURN UNSATISFIABLE
      \ELSE
        \STATE backtrack() - undo assignments until conflict clause is unit
      \ENDIF
    \ENDIF
  \ENDLOOP
\end{algorithmic}
\caption{miniSAT\label{lss}}
\end{algorithm}

\end{document}

enter image description here

share|improve this answer
    
Yes, i also use algorithm2e in the same document. Is there a way so that i can use both packages? And thx for the help egreg and Werner. \usepackage{algorithm} \usepackage[ruled,noline]{algorithm2e} \usepackage{algpseudocode} This doesn't work and removing algorithm gives a bug with algpseudocode. –  Vincent Oct 13 '12 at 11:33
    
@Vincent I don't think that those packages are compatible with each other. –  egreg Oct 13 '12 at 12:19

The code snippet resembles the syntax of algpseudocode (from algorithmicx). I've redefined the required \Return command, and also defined \funccall (short for func tion call) that sets its argument in italics. It also seems like each functional call has an accompanying comment, which I've set using \Comment:

enter image description here

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

\algrenewcommand{\Return}{\State \textbf{return}\ }
\algnewcommand{\funccall}[1]{\textit{#1}}% Function call
\begin{document}
\begin{algorithm}
  \begin{algorithmic}
    \Loop 
      \State \funccall{propagate}() \Comment{propagate unit clauses}
      \If {not conflict}
        \If {all variables assigned}
          \Return SATISFIABLE
        \Else
          \State \funccall{decide}() \Comment{pick a new variable and assign it}
        \EndIf
      \Else
        \State \funccall{analyse}() \Comment{analyze comflict and add a conflict clause}
        \If{top-level conflict found}
          \Return UNSATISFIABLE
        \Else
          \State \funccall{backtrack}() \Comment{undo assignments until conflict clause is unit}
        \EndIf
      \EndIf
    \EndLoop
  \end{algorithmic}
  \caption{miniSAT\label{lss}}
\end{algorithm}
\end{document}

For each code segment to be on a new line, it has to use some form of \State command. \Loop, \If, \Else, ... are all defined in terms of \State and therefore don't need to explicitly be called as such. However, your function calls are unknown to algorithmic as being a \State, so you need to insert this manually.

Adding line numbers is as simple as using \begin{algorithmic}[1] instead of just \begin{algorithmic}.

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.