# Alignment of line numbers

I am brand new to LaTeX, and am trying to create a short pseudo code section for a paper. The current issue I'm facing is that line numbers (and vertical alignment in general) is off for many lines.

Here is the code I currently have (I am using Texmaker on Linux for compiling).

\documentclass{article}
\usepackage{amsmath}
\usepackage[linesnumbered, ruled]{algorithm2e}
\usepackage[noend]{algpseudocode}
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{document}
{\SetAlgoNoLine
\begin{algorithm}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\caption{Leak Detection}
\begin{algorithmic}[1]
\Require{Set $C$ of all observations of interest}
\Ensure{Set $leaks$ of all identified $CH_{4}$ leak polygons}
\State 
\ForEach{record $c_{i}$ in $C$}{
\State $\textit{movingAvg} \gets \textit{avg.$CH_{4}$for past 2 min.}$
\State $\textit{movingSD} \gets \textit{SD of$CH_{4}$for past 2 min.}$
\State $\textit{movingThreshold} \gets \max(1 SD > movingAvg, 10\% > movingAvg)$

\If {$CH_{4} level of c_{i} > movingThreshold$}{
\State $\textit{flag$c_{i}$as elevated}$
\State $\textit{buffer$c_{i}$location with 20m radius}$
}
}
\State $leaks \gets \textit{merge of all intersecting$c_{i}$buffers}$
\State 
\ForEach{Polygon $p \in leaks$}{
\State $\textit{mark centroid of p}$
}
\Return $leaks$
\end{algorithmic}
\end{algorithm}}
\end{document}


And the output looks like this:

I cannot figure out why none of the lines in the foreach sections are not indented,and also why the line numbers are not cleanly lined up with each other.

I am trying to get lines 2-6 indented inside the first foreach, and line 9 indented within the second foreach. The final return statement is not supposed to be indented, but it is. What am I missing here?

You're actually mixing the notations of algorithm2e and algorithmicx. Since the larger portion of your algorithm follows the algorithmicx methodology (and macros), the following is an update of your setup that provides the output you desire:

\documentclass{article}

\usepackage{amsmath,algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\newcommand{\algorithmicforeach}{\textbf{foreach}}
\newcommand{\var}{\texttt}
\algdef{SE}[FOREACH]{ForEach}{EndForEach}[1]{\algorithmicforeach\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicforeach}%
\algtext*{EndForEach}
\newcommand{\AlgBlankLine}{\par\medskip}

\begin{document}

\begin{algorithm}
\caption{Leak Detection}
\begin{algorithmic}[1]
\Require{Set $C$ of all observations of interest}
\Ensure{Set $\var{leaks}$ of all identified $CH_4$ leak polygons}
\AlgBlankLine
\ForEach{record $c_i$ in $C$}
\State $\var{movingAvg} \gets \text{avg.\$CH_4$for past 2 min.}$
\State $\var{movingSD} \gets \text{SD of$CH_4$for past 2 min.}$
\State $\var{movingThreshold} \gets \max(1 SD > \var{movingAvg}, 10\% > \var{movingAvg})$
\If {$CH_4$ level of $c_i > \var{movingThreshold}$}
\State flag $c_i$ as elevated
\State buffer $c_i$ location with 20m radius
\EndIf
\EndForEach
\State $\var{leaks} \gets \text{merge of all intersecting$c_i$buffers}$
\AlgBlankLine
\ForEach{Polygon $p \in \var{leaks}$}
\State mark centroid of~$p$
\EndForEach
\State \Return $\var{leaks}$
\end{algorithmic}
\end{algorithm}

\end{document}


Here are my suggested changes:

• algorithm2e uses a \For{<condition>}{<body>} approach, while algorithmicx uses \For{<condition>} <body> \EndFor approach.
• algorithmicx doesn't have \ForEach defined by default.
• \Return under algorithmicx doesn't set a new \State, so that has to be added manually.
• Non-standard blocks require \algtext* to remove the ends.
• Create a macro for defining \variables; you can always change the formatting later, but it avoid you having something like movingSD being considered as the multiplication of math "constants" m, o, v, i, n, g, S and D.