如何在沒有 end 關鍵字的情況下正確在演算法偽代碼中建立垂直線縮排?

如何在沒有 end 關鍵字的情況下正確在演算法偽代碼中建立垂直線縮排?

我找到了這個回答我應用了它,但這就是結果。

在此輸入影像描述

這是我的程式碼。

\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。我還需要不帶關鍵字的偽代碼,如圖所示。ofend

謝謝。

編輯

我可以更改\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}

在此輸入影像描述

相關內容