# algorithmicx or algpseudocode custom command indentation within a while loop or if statement

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{\item\algorithmicset\ #1 \algorithmicto\ #2}
\algnewcommand\algorithmicfromkeyboard{\textbf{FROM KEYBOARD}}
\algnewcommand\algorithmicsend{\textbf{SEND}}
\algnewcommand\algorithmictodisplay{\textbf{TO DISPLAY}}
\algnewcommand\SEND{\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}
\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: 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.

• 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. Jun 3 '14 at 14:25

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: \documentclass{article}

\usepackage{algpseudocode}
\algnewcommand\algorithmicset{\textbf{SET}}
\algnewcommand\algorithmicto{\textbf{TO}}
\algnewcommand\SET{\State\algorithmicset\ #1 \algorithmicto\ #2}
\algnewcommand\algorithmicfromkeyboard{\textbf{FROM KEYBOARD}}
\algnewcommand\algorithmicsend{\textbf{SEND}}
\algnewcommand\algorithmictodisplay{\textbf{TO DISPLAY}}
\algnewcommand\SEND{\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}
\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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.71320647,"math_prob":0.9056875,"size":2175,"snap":"2021-31-2021-39","text_gpt3_token_len":610,"char_repetition_ratio":0.22892676,"word_repetition_ratio":0.0,"special_character_ratio":0.22390805,"punctuation_ratio":0.062111802,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99612015,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-22T06:44:45Z\",\"WARC-Record-ID\":\"<urn:uuid:ee15c6e2-c15f-49f7-85de-c829177b8656>\",\"Content-Length\":\"171994\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:34c0f2c7-e1ad-4b1d-8dd5-a0cf92438597>\",\"WARC-Concurrent-To\":\"<urn:uuid:fd648c42-967a-453e-b576-15971f8709e5>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/182948/algorithmicx-or-algpseudocode-custom-command-indentation-within-a-while-loop-or\",\"WARC-Payload-Digest\":\"sha1:V2ZCKTMA5XWIWSSWI3OQNPT6KCWIDYEB\",\"WARC-Block-Digest\":\"sha1:YLZLU3AQDXE4JQAX5W53ZJMYHCTEDEW6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057329.74_warc_CC-MAIN-20210922041825-20210922071825-00399.warc.gz\"}"}