# Incorrect indentation for the “end” word of a for loop

I'm writing a pseudo code in latex using algorithm package, however, I have problem with indentation of the last end word for the for-loop if this for-loop contains an if statement. If it doesn't contain the if statement, all looks fine.

Example with no if statement and correct indentation:

Example with an if statement and incorrect indentation:

The code used to generate the latter one is following (just remove the if part to get the code used for first example):

\begin{algorithm}[H]
\caption{Test algorithm}
\SetAlgoLined

\For{$t \in T$}{
$t \gets$ 1

\If{ (t) \in P} {
t \gets 0

}
}

\end{algorithm}

Do you know how can this be fixed? Thanks

• Are you sure about "using algorithm package"? The \SetAlgoLined seems to be introduced by the algorithm2e package. Could you please add a minimal working example (MWE) to your question? Also, do you recieve any error messages? If so, please don't ignore then. Even if you get something that on first glance resembles a pdf file, there can still be issues with it. After an error, TeX only tries to recover enough to syntax check more of the file, it does not try to make sensible output after an error. – leandriis Jul 7 at 16:56

From your usage of \SetAlgoLined I assume that you are actually using the algorithm2e package. If I make a MWE with this package, I recieve the following error message Missing $inserted. that is caused by \in and \gets being used in text mode in line 12 and 13. If I add the missing$s, the MWE compiles perfectly fine and gives the desired result:

\documentclass{article}
\usepackage[ruled]{algorithm2e}

\begin{document}
\begin{algorithm}[H]
\caption{Test algorithm}
\SetAlgoLined

\For{$t \in T$}{
$t \gets$ 1

\If{ $(t) \in P$} {
$t \gets 0$

}
}

\end{algorithm}
\end{document}
• Thanks a lot, that was exactly the problem and sorry for stating misleading package name! – leopik Jul 7 at 17:08