使用 algorithm2e 时目录中的格式校正

使用 algorithm2e 时目录中的格式校正

设置

在我的 MWE 中,我尝试将“算法列表”添加到目录中。按照这里的一些示例,我能够添加一个页面,但是目录中的每个标题都变成了两倍,并且名称“算法列表”不是大写的。

平均能量损失

\documentclass[12pt, oneside]{book}

%For Algorithms

%ALGORITHM CODE
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{tocbibind} %for some reason "loa" will only work with this package, however this repeates the titles in the TOC


%TABLE OF CONTENTS CODE
\renewcommand\contentsname{CONTENTS}
\renewcommand\listtablename{LIST OF TABLES}
\renewcommand\listfigurename{LIST OF FIGURES}

%WHAT IM TRYING TO ADD
\newcommand{\listofalgorithmes}{\tocfile{\listalgorithmcfname}{loa}}
%\renewcommand\listofalgorithmes{LIST OF ALGORITHMS}

    \addtocontents{toc}{{\protect\noindent\bfseries Contents\hfill Page No.\bigskip\par}} %contents 
    \addtocontents{lot}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %tables
    \addtocontents{lof}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %figuers
    \addtocontents{loa}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %Alg

     \usepackage[titles]{tocloft}
     \usepackage{xpatch}

    \renewcommand\cftbeforechapskip{0ex}
    \renewcommand\cftchapfont{\mdseries}
    \renewcommand\cftchappagefont{\mdseries}
    \renewcommand\cftchappresnum{Chapter~}
    \renewcommand\cftchapaftersnum{: }
    \newlength\tocindent
    \settowidth\tocindent{\cftchapfont\cftchappresnum9\cftchapaftersnum}
    \edef\cftchapnumwidth{\the\tocindent}
    \edef\cftsecindent{\the\tocindent}
    \advance\tocindent2.3em
    \edef\cftsubsecindent{\the\tocindent}
    \advance\tocindent3.2em
    \edef\cftsubsubsecindent{\the\tocindent}
    \renewcommand\cftsecdotsep{\cftnodots}
    \renewcommand\cftsubsecdotsep{\cftnodots}
    \renewcommand\cftsubsubsecdotsep{\cftnodots}
    \newcommand\tocmainmatter
    {\renewcommand\cftchappagefont{\color{white}}%
    }
    \xapptocmd\mainmatter{\addtocontents{toc}{\protect\tocmainmatter}}{}{}

    % Centring for \chapter* and TOC headers, needed - use back up code below
    \newif\ifschaptertoc
    \makeatletter
    \renewcommand\@makeschapterhead[1]%
     {{\parindent \z@ \centering
     \normalfont
    \interlinepenalty\@M
    \Large \bfseries  #1\par\nobreak
    \vskip 20\p@
    }%
    \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
    \ifschaptertoc
    \addcontentsline{toc}{chapter}{#1}%
    \fi
     }
    \makeatother

    %dots
    \renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}} %parts
    \renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} %chapters
    \renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} %sections
    \renewcommand{\cftsubsecleader}{\cftdotfill{\cftdotsep}} %subsection



%----------------------------BEGIN---------------------------
\begin{document}
\frontmatter

\tableofcontents
\schaptertoctrue


\clearpage
\listofalgorithmes

\clearpage
\listoftables

\clearpage
\listoffigures

%-----------------------------To fill TOC-----------------

\mainmatter

\chapter{Chapter 1}
\section{section 1}

\begin{algorithm}[H]
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}

    \underline{Initialise} conditions\;
    \Input{input of algorithm}
    \Output{output of algorithm}
    \underline{Sequence In Full:} 

      {
       \hspace{5pt}  \textbf{Start}: Start \;
      }
      {
       \hspace{15pt}  Find:  \;
      }
      {
       \hspace{15pt}  Return: \vspace{-20pt}
      }
      {

      \hspace {5pt} \textbf{End}: End
      }

    \caption{Algorithm 1)} \label{alg:1}
\end{algorithm} \bigskip


\end{document}

问题

我该如何修正上面的代码,将“算法列表”变成“算法列表”,并防止目录重复“算法列表”、“表格列表”和“图形列表”?

