algpseudocode 中的缩进范围行断开

algpseudocode 中的缩进范围行断开

我正在尝试提供一条垂直线来显示算法中的缩进。我正在关注这个答案,但是当我添加一些高于行高的数学符号时,行就会断开。这是结果,我也提供了代码。这个问题可以修复吗?

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath}                              % For \eqref
\usepackage{amssymb}                             % For mathematical symbols
\usepackage{amsbsy}                                % For bold symbol greek letters

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


\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

% for bmatrix with alignment
\makeatletter
  \renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
    \hskip -\arraycolsep
    \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother



\begin{document}

   \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$}
                   \State{$\mathbf{X} \gets \begin{bmatrix}[rrr]  \boldsymbol x_i  & \boldsymbol x_j & \ldots \end{bmatrix}^\intercal $} \Comment{get coordinates}

                \For{$j$ in $n$}
                    \If{$i=j$}
                        \State Select a random action
                    \Else
                        \If{$i=j^3+1$}
                            \State Stay silent 
                            \State $j \gets \begin{cases} a & b \\ c & d \end{cases} $
                        \Else 
                            \State $i \gets \mathbf{J}^\intercal$
                            \State Break
                        \EndIf
                    \EndIf
                \EndFor
            \EndFor
        \end{algorithmic}
    \end{algorithm}

\end{document}

更新

根据您的代码,我给出了另一个示例,您可以看到规则与返回语句重叠。

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsbsy}

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

\newcommand*{\algrule}[1][\algorithmicindent]{\hspace*{.5em}\vrule\vrule
width 0pt height .75\baselineskip depth .25\baselineskip\hspace*{\dimexpr#1-.5em}}

\makeatletter
\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}
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother

\AtBeginEnvironment{algorithmic}{\lineskip0pt}

\newcommand*\Let[2]{\State #1 $\gets$ #2}


\begin{document}

\begin{algorithm}
  \caption{Test 2
    \label{alg:interact}}
  \begin{algorithmic}[0]
    \Require{$\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ and  $\mathcal{G}$}
    \Statex
    \Function{test}{$\mathcal{M}, \mathcal{G}$}
      \Let{$\mathcal{S}$}{$\emptyset$} 
      \For{$e_i \in \mathcal{E}$}
        \For{$g_i \in \mathcal{G}$}
          \Let{$\mathcal{I}$}{$e_i  \cap g_i$}
          \If{$\mathcal{I} \neq \emptyset $} 
            \Let{$\left\{n_k \right\}$}{$\mathcal{I}$} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{S}}$}{$\mathcal{S} \bigcup_k \left\{ n_k, s_k\right\} $}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{N}}$}{$\mathcal{N} \bigcup_k \left\{n_k \right\}$}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{T}}$}{subdivide($e_i, \mathcal{I}$)} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \mathcal{T}$}  
          \EndIf
        \EndFor
      \EndFor
      \State \Return{$\mathcal{W}, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

这使

在此处输入图片描述

更新2

新代码是

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsbsy}

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

\newcommand*{\algrule}[1][\algorithmicindent]{\hspace*{.5em}\vrule\vrule
width 0pt height \baselineskip depth .25\baselineskip\hspace*{\dimexpr#1-.5em}}

\makeatletter
\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% can't do <=, so add one to RHS and use < instead
    \repeat
    \fi
    \fi
}%
\usepackage{etoolbox}
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother

\AtBeginEnvironment{algorithmic}{\lineskip0pt}

\newcommand*\Let[2]{\State #1 $\gets$ #2}


\algtext*{EndWhile}% Remove "end while" text
\algtext*{EndFor}% Remove "end for" text
\algtext*{EndIf}% Remove "end if" text
\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%

% increase line spacing for algorithmicx
\usepackage{etoolbox}
\makeatletter
\expandafter\patchcmd\csname\string\algorithmic\endcsname{\itemsep\z@}
{\itemsep=0.2ex plus2pt}{}{}
\makeatother

\begin{document}

\showoutput

\begin{algorithm}
  \caption{Test 2
    \label{alg:interact}}
  \begin{algorithmic}[0]
    \Require{$\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ and  $\mathcal{G}$}
    \Statex
    \Function{test}{$\mathcal{M}, \mathcal{G}$}
      \Let{$\mathcal{S}$}{$\emptyset$} 
      \For{$e_i \in \mathcal{E}$}
        \For{$g_i \in \mathcal{G}$}
          \Let{$\mathcal{I}$}{$e_i  \cap g_i$}
          \If{$\mathcal{I} \neq \emptyset $} 
            \Let{$\left\{n_k \right\}$}{$\mathcal{I}$} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{S}}$}{$\mathcal{S} \bigcup_k \left\{ n_k, s_k\right\} $}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{N}}$}{$\mathcal{N} \bigcup_k \left\{n_k \right\}$}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{T}}$}{subdivide($e_i, \mathcal{I}$)} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \mathcal{T}$}  
          \EndIf
        \EndFor
      \EndFor
      \State \Return{$\mathcal{W}, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}


