algorithm2e 垂直线缩进

algorithm2e 垂直线缩进

我真的很喜欢 algorithm2e 的垂直线功能,但是当我尝试缩进如果语句。我使用了以下代码。

\documentclass{article}
\usepackage{amssymb,amsthm,fullpage,amsmath}
\usepackage[ruled,vlined,boxed]{algorithm2e}

\begin{document}

\begin{algorithm}[H]
 \caption{\bf Generic Line Search Algorithm}

 {\bf Inputs:} Starting point $x_0$, initial step size parameter $\alpha_0$. \\
 \For{$k = 0,1,2,\dots$}{
    \nl {\bf Gradient approximation $g(x_k)$:}\\ \Indp
    Compute an approximation $g(x_k)$ of $\nabla \phi(x_k)$. \\ \Indm
    \nl {\bf Construct a search direction $d_k$:}\\ \Indp
    Construct a search direction $d_k$, e.g., $d_k = - g(x_k)$.\\ \Indm
    \nl {\bf Compute step size $\alpha_k$ and update the iterate:} \\ \Indp
    \eIf{sufficient decrease condition is satisfied}{
        $x_{k+1} = x_k - \alpha_k d_k$ and $\alpha_{k+1} \gets \tau^{-1} \alpha_k$
    }{
        $x_{k+1} = x_k$ and $\alpha_{k+1} \gets \tau \alpha_k$
    } 
 }
\end{algorithm}

\end{document}

文本已缩进,但如果声明并未如图片所示。 algorithm2e 中的垂直线缩进问题 有人知道如何缩进垂直线吗?

答案1

尝试这个:

  • \skiptext(vline space 之前)和\skiprule(vline space 之后)在缩进调整命令中另外更新。
  • 算法输入没有变化。
\documentclass{article}
%\usepackage{amssymb,amsthm,fullpage,amsmath}
\usepackage[ruled,vlined,boxed]{algorithm2e}

\makeatletter
\renewcommand{\Indentp}[1]{%
  \advance\leftskip by #1
  \advance\skiptext by -#1
  \advance\skiprule by #1}%
\renewcommand{\Indp}{\algocf@adjustskipindent\Indentp{\algoskipindent}}
\renewcommand{\Indpp}{\Indentp{0.5em}}%
\renewcommand{\Indm}{\algocf@adjustskipindent\Indentp{-\algoskipindent}}
\renewcommand{\Indmm}{\Indentp{-0.5em}}%
\makeatother

\begin{document}
\begin{algorithm}[H]
 \caption{\bfseries OP's example}

 \textbf{Inputs:} Starting point $x_0$, initial step size parameter $\alpha_0$. \\
 \For{$k = 0,1,2,\dots$}{
    \nl \textbf{Gradient approximation $g(x_k)$:}\\ \Indp
    Compute an approximation $g(x_k)$ of $\nabla \phi(x_k)$. \\ \Indm
    \nl \textbf{Construct a search direction $d_k$:}\\ \Indp
    Construct a search direction $d_k$, e.g., $d_k = - g(x_k)$.\\ \Indm
    \nl \textbf{Compute step size $\alpha_k$ and update the iterate:} \\ \Indp
    \eIf{sufficient decrease condition is satisfied}{
        $x_{k+1} = x_k - \alpha_k d_k$ and $\alpha_{k+1} \gets \tau^{-1} \alpha_k$
    }{
        $x_{k+1} = x_k$ and $\alpha_{k+1} \gets \tau \alpha_k$
    }
 }
\end{algorithm}


\begin{algorithm}
  \caption{More tests}
  \nl test \\
  \eIf{sufficient decrease condition is satisfied}{
    if clause
  }{
    else clause
  }
  \nl test \\ \Indp\Indp
  \eIf{sufficient decrease condition is satisfied}{
    if clause
  }{
    else clause
  } \Indm
  \nl test \\
  \eIf{sufficient decrease condition is satisfied}{
    if clause
  }{
    else clause
  }
\end{algorithm}
\end{document}

在此处输入图片描述 在此处输入图片描述

笔记:

  • \Indp(或\Indm)增加(或减少)缩进\algoskipindent(又称“步骤”)。
  • 这可能不是完美的解决方案,因为我还没有完全理解其内部algorithm2e原理。\algocf@Vline\algocf@Vsline

答案2

正如评论中所说,使用垂直线和\indp\indm有点问题。另一种方法是将计算步长变成一个块,从而允许algorithm2e包以更好的方式自行处理缩进。

这是使用\SetKwBlock{Begin}{begin}{end}宏完成的:

\SetKwBlock{NewBlock}{Compute step size $\alpha_k$ and update the iterate:}{}

这样可以很好地缩进 if ,但是,它也会为新的 计算步长块。要删除该垂直线,我们添加\SetAlgoNoLine\SetAlgoVlined

梅威瑟:

\documentclass{article}
\usepackage{amssymb,amsthm,fullpage,amsmath}
\usepackage[ruled,vlined,boxed]{algorithm2e}


\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
\SetKwBlock{NewBlock}{Compute step size $\alpha_k$ and update the iterate:}{}
\caption{\bf Generic Line Search Algorithm}

\KwIn{Starting point $x_0$, initial step size parameter $\alpha_0$.}\;
\For{$k = 0,1,2,\dots$}{
   \nl {\bf Gradient approximation $g(x_k)$:}\; 
   \Indp Compute an approximation $g(x_k)$ of $\nabla \phi(x_k)$.\;

   \Indm \nl {\bf Construct a search direction $d_k$:}\; 
   \Indp Construct a search direction $d_k$, e.g., $d_k = - g(x_k)$.\; 


   \Indm\nl\SetAlgoNoLine\NewBlock{ \SetAlgoVlined\eIf{sufficient decrease condition is satisfied}{
        $x_{k+1} = x_k - \alpha_k d_k$ and $\alpha_{k+1} \gets \tau^{-1} \alpha_k$\;
        }{
        $x_{k+1} = x_k$ and $\alpha_{k+1} \gets \tau \alpha_k$\;
        }
    }
 }
\end{algorithm}

\end{document}

结果如下:

在此处输入图片描述

请注意OP的MWE的以下改进:

  1. 每行末尾\;使用 (不需要\\)。为了不打印分号,我们使用DontPrintSemicolon
  2. 我们使用algorithm2e内置\KwIn宏。

相关内容