答案1

不要使用tocbibind,不要使用\listofalgorithmes。要更改列表的名称,请重新定义\listalgorithmcfname。通过这些更改(我希望这些都是我所做的),结果应该是您想要的:

\documentclass[12pt, oneside]{book}

%For Algorithms

%ALGORITHM CODE
\usepackage[linesnumbered,ruled]{algorithm2e}
%\usepackage{tocbibind} %for some reason "loa" will only work with this package, however this repeates the titles in the TOC


%TABLE OF CONTENTS CODE
\renewcommand\contentsname{CONTENTS}
\renewcommand\listtablename{LIST OF TABLES}
\renewcommand\listfigurename{LIST OF FIGURES}
\renewcommand\listalgorithmcfname{LIST OF ALGORITHMS}

%WHAT IM TRYING TO ADD
%\newcommand{\listofalgorithmes}{\tocfile{\listalgorithmcfname}{loa}}
%\renewcommand\listofalgorithmes{LIST OF ALGORITHMS}

    \addtocontents{toc}{{\protect\noindent\bfseries Contents\hfill Page No.\bigskip\par}} %contents 
    \addtocontents{lot}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %tables
    \addtocontents{lof}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %figuers
    \addtocontents{loa}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %Alg

     \usepackage[titles]{tocloft}
     \usepackage{xpatch}

    \renewcommand\cftbeforechapskip{0ex}
    \renewcommand\cftchapfont{\mdseries}
    \renewcommand\cftchappagefont{\mdseries}
    \renewcommand\cftchappresnum{Chapter~}
    \renewcommand\cftchapaftersnum{: }
    \newlength\tocindent
    \settowidth\tocindent{\cftchapfont\cftchappresnum9\cftchapaftersnum}
    \edef\cftchapnumwidth{\the\tocindent}
    \edef\cftsecindent{\the\tocindent}
    \advance\tocindent2.3em
    \edef\cftsubsecindent{\the\tocindent}
    \advance\tocindent3.2em
    \edef\cftsubsubsecindent{\the\tocindent}
    \renewcommand\cftsecdotsep{\cftnodots}
    \renewcommand\cftsubsecdotsep{\cftnodots}
    \renewcommand\cftsubsubsecdotsep{\cftnodots}
    \newcommand\tocmainmatter
    {\renewcommand\cftchappagefont{\color{white}}%
    }
    \xapptocmd\mainmatter{\addtocontents{toc}{\protect\tocmainmatter}}{}{}

    % Centring for \chapter* and TOC headers, needed - use back up code below
    \newif\ifschaptertoc
    \makeatletter
    \renewcommand\@makeschapterhead[1]%
     {{\parindent \z@ \centering
     \normalfont
    \interlinepenalty\@M
    \Large \bfseries  #1\par\nobreak
    \vskip 20\p@
    }%
    \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
    \ifschaptertoc
    \addcontentsline{toc}{chapter}{#1}%
    \fi
     }
    \makeatother

    %dots
    \renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}} %parts
    \renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} %chapters
    \renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} %sections
    \renewcommand{\cftsubsecleader}{\cftdotfill{\cftdotsep}} %subsection



%----------------------------BEGIN---------------------------
\begin{document}
\frontmatter

\tableofcontents
\schaptertoctrue

\clearpage
\listofalgorithms

\clearpage
\listoftables

\clearpage
\listoffigures

%-----------------------------To fill TOC-----------------

\mainmatter

\chapter{Chapter 1}
\section{section 1}

\begin{algorithm}[H]
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}

    \underline{Initialise} conditions\;
    \Input{input of algorithm}
    \Output{output of algorithm}
    \underline{Sequence In Full:} 

      {
       \hspace{5pt}  \textbf{Start}: Start \;
      }
      {
       \hspace{15pt}  Find:  \;
      }
      {
       \hspace{15pt}  Return: \vspace{-20pt}
      }
      {

      \hspace {5pt} \textbf{End}: End
      }

    \caption{Algorithm 1)} \label{alg:1}
\end{algorithm} \bigskip


\end{document}

在此处输入图片描述

在此处输入图片描述

答案2

