4

I have been trying all day to set-up the commands necessary to represent the Haggis pseudocode format using the algorithmicx package in Latex. I have managed to create the commands I need for the simple algorithm I am describing but I can't get the indentation to appear correctly.

In my main document I am defining the following new commands and my algorithm below it:

\documentclass{report}

\usepackage{algpseudocode}
\algnewcommand\algorithmicset{\textbf{SET}}
\algnewcommand\algorithmicto{\textbf{TO}}
\algnewcommand\SET[2]{\item\algorithmicset\ #1 \algorithmicto\ #2}
\algnewcommand\algorithmicreceive{\textbf{RECEIVE}}
\algnewcommand\algorithmicfromkeyboard{\textbf{FROM KEYBOARD}}
\algnewcommand\RECEIVE[1]{\item\algorithmicreceive\ #1 \algorithmicfromkeyboard}
\algnewcommand\algorithmicsend{\textbf{SEND}}
\algnewcommand\algorithmictodisplay{\textbf{TO DISPLAY}}
\algnewcommand\SEND[1]{\item\algorithmicsend\ #1 \algorithmictodisplay}
\algrenewcommand\algorithmicwhile{\textbf{WHILE}}
\algrenewcommand\algorithmicend{\textbf{END}}
\algrenewcommand\algorithmicdo{\textbf{DO}}
\algrenewcommand\algorithmicif{\textbf{IF}}
\algrenewcommand\algorithmicthen{\textbf{THEN}}

\begin{document}

\begin{algorithm}[H]
    \caption{Hello world}
\begin{algorithmic}[1]
\SET{$number$}{$0$}
\While{$number \neq -1$}
    \SEND{$"Please\ enter\ the\ next\ number\ (-1\ to\ end):\ "$}
    \RECEIVE{$number$}
    \If{$number \neq -1$}
        \SET{$number$}{$total\ +\ number$}
    \EndIf
\EndWhile
\SEND{$total$}
\end{algorithmic}
\end{algorithm}

\end{document}

When I create the PDF I get the following:

Final representation

There is something wrong with the either the commands I have created or there is something else that I need to do that I am unaware of. I have searched for sometime without success and I would appreciate any assistance with this problem as I am tearing my hair out! No doubt it is something obvious I have overlooked.

1
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Commented Jun 3, 2014 at 14:25

1 Answer 1

3

While algorithmicx sets an algorithm as a list - implying that \item should work as an instruction-setting command - \State is the preferred setting mechanism which uses the appropriate indentation at that level:

enter image description here

\documentclass{article}

\usepackage{algpseudocode}
\algnewcommand\algorithmicset{\textbf{SET}}
\algnewcommand\algorithmicto{\textbf{TO}}
\algnewcommand\SET[2]{\State\algorithmicset\ #1 \algorithmicto\ #2}
\algnewcommand\algorithmicreceive{\textbf{RECEIVE}}
\algnewcommand\algorithmicfromkeyboard{\textbf{FROM KEYBOARD}}
\algnewcommand\RECEIVE[1]{\State\algorithmicreceive\ #1 \algorithmicfromkeyboard}
\algnewcommand\algorithmicsend{\textbf{SEND}}
\algnewcommand\algorithmictodisplay{\textbf{TO DISPLAY}}
\algnewcommand\SEND[1]{\State\algorithmicsend\ #1 \algorithmictodisplay}
\algrenewcommand\algorithmicwhile{\textbf{WHILE}}
\algrenewcommand\algorithmicend{\textbf{END}}
\algrenewcommand\algorithmicdo{\textbf{DO}}
\algrenewcommand\algorithmicif{\textbf{IF}}
\algrenewcommand\algorithmicthen{\textbf{THEN}}

\begin{document}

\begin{algorithmic}[1]
  \SET{$number$}{$0$}
  \While{$number \neq -1$}
    \SEND{$"Please\ enter\ the\ next\ number\ (-1\ to\ end):\ "$}
    \RECEIVE{$number$}
    \If{$number \neq -1$}
      \SET{$number$}{$total\ +\ number$}
    \EndIf
  \EndWhile
  \SEND{$total$}
\end{algorithmic}

\end{document}

You'll notice all \items are changed to \States.

0

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.