我所在大学对论文格式有一些奇怪的要求。我几乎可以模仿所有这些要求,但algorithm
包中的算法列表是我无法浏览的 - 我甚至不知道从哪里开始查找。基本上,我想要实现的是让 LoA 和 LoF 具有相同的格式。
最小工作示例如下:
\documentclass[10pt,a4paper,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[left=3.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm]{geometry}
\usepackage{tocloft}
\setlength{\cftafterloftitleskip}{6pt}
\renewcommand{\cftloftitlefont}{\clearpage \large\bfseries\MakeUppercase}
\renewcommand{\cftdotsep}{0.0}
\setlength{\cftparskip}{6pt}
\setlength{\cftbeforechapskip}{0pt}
\renewcommand{\cftchapfont}{}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdot}}
\renewcommand{\cftchappagefont}{}
\renewcommand{\cftchappresnum}{\chaptertitlename}
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftsecaftersnum}{.}
\renewcommand{\cftsubsecaftersnum}{.}
\renewcommand{\cftfigaftersnum}{.}
\setlength{\cftfigindent}{0pt}
\setlength{\cfttabindent}{0pt}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{tikz}
\begin{document}
\chapter{Chapter}
\section{Section}
\begin{algorithm}
\begin{algorithmic}[1]
\State $a \gets 0$
\end{algorithmic}
\caption{Alg}\label{alg:algorithm}
\end{algorithm}
\begin{figure}[h]
\begin{tikzpicture}
\coordinate [label=left:$m$,circle,fill,inner sep=1pt] (m) at (-12em,-4.em);
\coordinate [label=below:$O$,circle,fill,inner sep=1pt] (O) at (2em,-10em);
\draw[blue, ->, thick] (m) -- node[above] {} (O);
\end{tikzpicture}
\caption{Fig}
\label{fig:figure}
\end{figure}
\listoffigures
\addcontentsline{toc}{chapter}{List of Figures}
\listofalgorithms
\addcontentsline{toc}{chapter}{List of Algorithms}
\end{document}
答案1
下面的代码段复制了所涉及的宏的完整副本\listoffigures
,并将它们应用于重新定义的\listofalgorithms
:
\usepackage{etoolbox}
\makeatletter
\AtBeginDocument{%
\let\l@algorithm\l@figure%
\let\listofalgorithms\listoffigures% Copy \listoffigures
\let\@cftmakeloatitle\@cftmakeloftitle% Copy LoF title
% Update LoA-related macros
\patchcmd{\listofalgorithms}{\@cftmakeloftitle}{\@cftmakeloatitle}{}{}%
\patchcmd{\listofalgorithms}{\@starttoc{lof}}{\@starttoc{loa}}{}{}%
\patchcmd{\@cftmakeloatitle}{\listfigurename}{\listalgorithmname}{}{}%
% Add per-chapter LoA space (similar to LoF)
\patchcmd{\@chapter}{\addtocontents}{%
\addtocontents{loa}{\protect\addvspace{10\p@}}%
\addtocontents}{}{}%
}
\makeatother
这是您的实际示例:
\documentclass{report}
\usepackage{tocloft}
\setlength{\cftafterloftitleskip}{6pt}
\renewcommand{\cftloftitlefont}{\clearpage \large\bfseries\MakeUppercase}
\renewcommand{\cftdotsep}{0.0}
\setlength{\cftparskip}{6pt}
\setlength{\cftbeforechapskip}{0pt}
\renewcommand{\cftchapfont}{}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdot}}
\renewcommand{\cftchappagefont}{}
\renewcommand{\cftchappresnum}{\chaptertitlename}
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftsecaftersnum}{.}
\renewcommand{\cftsubsecaftersnum}{.}
\renewcommand{\cftfigaftersnum}{.}
\setlength{\cftfigindent}{0pt}
\setlength{\cfttabindent}{0pt}
\usepackage{algorithm,etoolbox}
\makeatletter
\AtBeginDocument{%
\let\l@algorithm\l@figure%
\let\listofalgorithms\listoffigures%
\let\@cftmakeloatitle\@cftmakeloftitle%
\patchcmd{\listofalgorithms}{\@cftmakeloftitle}{\@cftmakeloatitle}{}{}%
\patchcmd{\listofalgorithms}{\@starttoc{lof}}{\@starttoc{loa}}{}{}%
\patchcmd{\@cftmakeloatitle}{\listfigurename}{\listalgorithmname}{}{}%
\patchcmd{\@chapter}{\addtocontents}{%
\addtocontents{loa}{\protect\addvspace{10\p@}}%
\addtocontents}{}{}%
}
\makeatother
\begin{document}
\listoffigures
\listofalgorithms
\chapter{Chapter}
\begin{algorithm}
\caption{Alg}
\end{algorithm}
\begin{figure}[h]
\caption{Fig}
\end{figure}
\end{document}
答案2
毫无疑问很多很多不这样做的理由...
买者自负...
这个想法本质上是将算法列表写入辅助文件两次。
我们让algorithm
包自己做事:它有自己的计数器、自己的内容行、自己的格式。一切。我们只是从不打印它生成的算法列表。
相反,我们algorithm
使用修改环境etoolbox
,使其增加二计数器,写入二辅助文件等的大量信息:
\AtBeginEnvironment{algorithm}{%
\let\oldcaption\caption
\renewcommand\caption[1]{%
\oldcaption{#1}%
\stepcounter{alg}%
\addcontentsline{loalg}{alg}{\protect\numberline{\thealg}{ #1}}}}
这将保存 的旧定义\caption
,然后重新定义它以增加tocloft
的计数器并将内容行写入 的tocloft
辅助文件。这些以通常的方式创建:
\newlistof[chapter]{alg}{loalg}{\listalgorithmname}
因此,我们可以对其应用与图形列表相同的定制:
\setlength{\cftafterloalgtitleskip}{6pt}
\renewcommand{\cftloalgtitlefont}{\clearpage \large\bfseries\MakeUppercase}
...
\renewcommand{\cftalgaftersnum}{.}
...
\setlength{\cftalgindent}{0pt}
当然,这不是一个非常有效的解决方案。首先,它浪费了一个计数器。但是,最低限度的测试表明它可以发挥作用:
完整代码:
\documentclass[10pt,a4paper,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[left=3.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm]{geometry}
\usepackage{tocloft,etoolbox}
\newlistof[chapter]{alg}{loalg}{\listalgorithmname}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{tikz}
\setlength{\cftafterloftitleskip}{6pt}
\renewcommand{\cftloftitlefont}{\clearpage \large\bfseries\MakeUppercase}
\setlength{\cftafterloalgtitleskip}{6pt}
\renewcommand{\cftloalgtitlefont}{\clearpage \large\bfseries\MakeUppercase}
\renewcommand{\cftdotsep}{0.0}
\setlength{\cftparskip}{6pt}
\setlength{\cftbeforechapskip}{0pt}
\renewcommand{\cftchapfont}{}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdot}}
\renewcommand{\cftchappagefont}{}
\renewcommand{\cftchappresnum}{\chaptertitlename}
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftsecaftersnum}{.}
\renewcommand{\cftsubsecaftersnum}{.}
\renewcommand{\cftfigaftersnum}{.}
\renewcommand{\cftalgaftersnum}{.}
\setlength{\cftfigindent}{0pt}
\setlength{\cfttabindent}{0pt}
\setlength{\cftalgindent}{0pt}
\AtBeginEnvironment{algorithm}{%
\let\oldcaption\caption
\renewcommand\caption[1]{%
\oldcaption{#1}%
\stepcounter{alg}%
\addcontentsline{loalg}{alg}{\protect\numberline{\thealg}{ #1}}}}
\begin{document}
\chapter{Chapter}
\section{Section}
\begin{algorithm}
\begin{algorithmic}[1]
\State $a \gets 0$
\end{algorithmic}
\caption{Alg}\label{alg:algorithm}
\end{algorithm}
\begin{figure}[h]
\begin{tikzpicture}
\coordinate [label=left:$m$,circle,fill,inner sep=1pt] (m) at (-12em,-4.em);
\coordinate [label=below:$O$,circle,fill,inner sep=1pt] (O) at (2em,-10em);
\draw[blue, ->, thick] (m) -- node[above] {} (O);
\end{tikzpicture}
\caption{Fig}
\label{fig:figure}
\end{figure}
\listoffigures
\addcontentsline{toc}{chapter}{\listfigurename}
\listofalg
\addcontentsline{toc}{chapter}{\listalgorithmname}
\end{document}