的重复出现LIST OF ALGORITHMS是由于已经在目录中添加了一个条目,并且在 的定义中\tocfile还有第二个条目。但是,测试可以转移到。\addcontentsline\@makechapterhead\ifschaptertoc\listofalgorithms

\usepackage[nottoc]{bibind]防止目录中的 重复CONTENTS,因为目录中已经有一个单独的用户定义 - 内容行,添加了\addtocontents

\documentclass[12pt, oneside]{book}

%For Algorithms

\newif\ifschaptertoc

\usepackage{tocloft}
\usepackage[nottoc]{tocbibind} %for some reason "loa" will only work with this package, however this repeates the titles in the TOC

%TABLE OF CONTENTS CODE

\renewcommand\contentsname{CONTENTS}
\renewcommand\listtablename{LIST OF TABLES}
\renewcommand\listfigurename{LIST OF FIGURES}



%ALGORITHM CODE

\usepackage[linesnumbered,ruled]{algorithm2e}


%WHAT IM TRYING TO ADD

\renewcommand{\listalgorithmcfname}{LIST OF ALGORITHMS}
\newcommand{\listofalgorithmes}{\ifschaptertoc\begingroup\tocfile{\listalgorithmcfname}{loa}\endgroup\fi}


\addtocontents{toc}{{\protect\noindent\bfseries Contents\hfill Page No.\bigskip\par}} %contents 
\addtocontents{lot}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %tables
\addtocontents{lof}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %figuers
\addtocontents{loa}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}} %Alg

\usepackage{xpatch}

\schaptertoctrue

\renewcommand\cftbeforechapskip{0ex}
\renewcommand\cftchapfont{\mdseries}
\renewcommand\cftchappagefont{\mdseries}
\renewcommand\cftchappresnum{Chapter~}
\renewcommand\cftchapaftersnum{: }
\newlength\tocindent
\settowidth\tocindent{\cftchapfont\cftchappresnum9\cftchapaftersnum}
\edef\cftchapnumwidth{\the\tocindent}
\edef\cftsecindent{\the\tocindent}
\advance\tocindent2.3em
\edef\cftsubsecindent{\the\tocindent}
\advance\tocindent3.2em
\edef\cftsubsubsecindent{\the\tocindent}
\renewcommand\cftsecdotsep{\cftnodots}
\renewcommand\cftsubsecdotsep{\cftnodots}
\renewcommand\cftsubsubsecdotsep{\cftnodots}
\newcommand\tocmainmatter
{\renewcommand\cftchappagefont{\color{white}}%
}
\xapptocmd\mainmatter{\addtocontents{toc}{\protect\tocmainmatter}}{}{}

% Centring for \chapter* and TOC headers, needed - use back up code below
\makeatletter
\renewcommand\@makeschapterhead[1]%
{{\parindent \z@ \centering
    \normalfont
    \interlinepenalty\@M
    \Large \bfseries  #1\par\nobreak
    \vskip 20\p@
  }%
  \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
%  \ifschaptertoc
%  \addcontentsline{toc}{chapter}{#1}%
%  \fi
}
\makeatother

% dots
\renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}} %parts
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} %chapters
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} %sections
\renewcommand{\cftsubsecleader}{\cftdotfill{\cftdotsep}} %subsection



% ----------------------------BEGIN---------------------------
\begin{document}
\frontmatter

\tableofcontents


\clearpage
\listofalgorithmes

\clearpage
\listoftables

\clearpage
\listoffigures

%-----------------------------To fill TOC-----------------

\mainmatter

\chapter{Chapter 1}
\section{section 1}

\begin{algorithm}[H]
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}

    \underline{Initialise} conditions\;
    \Input{input of algorithm}
    \Output{output of algorithm}
    \underline{Sequence In Full:} 

      {
       \hspace{5pt}  \textbf{Start}: Start \;
      }
      {
       \hspace{15pt}  Find:  \;
      }
      {
       \hspace{15pt}  Return: \vspace{-20pt}
      }
      {

      \hspace {5pt} \textbf{End}: End
      }

    \caption{Algorithm 1)} \label{alg:1}
\end{algorithm} \bigskip


\end{document}

在此处输入图片描述

相关内容