为什么添加 hyperref 会导致 algorithm2e 不再跨页面?

为什么添加 hyperref 会导致 algorithm2e 不再跨页面?

TL 2020. 使用编译lualatex

我已经设置了表格

 \begin{algorithm}[H]
    ....
 \end{algorithm}

 \begin{algorithm}[H]
    ....
 \end{algorithm}

 \begin{algorithm}[H]
    ....
 \end{algorithm}

如果没有它,编译时也\usepackage{hyperref}可以正常工作,并且所有算法都不会尝试适应单个页面。

添加时\usepackage{hyperref},所有算法都尝试放入单个页面,但它们被截断了。尝试更改和的顺序hyperrefalgorithmic但没有任何变化。

我做错什么了吗?

下面是 MWE。首先,这是屏幕截图

截屏

在此处输入图片描述

平均能量损失

\documentclass{article}

\usepackage{amsmath,mleftright}
\usepackage{mathtools,amssymb}
\usepackage{float}
%\usepackage{hyperref}  %causes problem
\usepackage{algorithmic}
\usepackage[ruled]{algorithm2e}


\begin{document}
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{1st order ODE}
    \KwOut{solution}

    let ode be $g(y'(x))=f(x,y)$

    \eIf{ode is linear in $y'(x)$}
    {
       solve for $y'(x)$ and write ode  as $y'(x)=f(x,y)$\;
       \eIf{ $f(x,y)$ is linear in $y(x)$}
       {
           write $f(x,y)$ in the form $f(x,y)=f_0(x)+f_1(x) y(x)$\;
           solve $y'=f_0(x)+f_1(x)y$ using integrating factor
       }
       {
           \uIf{ A}
           {
              B
           }
           \uElseIf{ A }
           {
             B
           }
           \uElseIf{A }
          {
             B
          }           
          \ElseIf{ A }
          {
             B
          }
       }
    }
    {
      B
    }
    \Return solution

    \caption{A}

\end{algorithm}

%%%%%%%%%%%%%%%%%%%%%
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{A}
    \KwOut{boolean}

    \eIf{A
         where B} 
    {
       \eIf{ A }
       {
            \Return true
       }      
       {
           \eIf{ B }
           {
                \Return true
           }
           {
                \Return false
           }
       }             
    }
    {
       \Return false
    }

    \caption{B}

\end{algorithm}


%%%%%%%%%%%%%%%%%%%%%
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{A}
    \KwOut{boolean}

    \eIf{A
         where B} 
    {
       \eIf{ A }
       {
            \Return true
       }      
       {
           \eIf{ B }
           {
                \Return true
           }
           {
                \Return false
           }
       }             
    }
    {
       \Return false
    }

    \caption{B}

\end{algorithm}



%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{A}
    \KwOut{boolean}

    \eIf{A
         where B} 
    {
       \eIf{ A }
       {
            \Return true
       }      
       {
           \eIf{ B }
           {
                \Return true
           }
           {
                \Return false
           }
       }             
    }
    {
       \Return false
    }

    \caption{B}

\end{algorithm}

\end{document}

答案1

我认为是这样的

\documentclass{article}

\usepackage{amsmath,mleftright}
\usepackage{mathtools,amssymb}
\usepackage{float}
\usepackage{hyperref}  %causes problem
\usepackage{algorithmic}
\usepackage[ruled]{algorithm2e}

\makeatletter
\def\Hy@SaveLastskip{%
  \let\Hy@RestoreLastskip\relax
  \ifvmode
    \ifdim\lastskip=\z@
      \ifnum\lastnodetype=11 %not sure this is needed really
        \let\Hy@RestoreLastskip\nobreak
      \else
        \let\Hy@RestoreLastskip\relax
      \fi
    \else
      \begingroup
        \skip@=-\lastskip
        \edef\x{%
          \endgroup
          \def\noexpand\Hy@RestoreLastskip{%
            \noexpand\ifvmode
              \noexpand\nobreak
              \vskip\the\skip@
              \vskip\the\lastskip\relax
            \noexpand\fi
          }%
        }%
      \x
    \fi
  \else
    \ifhmode
      \ifdim\lastskip=\z@
        \let\Hy@RestoreLastskip\nobreak
      \else
        \begingroup
          \skip@=-\lastskip
          \edef\x{%
            \endgroup
            \def\noexpand\Hy@RestoreLastskip{%
              \noexpand\ifhmode
                \noexpand\nobreak
                \hskip\the\skip@
                \hskip\the\lastskip\relax
              \noexpand\fi
            }%
          }%
        \x
      \fi
    \fi
  \fi
}%
\makeatother

\begin{document}
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{1st order ODE}
    \KwOut{solution}

    let ode be $g(y'(x))=f(x,y)$

    \eIf{ode is linear in $y'(x)$}
    {
       solve for $y'(x)$ and write ode  as $y'(x)=f(x,y)$\;
       \eIf{ $f(x,y)$ is linear in $y(x)$}
       {
           write $f(x,y)$ in the form $f(x,y)=f_0(x)+f_1(x) y(x)$\;
           solve $y'=f_0(x)+f_1(x)y$ using integrating factor
       }
       {
           \uIf{ A}
           {
              B
           }
           \uElseIf{ A }
           {
             B
           }
           \uElseIf{A }
          {
             B
          }           
          \ElseIf{ A }
          {
             B
          }
       }
    }
    {
      B
    }
    \Return solution

    \caption{A}

\end{algorithm}

%%%%%%%%%%%%%%%%%%%%%
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{A}
    \KwOut{boolean}

    \eIf{A
         where B} 
    {
       \eIf{ A }
       {
            \Return true
       }      
       {
           \eIf{ B }
           {
                \Return true
           }
           {
                \Return false
           }
       }             
    }
    {
       \Return false
    }

    \caption{B}

\end{algorithm}


%%%%%%%%%%%%%%%%%%%%%
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{A}
    \KwOut{boolean}

    \eIf{A
         where B} 
    {
       \eIf{ A }
       {
            \Return true
       }      
       {
           \eIf{ B }
           {
                \Return true
           }
           {
                \Return false
           }
       }             
    }
    {
       \Return false
    }

    \caption{B}

\end{algorithm}



%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%
\begin{algorithm}[H]
    \DontPrintSemicolon 
    %\SetAlgoNoLine

    \KwIn{A}
    \KwOut{boolean}

    \eIf{A
         where B} 
    {
       \eIf{ A }
       {
            \Return true
       }      
       {
           \eIf{ B }
           {
                \Return true
           }
           {
                \Return false
           }
       }             
    }
    {
       \Return false
    }

    \caption{B}

\end{algorithm}

\end{document}

相关内容