8

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}
  • Welcome to TeX.SE. You can control the indentation via \SetInd{0.1ex}{0.1ex}, where the first parameter is the space before, and the 2nd is the space after. It would be helpful if you turned your code into a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Mar 13 '14 at 22:27
  • It worked. I was using 'em', instead of 'ex' within parameters. I will put complete code next time. :) – user1890924 Mar 13 '14 at 22:34
  • Oppps, em is actually the correct unit to use for horizontal spacing. – Peter Grill Mar 13 '14 at 22:36
8

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}

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.