如何减少算法循环内的缩进大小

如何减少算法循环内的缩进大小

对于以下算法,我在循环内为每行文本获得四个空格。我怎样才能只获得两个或三个空格?我的一些文本行转到下一行。我想节省一些水平空格。

\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}

答案1

使用

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

控制垂直规则前后的间距(通过该noline选项禁用)。以下是默认设置和 的比较\SetInd{0.25em}{0.1em}

在此处输入图片描述

代码:

\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}

相关内容