如何在没有结束关键字的情况下在算法伪代码中正确创建垂直线缩进?

如何在没有结束关键字的情况下在算法伪代码中正确创建垂直线缩进?

我找到了这个回答我应用了它,但结果就是这样。

在此处输入图片描述

这是我的代码。

\documentclass[conference]{IEEEtran}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{dsfont}
\usepackage{algorithm}
\PassOptionsToPackage{noend}{algpseudocode}% comment out if want end's to show
\usepackage{algpseudocode}

\makeatletter
% start with some helper code
% This is the vertical rule that is inserted
\newcommand*{\algrule}[1][\algorithmicindent]{\makebox[#1][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

\newcount\ALG@printindent@tempcnta
\def\ALG@printindent{%
    \ifnum \theALG@nested>0% is there anything to print
    \ifx\ALG@text\ALG@x@notext% is this an end group without any text?
    % do nothing
    \addvspace{-3pt}% FUDGE for cases where no text is shown, to make the rules line up
    \else
    \unskip
    % draw a rule for each indent level
    \ALG@printindent@tempcnta=1
    \loop
    \algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
    \advance \ALG@printindent@tempcnta 1
    \ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax% can't do <=, so add one to RHS and use < instead
    \repeat
    \fi
    \fi
}%
\usepackage{etoolbox}
% the following line injects our new indent handling code in place of the default spacing
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother
% end vertical rule patch for algorithmicx
\makeatother


\begin{document}

\IEEEpeerreviewmaketitle

    \begin{algorithm}
        \caption{Arbitrary Algorithm}\label{IS2OSLS}
        \begin{algorithmic}[1]
            \Require A matrix $\mathbf{A}$ of size $m\times n$.
            \Ensure Something.
            \For{$i$ in $m$}
                \For{$j$ in $n$}
                    \If{$i=j$}
                        \State Select a random action
                    \Else
                        \If{$i=j+1$}
                            \State Stay silent 
                        \Else 
                            \State Break
                        \EndIf
                    \EndIf
                \EndFor
            \EndFor
        \end{algorithmic}
    \end{algorithm}
\end{document}

我希望垂直线刚好在关键字的第一个字母之后开始,即对于关键字,for垂直线应该在之后开始f,等等。在上图中,垂直线在之后开始,而o不是在之后f。此外,我需要没有关键字的伪代码,end如图所示。

谢谢。

编辑

我可以改为\hspace*{.5em}以便\hspace*{.1em}将垂直线稍微向左移动。这解决了。但是为什么在算法结束时线条会重叠?(如果我添加关键字,end问题就解决了。)

答案1

\addvspace导致重叠的指令;删除它。我还添加了另一个补丁,用于避免在省略“结束”标签时产生虚假垂直空间,请参阅使用 algpseudocode 和 noend 的虚假空白

对于向左移动的规则,\hspace{.5em}按照你所发现的进行操作;在我使用的例子中.2em

在下面的代码中我只留下了必要的包。

\documentclass[conference]{IEEEtran}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{etoolbox}

\makeatletter
% start with some helper code
% This is the vertical rule that is inserted
\newcommand*{\algrule}[1][\algorithmicindent]{%
  \makebox[#1][l]{%
    \hspace*{.2em}% <------------- This is where the rule starts from
    \vrule height .75\baselineskip depth .25\baselineskip
  }
}

\newcount\ALG@printindent@tempcnta
\def\ALG@printindent{%
    \ifnum \theALG@nested>0% is there anything to print
    \ifx\ALG@text\ALG@x@notext% is this an end group without any text?
    % do nothing
    \else
    \unskip
    % draw a rule for each indent level
    \ALG@printindent@tempcnta=1
    \loop
    \algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
    \advance \ALG@printindent@tempcnta 1
    \ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax
    \repeat
    \fi
    \fi
}
% the following line injects our new indent handling code in place of the default spacing
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\patchcmd{\ALG@doentity}{\item[]\nointerlineskip}{}{}{} % no spurious vertical space
% end vertical rule patch for algorithmicx
\makeatother

\begin{document}

\IEEEpeerreviewmaketitle

\begin{algorithm}
\caption{Arbitrary Algorithm}\label{IS2OSLS}

\begin{algorithmic}[1]
  \Require A matrix $\mathbf{A}$ of size $m\times n$.
  \Ensure Something.
  \For{$i$ in $m$}
    \For{$j$ in $n$}
      \If{$i=j$}
        \State Select a random action
      \Else
        \If{$i=j+1$}
          \State Stay silent 
        \Else 
          \State Break
        \EndIf
      \EndIf
    \EndFor
  \EndFor
\end{algorithmic}

\end{algorithm}

\end{document}

在此处输入图片描述

相关内容