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.


\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

\caption{Generate transition matrix for one-dimensional quantum walk}\label{tran_mat_1d_qw}
    \State $numElements \gets steps * 4 + 2$
    \State $transitionMat \gets numElements \times numElements$ MATRIX

    \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$
  • 1
    It is helpful if your MWE starts with \documentclass and includes all your packages and definitions so that it can be compiled readily by those trying to help you. – Steven B. Segletes Apr 4 '14 at 19:34
  • @StevenB.Segletes I have edited in the package being used. – sonicboom Apr 4 '14 at 19:40
  • 1
    That's better, but it still does not compile (\begin{document} must follow the \usepackage declarations). Also, since the document class is not standard, the rest of us can't access it. Are you able to recreate the problem using a standard document class like article? If so, that makes for a much more useful MWE. Additionally, things like \BlankLine are not defined by the standard document classes. While I can guess what it might mean, it makes for a lot of work for those trying to help. – Steven B. Segletes Apr 4 '14 at 19:44
  • @StevenB.Segletes I am using the online tool ShareLatex to build my document. I wasn't aware that \begin{document} must follow the \usepackage declarations, I will take note of that for future question. I just tried using article as my document class and the document failed to compile :( – sonicboom Apr 4 '14 at 19:49
  • 2
    Well, I am not able to help, but I did find a listing for this class at raw.githubusercontent.com/unaguil/thesis-latex/master/Latex/…, if that might help others... – Steven B. Segletes Apr 4 '14 at 19:57

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

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

\caption{Generate transition matrix for one-dimensional quantum walk}\label{tran_mat_1d_qw}
    \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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.