algorithm2e 中长行的缩进和垂直间距问题

algorithm2e 中长行的缩进和垂直间距问题

花了 2 小时尝试了各种方法后,我放弃了。我正在使用algorithm2e,当我有较长的描述行时,新行会稍微向右移动。此外,即使我在两个段落之间添加了新的空行,输出中也不会显示空行。

首先,这是问题的图表,下面是生成该问题的代码

在此处输入图片描述

代码是

\documentclass[12pt]{article}

\usepackage{algorithm2e}
\DontPrintSemicolon
\usepackage{algpseudocode}

\usepackage{amsmath}
\pagestyle{empty}
\setlength{\parindent}{0pt}

\begin{document}

\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\Input{$y'=f(x,y)$} 
Assume expansion around $x_0$ with initial conditions $y(x_0)=y_0$

\eIf{$f(x,y)$ analytic at $x_0$}
{ 
    Very long description here. very long description here. very long 
    description here. very long description here. Let\;

    \begin{align*}
    y &= y_0 + \sum_{n=0}^{\infty}  \frac{ x^{n+1}}{(n+1)!}  \, F_n(x,y)  \bigg\vert_{\substack{x=x_0\\y=y_0}}
    \end{align*}

     more very long description here. very long description here. 
     very long description here. very long description here.\;

     How to add paragraph spacing above this line?
}
{
   \eIf{ $f(x,y)$ not linear in $y(x)$}
    {
        \textbf{return}  \texttt{-{}-}     Not supported\;
    }
    {
    And so on
    }
}
   
\end{algorithm}

\end{document}

在 TL 2023 中使用 lualatex 进行编译。

我尝试\;用替换\\,但没有效果。我还尝试添加,\raggedright但没有效果。

我查看并尝试了许多其他与相关问题相关的方法,但无法弄清楚如何纠正这个问题。

我可以使用下面这种新的 MWE 来强制段落之间的垂直间距\vspace。这样可以修复段落间距,但长行上的初始偏移仍然存在

\documentclass[12pt]{article}

\usepackage{algorithm2e}
\DontPrintSemicolon
\usepackage{algpseudocode}

\usepackage{amsmath}
\pagestyle{empty}
\setlength{\parindent}{0pt}

\begin{document}

\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\Input{$y'=f(x,y)$} 
Assume expansion around $x_0$ with initial conditions $y(x_0)=y_0$

\eIf{$f(x,y)$ analytic at $x_0$}
{ 
    Very long description here. very long description here. very long 
    description here. very long description here. Let\;
    %
    \begin{align*}
    y &= y_0 + \sum_{n=0}^{\infty}  \frac{ x^{n+1}}{(n+1)!}  \, F_n(x,y)  \bigg\vert_{\substack{x=x_0\\y=y_0}}
    \end{align*}
     %
     more very long description here. very long description here. 
     very long description here. very long description here.\;
     
     \vspace{6pt}
     How to add paragraph spacing above this line?
}
{
   \eIf{ $f(x,y)$ not linear in $y(x)$}
    {
        \textbf{return}  \texttt{-{}-}     Not supported\;
    }
    {
    And so on
    }
}
   
\end{algorithm}

\end{document}

在此处输入图片描述

问题是:如何让所有换行的长行都从左对齐开始,而不移动。使用\vspace强制段落间留空是否是正确的解决方案?

答案1

算法的设置方式与常规段落相同。因此,如果你想在语句,你必须以某种方式强制执行。因此,\vspace为此使用某种形式的。下面我使用\medskip

您在换行的宽语句(段落)中看到的另一个水平错位是由悬挂缩进引起的。您可以通过添加\SetAlgoHangIndent{0pt}到序言中来消除此问题。

在此处输入图片描述

\documentclass[12pt]{article}

\usepackage{algorithm2e}
\DontPrintSemicolon
\SetAlgoHangIndent{0pt}

\usepackage{amsmath}

\begin{document}

\begin{algorithm}[H]
  \SetKwInOut{Input}{Input}
  \Input{$y' = f(x, y)$} 
  Assume expansion around $x_0$ with initial conditions $y(x_0) = y_0$

  \eIf{$f(x, y)$ analytic at $x_0$}{%
    Very long description here. very long description here. very long 
    description here. very long description here. Let
    \[
      y = y_0 + 
        \sum_{n = 0}^{\infty} \frac{x^{n + 1}}{(n + 1)!} 
        \,
        F_n(x,y) \biggr\rvert_{\substack{x = x_0 \\ y = y_0}}
    \]
    more very long description here. very long description here. 
    very long description here. very long description here.\;
    
    \medskip

    How to add paragraph spacing above this line?\;
  }{
    \eIf{$f(x, y)$ not linear in $y(x)$}{%
      \textbf{return} \texttt{-{}-} Not supported\;
    }{%
      And so on
    }
  }
\end{algorithm}

\end{document}

相关内容