0

I am learning how to use Algpseudocodex which is an enhanced version of algpseudocode

I am having hard time with getting correct spacing around \begin{align*}...\end{align*} when using inside the algorithm. Space above looks OK but space below is not. Also not all environments are aligned vertically correctly. Some start shifted too much to the right. A picture is worth 1,000 words:

enter image description here

I have feeling I am not using displayed math correctly.

How to fix the above two issues? Here is the 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}

Compiled using lualatex in TL 2023

1 Answer 1

1

Here's my proposal. However, you should be aware that's a no-no to use align for single equations.

I made a few changes for consistency and to get grammar right.

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

The algodisplay environment has an optional argument to fix bad vertical space issues like in the last display.

enter image description here

1
  • Thanks. Great solution.
    – Nasser
    Commented Oct 22, 2023 at 20:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .