[Tex/LaTex] How to reduce indentation size within loop of an algorithm

algorithm2ealgorithmsindentation

For the following algorithm, I get four spaces for each text line inside the loop. How can I get only two or three spaces? Some of my text lines go to next line. I want to save some horizontal spaces.

\usepackage[noline,boxruled,commentsnumbered,linesnumbered,titlenumbered]{algorithm2e}

\IncMargin{0.5em}
\begin{algorithm}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\SetKwFor{Foreach}{for each}{do}{endfor}
\SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}
\BlankLine
\Input{A method \emph{m}}
\Output{result}
\BlankLine
\Foreach { $x \in M_{x}$}{
 do something
}
\caption{Algorithm}\label{Method}
\end{algorithm}

Best Answer

Use

\SetInd{<space before>}{<space after>}

to control the spacing before and after the vertical rule (which is disabled via that noline option). Here is a comparison of the default setting and one with \SetInd{0.25em}{0.1em}:

enter image description here

Code:

\documentclass{article}
\usepackage[noline,boxruled,commentsnumbered,linesnumbered,titlenumbered]{algorithm2e}

\IncMargin{0.5em}
\begin{document}
\noindent
Without setting \verb|\SetInd{}{}|: 
\begin{algorithm}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\SetKwFor{Foreach}{for each}{do}{endfor}
\SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}
\BlankLine
\Input{A method \emph{m}}
\Output{result}
\BlankLine
\Foreach { $x \in M_{x}$}{
 do something
}
\caption{Algorithm}\label{Method}
\end{algorithm}

\noindent
With \verb|\SetInd{0.25em}{0.1em}|:
\SetInd{0.25em}{0.1em}
\begin{algorithm}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\SetKwFor{Foreach}{for each}{do}{endfor}
\SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}
\BlankLine
\Input{A method \emph{m}}
\Output{result}
\BlankLine
\Foreach { $x \in M_{x}$}{
 do something
}
\caption{Algorithm}\label{Method}
\end{algorithm}
\end{document}