我希望算法以章节为前缀,比如第 1 章应该是 1.1、1.2,第 2 章应该是 2.1、2.2
但就我的情况而言,所有章节只有 1、2、3、...,我该如何更改它?
包含的包如下:
\usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e}
这是最小的工作示例
\documentclass[11pt]{book}
\usepackage{bigstrut}
\usepackage{stmaryrd}
\usepackage{tipa}
\usepackage{amssymb}
\usepackage{textcomp}
\usepackage{times}
\usepackage{pifont}
\usepackage{amsfonts}
\usepackage{makeidx}
\usepackage{moreverb}
\usepackage{calc}
\usepackage{dsfont}
\usepackage{tikz}
\usepackage{color}
\usepackage{etex}
\usepackage{ctable}
\usepackage{bigstrut}
\usepackage{pifont}
\usepackage{amsfonts}
\usepackage{makeidx}
\usepackage{moreverb}
\usepackage{scalefnt}
\usetikzlibrary{shapes.geometric, positioning, fit, calc}
\usetikzlibrary{decorations.markings}
\usepackage{scalefnt}
\usepackage{pgf}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{verbatim}
\usetikzlibrary{
shapes.geometric,
positioning,
fit,
calc
}
\usepackage{amsmath}
\usetikzlibrary{arrows,automata,positioning,shapes,patterns}
\usepackage{etex}
\usepackage{amsmath, url, proof} % mathabx,
\usepackage{textcomp}
\usepackage{listings}
\usepackage{float}
\usepackage{multirow}
\usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usetikzlibrary{trees}
\usepackage{verbatim}
\usetikzlibrary{trees,decorations.pathmorphing}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\usepackage{lipsum}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\pgfsysdriver{pgfsys-dvipdfm.def}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepackage{epsfig}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}
\usepackage{epsf}
\usepackage{amsmath,amssymb,amsfonts} %
\usepackage{graphicx}
\usepackage{ltlfonts}
\usepackage{textcomp}
\usepackage{color}
\usepackage{chngcntr}
\usepackage{latexsym} % latex symbols
\usepackage{amsthm} % for defining theorems
\usepackage{syntonly} % verify syntax, not very important
\usepackage{lscape}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{caption}
\usepackage{subcaption}% http://ctan.org/pkg/subcaption
\captionsetup{compatibility=false}
\DeclareCaptionSubType*{algorithm}
\renewcommand\thesubalgorithm{\thetable\alph{subalgorithm}}
\DeclareCaptionLabelFormat{alglabel}{Alg.~#2}
\usepackage{pxfonts, pifont} % About fonts
\usepackage{setspace}
\usepackage{graphics}
\usepackage{multirow} % To write complicated tables
\usepackage{colortbl} % Colors
\usepackage{rotating} % For rotating tables
\usepackage{verbatim} % Comment Environment
% eg. \begin{comment}
% \end{comment}
\usepackage{mdwlist}
%%
\usepackage[titletoc]{appendix}
\usepackage{url}
\usepackage{style/mythesis}
\usepackage{lscape}
\usepackage{listings}
\newcommand{\myfontsize}{\fontsize{9}{10}\selectfont}
\usepackage[backref=section,colorlinks=true,hyperfootnotes=false]{hyperref}
\hypersetup{
colorlinks,%
citecolor=black,%
filecolor=black,%
linkcolor=black,%
urlcolor=black,breaklinks = true, %
pdfprintscaling = None, %
unicode = true, %
bookmarksnumbered = true,
}
\setcounter{secnumdepth}{3}
\interfootnotelinepenalty=10000
\begin{document}
\newpage
\pagenumbering{roman}
\singlespacing{ \listofalgorithms
\addcontentsline{toc}{section}{\numberline{}\hspace{-.35in}{\bf List
of Algorithms}}}
\doublespacing
\chapter{chapter1}~\label{a1}
\begin{algorithm}[H]
\centering
a
\caption{algo}\label{algo1}
\end{algorithm}
\chapter{chapter2}~\label{a2}
\begin{algorithm}[H]
\centering
a
\caption{algo}\label{algo2}
\end{algorithm}
\begin{algorithm}[H]
\centering
a
\caption{algo}\label{algo3}
\end{algorithm}
\end{document}
答案1
由于您同时使用了algorithm2e
和algorithm
包,并且它们都实现了一些相同的命令/环境,因此您必须使用选项algo2e
来algorithm2e
防止冲突,这将覆盖您设置为选项的计数器的设置algorith2e
,因为将使用的环境是来自的环境algorithm
。您有三种可能的解决方案:
如果不需要该
algorithm
包,则只加载algorithm2e
而不是algorithm
加载前者没有选项algo2e
:\documentclass[11pt]{book} \usepackage[ruled,vlined,linesnumbered,resetcount,algochapter]{algorithm2e} \begin{document} \chapter{chapter1} \begin{algorithm}[H] \centering test \caption{algo} \label{algo1} \end{algorithm} \end{document}
如果你真的您的文档中需要和
algorithm2e
,algorithm
并且您想要使用algorithm
环境(来自algorithm
包),在加载包之后,将以下几行添加到您的序言中:\makeatletter \renewcommand\thealgorithm{\thechapter.\arabic{algorithm}} \@addtoreset{algorithm}{chapter} \makeatother
完整示例:
\documentclass[11pt]{book} \usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e} \usepackage{algorithm} \makeatletter \renewcommand\thealgorithm{\thechapter.\arabic{algorithm}} \@addtoreset{algorithm}{chapter} \makeatother \begin{document} \chapter{chapter1} \begin{algorithm}[H] \centering test \caption{algo} \label{algo1} \end{algorithm} \end{document}
如果你真的您的文档中需要和
algorithm2e
,algorithm
并且您想要使用环境algorithm
(来自algorithm2e
包),请使用algorithm2e
环境(该algo2e
选项重命名algorithm
为algorithm2e
):\documentclass[11pt]{book} \usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e} \usepackage{algorithm} \begin{document} \chapter{chapter1} \begin{algorithm2e}[H] \centering test \caption{algo} \label{algo1} \end{algorithm2e} \end{document}
顺便说一句(与问题无关),您多次加载包;应该避免这种情况。
答案2
您可以使用\ counterwithin
最初由chngcntr
包定义的命令,但现在在基本乳胶中。我还使用它caption
在标题标签和标题文本之间添加分隔符:
\documentclass[11pt]{book}
\usepackage{algorithm}
\counterwithin{algorithm}{chapter}
\usepackage{caption}
\captionsetup[algorithm]{labelsep=colon, textfont=it}
\begin{document}
\chapter{chapter1}
\begin{algorithm}[H]
\centering
test
\caption{algo}
\label{algo1}
\end{algorithm}
This where we refer to Algorithm \ref{algo1}.
\end{document}