为什么使用算法包时垂直规则有时会变成虚线?

为什么使用算法包时垂直规则有时会变成虚线?

我想使用算法包创建垂直缩进规则。我应用了此处提出的解决方案如何在没有结束关键字的情况下在算法伪代码中正确创建垂直线缩进?

但是,当我应用此解决方案时,我得到了如下的虚线垂直线:

在此处输入图片描述 并且随着代码的增加,破折号的数量也会增加。

我的 LaTeX 代码如下:

\documentclass[12pt,journal,compsoc,onecolumn]{IEEEtran}

\usepackage[lite,subscriptcorrection,slantedGreek,nofontinfo]{mtpro2}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{etoolbox}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
% 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}

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

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



\begin{algorithm}
    \caption{\textsc{Increase Algorithm}}
    \label{algo:ffig}
    \begin{algorithmic}[1]
        \Require{$T$.}
        \Ensure{A pair.}

        \For{$\ell\gets1$ \textbf{to} $L$}
            \For{$t\gets1$ \textbf{to} $T$}     
                \State $x_\ell^t\gets 0$
            \EndFor
        \EndFor         
        \For{$\ell\gets 1$ \textbf{to} $L$}
            \For{$t\gets1$ \textbf{to} $T$}
                \State $x_\ell^t\gets 1$
                \State $S_\ell^t\gets 0$
                \For{$\ell'\gets1$ \textbf{to} $L$}
                    \State $S_\ell^t\gets S_\ell^t + t$
                \EndFor

            \EndFor
        \EndFor
        \State \Return The solution
    \end{algorithmic}
\end{algorithm}


\end{document}

答案1

问题在于两行连续的线具有高而深的符号。

您可以使用稍大一点的规则深度来解决这个问题:

\newcommand*{\algrule}[1][\algorithmicindent]{%
    \makebox[#1][l]{%
        \hspace*{.2em}% <------------- This is where the rule starts from
        \vrule height .75\baselineskip depth .3\baselineskip
    }
}

在示例中,我没有\usepackage[sc]{mathpazo}加载mtpro2:Times 数学字体与类使用的 Palatino 字体不一致,但结果与相同mtpro2

在此处输入图片描述

相关内容