演算法和演算法2e的目錄一起

演算法和演算法2e的目錄一起

演算法的內容認為使用的演算法algorithm是一種演算法,而使用的演算法algorithm2e是另一種演算法,不是與其他套件一起導入的第二個演算法,而是不同的實體或其他東西,在我看來這是不行的。

我想要這個:

  1 algo 1..................... page of algo 1
1 algo 2..................... page of algo 2

改為這樣:

1 algo 1..................... page of algo 1
2 algo 2..................... page of algo 2

上面的對齊方式和編號已更改。

這是基於的最小範例問題:

\documentclass{article}
\usepackage{algorithm} 
\usepackage{algorithmic}  
\usepackage[algo2e]{algorithm2e} 
\renewcommand{\listalgorithmname}{List of Algorithms}
\addtocontents{loa}{\protect\thispagestyle{empty}}

\begin{document}
    \listofalgorithms
    \smallskip
    \begin{algorithm}%>- from algorithm package
        test
        \caption{algo 1}
    \end{algorithm}

    \begin{algorithm2e}%>- from algorithm2e package
        test
        \caption{algo 2}
    \end{algorithm2e}
\end{document}

輸出: 在此輸入影像描述


編年史

我已經準備好論文了。然而,就在昨晚我們提交了一篇基於它的論文。我也必須提交我的論文,我想將論文中寫的一些演算法納入論文中。由於多種原因,我遇到了這樣一種情況:有人會為我的問題提供解決方案,或者我會直接離開論文而沒有論文演算法,這當然會導致一篇令人心碎的論文。 :)

論文的演算法用 寫algorithm2e,論文的演算法用 和algorithmalgorithmic。我必須明天早上第一件事提交,因此我沒有時間重寫它們(我現在正在其他部分編輯我的論文),這就是為什麼我要么在這裡聽到社區的解決方案,要么離開算法我的論文之外的論文!

答案1

\setcounter一個快速的解決方案是在每個演算法內部使用。此外,為了使演算法在演算法清單中對齊,您需要重新定義\listof\l@algocf命令並設定\@dottedtocline所需的水平間距。這是代碼:

\documentclass{article}
\usepackage{algorithm} 
\usepackage{algorithmic}  
\usepackage[algo2e]{algorithm2e} 
\renewcommand{\listalgorithmname}{List of Algorithms}
\addtocontents{loa}{\protect\thispagestyle{empty}}

% add this to align the list of algorithms
\makeatletter
  \renewcommand*{\listof}[2]{%
    \@ifundefined{ext@#1}{\float@error{#1}}{%
      \@namedef{l@#1}{\@dottedtocline{1}{1em}{2.3em}}% line of the list (change from 1em to the desired value)
      \float@listhead{#2}%
      \begingroup\setlength{\parskip}{\z@}%
        \@starttoc{\@nameuse{ext@#1}}%
      \endgroup}}
  \renewcommand*\l@algocf{\@dottedtocline{1}{1em}{2.3em}}% line of the list (change from 1em to the desired value)
\makeatother

\begin{document}
    \listofalgorithms
    \smallskip
    \begin{algorithm}%>- from algorithm package
      \setcounter{algorithm}{0}% set the counter for the 1st algorithm
        test
        \caption{algo 1}
    \end{algorithm}

    \begin{algorithm2e}%>- from algorithm2e package
      \setcounter{algocf}{1}% set the counter for the 2nd algorithm
        test
        \caption{algo 2}
    \end{algorithm2e}

    \begin{algorithm}%>- from algorithm package
      \setcounter{algorithm}{2}% set the counter for the 3rd algorithm
        test
        \caption{algo 3}
    \end{algorithm}

    \begin{algorithm2e}%>- from algorithm2e package
      \setcounter{algocf}{3}% set the counter for the 4th algorithm
        test
        \caption{algo 4}
    \end{algorithm2e}
\end{document}

相關內容