使用基于 algpseudocode 的包正确使用算法中的显示数学。对齐和间距问题

使用基于 algpseudocode 的包正确使用算法中的显示数学。对齐和间距问题

我正在学习如何使用阿尔卑斯伪抄本这是algpseudocode

在算法内部使用时,我很难获得正确的间距\begin{align*}...\end{align*}。上方的空间看起来不错,但下方的空间却不行。此外,并非所有环境都垂直对齐正确。有些环境开始向右移动太多。一张图片胜过千言万语:

在此处输入图片描述

我感觉我没有正确使用显示的数学。

如何解决上述两个问题?以下是 MWE

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage[noEnd=false,indLines=true]{algpseudocodex}

%use plain to remove top and botton horizontal lines
\usepackage[plain]{algorithm}

\begin{document}
\begin{algorithm}
\begin{algorithmic}[0]
\Function{clairaut\_dalmbert\_solver}{$F(x,y,p)$}
\State Solve for $y$ and write the ode as  (where $p=y'$)
    \begin {align*}  
           y &= x f(p) +g(p) \tag{1}
    \end {align*}
\If{$f(p)=p$} \Comment{Example $ y = x p +g(p) $}
    \If{$g(p)=0$}  \Comment{Example  $y=x p$}
       \State \Return as this this is not clairaut nor dAlmbert.
    \ElsIf{$g(p)$ is linear in $p$} \Comment{Example   $y=x p+p$}
       \State \Return as this this is not clairaut nor dAlmbert.
    \Else  \Comment{Example  $y=x p+p^2$ or $y=x p+\sin(p)$}
       \State This is Clairaut ode. Taking derivative of (1) w.r.t $x$ gives
           \begin {align*}  
                 p & =\frac {d}{dx}\left ( xp+g\right ) \\ 
                 p & =\left ( p+x\frac {dp}{dx}\right ) +\left ( g^{\prime }\frac {dp}{dx}\right ) \\ 
                 p & =p+\left ( x+g^{\prime }\right ) \frac {dp}{dx}\\ 
                 0 & =\left ( x+g^{\prime }\right ) \frac {dp}{dx} 
          \end {align*}
       \State where $g'$ is derivative of $g(p)$ w.r.t $p$. The general solution is 
          \begin {align*}  
                \frac {dp}{dx} & =0\\ 
                p              & =c_{1} 
          \end{align*}
       \State where $c_1$ is constant.
    \EndIf
\Else 
    \State\textbf{CALL} dAlembert\_solver($F(x,y,p)$)
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}

在 TL 2023 中使用 lualatex 编译

答案1

align这是我的建议。但是,您应该知道,对于单个方程式来说,这是不可以的。

我做了一些改动以保持一致性并使语法正确。

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage[noEnd=false,indLines=true]{algpseudocodex}

%use plain to remove top and botton horizontal lines
\usepackage[plain]{algorithm}

\NewDocumentEnvironment{algodisplay}{mob}{%
  \unskip\hspace*{\fill}\par\begin{minipage}{\linewidth}
  \setlength{\abovedisplayskip}{0.5\abovedisplayskip}
  \IfValueT{#2}{\vspace{#2}}
  \begin{#1*}#3\end{#1*}\par
  \vspace{-\baselineskip}
  \vspace{\belowdisplayskip}
  \end{minipage}}{}

\begin{document}

\begin{algorithm}
\begin{algorithmic}[0]
\Function{clairaut\_dalembert\_solver}{$F(x,y,p)$}
  \State Solve for $y$ and write the ode as  (where $p=y'$)
    \begin{algodisplay}{equation}  
      y = x f(p) +g(p) \tag{1}
    \end{algodisplay}
  \If{$f(p)=p$} \Comment{Example $ y = x p +g(p) $}
    \If{$g(p)=0$}  \Comment{Example  $y=x p$}
      \State \Return as this is neither Clairaut nor d'Alembert.
    \ElsIf{$g(p)$ is linear in $p$} \Comment{Example   $y=x p+p$}
      \State \Return as this is neither Clairaut nor d'Alembert.
    \Else  \Comment{Example  $y=x p+p^2$ or $y=x p+\sin(p)$}
      \State This is a Clairaut ode. Taking the derivative of (1) w.r.t.\ $x$ gives
        \begin{algodisplay}{align}
          p & =\frac {d}{dx}\left ( xp+g\right ) \\ 
          p & =\left ( p+x\frac {dp}{dx}\right ) +\left ( g^{\prime }\frac {dp}{dx}\right ) \\ 
          p & =p+\left ( x+g^{\prime }\right ) \frac {dp}{dx}\\ 
          0 & =\left ( x+g^{\prime }\right ) \frac {dp}{dx} 
        \end{algodisplay}
      \State where $g'$ is the derivative of $g(p)$ w.r.t.\ $p$. The general solution is \\
        \begin{algodisplay}{equation}[-\baselineskip]
          \frac {dp}{dx} = 0 \qquad p = c_{1} 
        \end{algodisplay}
      \State where $c_1$ is constant.
    \EndIf
  \Else 
    \State\textbf{CALL} $\Call{dalembert\_solver}{F(x,y,p)}$
  \EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}

\end{document}

环境algodisplay有一个可选参数来修复上次显示中的垂直空间问题。

在此处输入图片描述

相关内容