使用mdframed和algorithm2e的算法环境

使用mdframed和algorithm2e的算法环境

我想将所有算法放入其中mdframed,但我无法完全实现我想要的效果。我已将算法环境更改为纯文本,但我希望标题/标题左对齐并进入 mdframed 的标题框,就像定理一样。我该怎么做?

这是 MWE(如果格式不正确请见谅,我是新手:)):

\documentclass{article}
    % General document formatting
    \usepackage[margin=0.7in]{geometry}
    \usepackage[parfill]{parskip}
    \usepackage[utf8]{inputenc}
    
    % Related to math
    \usepackage{amsmath,amssymb,amsfonts,amsthm}
    \usepackage[dvipsnames]{xcolor}
    \usepackage[plain]{algorithm2e}
    \usepackage[framemethod=TikZ]{mdframed}
    \usepackage{algorithmic}
    
    \mdfsetup{skipabove=\topskip,skipbelow=0pt}%-.5\topskip}
    \mdfdefinestyle{mdfalg}{
        skipabove=\topskip,
        skipbelow=\topskip,
        rightmargin=0pt,
        leftmargin=0pt,
        innertopmargin=7pt,
        innerbottommargin=3pt,
        innerrightmargin=5pt,
        innerleftmargin=5pt,
        topline=false,
        bottomline=false,
        rightline=false,
        leftline=false,
        frametitlerule=true,
        backgroundcolor=Goldenrod!45!white,
        frametitlebackgroundcolor=Goldenrod!45!white,
    }

    % Redefine algorithm environment
    \makeatletter
    \def\@algocf@pre@plain{
      \begin{mdframed}[
        style=mdfalg,
        nobreak=true]
    }%
    \def\@algocf@post@plain{\end{mdframed}\relax}%
    \makeatother
    \makeatletter
    \renewcommand{\@algocf@capt@plain}{above}% formerly {bottom}
    \makeatother

\begin{document}

test the algorthm:
\begin{algorithm}
\caption{My amazing algorithm}
\label{alg:amazing_algorithm}
\begin{algorithmic}
  \IF{some condition}
    \RETURN do something
  \ELSE
    \FORALL{children of the sun}
      \STATE Call this function recursively
    \ENDFOR
  \ENDIF
\end{algorithmic}
\end{algorithm}

\end{document}

相关内容