[Tex/LaTex] Code is a mess because I cannot indent inside a for loop in the algorithm

algorithmicxalgorithmsindentation

I have a for loop in my algorithm and I can't get latex to properly indent the pseudocode inside it. It's just a mess at the moment, it won't even put the code in the for loop on separate lines.

\documentclass[a4paper,12pt,times,numbered,print,index]{Classes/PhDThesisPSnPDF}

\RequirePackage[left=37mm,right=30mm,top=35mm,bottom=30mm]{geometry}
\RequirePackage{libertine} 
\RequirePackage[small,bf]{caption}
\RequirePackage[labelsep=space,tableposition=top]{caption} 
\usepackage{subfig}  
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{braket}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\begin{document}
\begin{algorithm}
\caption{Generate transition matrix for one-dimensional quantum walk}\label{tran_mat_1d_qw}
\begin{algorithmic}[1]
\Procedure{generateTransitionMatrix}{$steps$}
    \State $numElements \gets steps * 4 + 2$
    \State $transitionMat \gets numElements \times numElements$ MATRIX

    \BlankLine
    \State $col \gets 0$
    \For {$i = 1 \to (steps * 2) - 1$}
        \State row \gets i * 2$
        \STATE\hspace{\algorithmicindent} transitionMat[row,      col]        \gets $1$
        \STATE\hspace{\algorithmicindent} transitionMat[row,      col + 1]    \gets $1$
        \STATE\hspace{\algorithmicindent} transitionMat[row + 5,  col]        \gets $1$
        \STATE\hspace{\algorithmicindent} transitionMat[row + 5,   col + 1]   \gets $-1$
    \ENDFOR
\end{algorithmic}
\end{algorithm}
\end{document}

Best Answer

I don't know how \BlankLines is defined, so I've removed that. If you want an unnumbered, empty line use \Statex.

There are numerous problems with your code.

  • \gets must be placed in math mode, i.e. it needs to be within a pair of dollar signs. When outside math mode it generates the missing $ inserted error.

  • In the first \State in the for loop you have forgotten the opening $, there is just the one at the end of the line.

  • Macros are case sensitive, this means that \State is not the same as \STATE. The algorithmicx package defines the former, so change all \STATE to \State. Similarly, \ENDFOR should be \EndFor (read the documentation for algorithmicx/algpseudocode).

    These mistakes generate Undefined control sequence errors.

  • You have to close the \Procedure with an \EndProcedure. This generates an error from algorithmicx saying Some blocks are not closed!!!.

Other comments:

  • When writing words (as opposed to variables, such as x), use either \text{the word(s)} or \mathrm{the word(s)}, which typesets them in a roman font.

  • You use \times one place, and * other places, use \times consistently.

  • Concerning your preamble: You load caption twice, with different arguments, why? Add all the arguments to a single call of caption. You also load both algorithm and algpseudocode twice, which is pointless.

    Finally, the convention is to use \usepackage and not \RequirePackage in preambles, though they do the same thing. See e.g. https://tex.stackexchange.com/a/19933/586


In the code below I removed the unused packages.

enter image description here

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

\begin{document}
\begin{algorithm}
\caption{Generate transition matrix for one-dimensional quantum walk}\label{tran_mat_1d_qw}
\begin{algorithmic}[1]
\Procedure{generateTransitionMatrix}{$\mathrm{steps}$}
    \State $\mathrm{numElements} \gets \mathrm{steps} \times 4 + 2$
    \State $\mathrm{transitionMat} \gets \mathrm{numElements} \times \mathrm{numElements}$ MATRIX
    \State $\mathrm{col} \gets 0$
    \For {$i = 1 \to (\mathrm{steps} \times 2) - 1$}
        \State $\mathrm{row} \gets i \times 2$
        \State\hspace{\algorithmicindent} $\mathrm{transitionMat[row,      col]       } \gets 1$
        \State\hspace{\algorithmicindent} $\mathrm{transitionMat[row,      col + 1]}    \gets 1$
        \State\hspace{\algorithmicindent} $\mathrm{transitionMat[row + 5,  col]       } \gets 1$
        \State\hspace{\algorithmicindent} $\mathrm{transitionMat[row + 5,   col + 1]}   \gets -1$
    \EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}