\end{document}

这使

在此处输入图片描述

答案1

tikz这是基于已发布代码的解决方案。完全不同的方法是使用algorithmicx 中的垂直线问题

要修改当前代码,您需要使用没有固定高度和深度的规则。实现此目的的一种方法是简单地编写\vrule。但是,您应该确保最小尺寸,并且需要确保生成的线不会被机制移开\lineskip。下面的代码涵盖了您的所有示例。

示例输出

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsbsy}

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

\usepackage{etoolbox}

\newcommand{\algruledefaultfactor}{.75}
\newcommand{\algstrut}[1][\algruledefaultfactor]{\vrule width 0pt
depth .25\baselineskip height #1\baselineskip\relax}
\newcommand*{\algrule}[1][\algorithmicindent]{\hspace*{.5em}\vrule\algstrut
\hspace*{\dimexpr#1-.5em}}

\makeatletter
\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% can't do <=, so add one to RHS and use < instead
    \repeat
    \fi
    \fi
}%

\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}

\AtBeginEnvironment{algorithmic}{\lineskip0pt}

\newcommand*\Let[2]{\State #1 $\gets$ #2}
\newcommand*\Stateh{\State \algstrut[1]}

\begin{document}

\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$}
      \State{$\mathbf{X} \gets \begin{bmatrix*}[r]  \boldsymbol x_i  & \boldsymbol x_j & \ldots \end{bmatrix*}^\intercal $} \Comment{get coordinates}
      \For{$j$ in $n$}
        \If{$i=j$}
          \State Select a random action
          \Else
          \If{$i=j^3+1$}
            \State Stay silent
            \State $j \gets \begin{cases} a & b \\ c & d \end{cases} $
            \Else
            \State $i \gets \mathbf{J}^\intercal$
            \State Break
          \EndIf
        \EndIf
        \EndFor
    \EndFor
  \end{algorithmic}
\end{algorithm}

\begin{algorithm}
  \caption{Test 2
    \label{alg:interact}}
  \begin{algorithmic}[0]
    \Require{$\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ and  $\mathcal{G}$}
    \Function{test}{$\mathcal{M}, \mathcal{G}$}
      \Let{$\mathcal{S}$}{$\emptyset$}
      \For{$e_i \in \mathcal{E}$}
        \For{$g_i \in \mathcal{G}$}
          \Let{$\mathcal{I}$}{$e_i  \cap g_i$}
          \If{$\mathcal{I} \neq \emptyset $}
            \Let{$\left\{n_k \right\}$}{$\mathcal{I}$}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{ \mathcal{S}}$}{$\mathcal{S} \bigcup_k \left\{ n_k, s_k\right\} $}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{ \mathcal{N}}$}{$\mathcal{N} \bigcup_k \left\{n_k \right\}$}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{\mathcal{T}}$}{subdivide($e_i, \mathcal{I}$)}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \mathcal{T}$}
            \EndIf
        \EndFor
      \EndFor
      \Stateh \Return{$\mathcal{W}, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

我重写了该\algrule命令,将其从框中解包,因此一个孤立的元素\vrule可以填充当前行的高度和深度。然后绘制零宽度规则以强制该高度和深度在标准行上合理。此规则封装在新命令中\algstrut。最后algorithmic修补环境以将其\lineskip设置为0pt

如果您有两条连续的高线,它们现在可能会接触,但您可以通过在这些一次性情况下向文本添加合适高度和深度的零宽度规则来解决这个问题。这可以使用\algstrut[1]或给定线上的一些大因子来完成。

事实证明,有必要\State在线路上拥有更高的支撑,因此我提供了一个\Stateh包含此功能的新命令。

顺便说一句,我删除了对齐的代码bmatrix,因为mathtools包现在提供了bmatrix*对齐选项。另外,我在你的示例中使用了\hphantom\phantom因为你只对调整水平间距感兴趣。

相关内容