这是一个后续问题按章节分组待办事项列表.egreg 的解决方案与classicthesis
和配合使用效果很好scrreprt
,我也一直在尝试在没有classicthesis
和 的情况下做同样的scrartcl
事情,即按部分添加待办事项列表的分组。
下面的代码直接基于 egreg 的代码,几乎可以正常工作,尽管它总是会添加最后一节的标题,即使没有 todonote 也是如此。所以这是我需要帮助修复的第一件事,其次,可能有比使用基于的代码更简单的方法来做到这一点classicthesis
。
我还没有结婚tocloft
,但我很喜欢它的定制(这不在本 MWE 中)。
\documentclass{scrartcl}
\usepackage{todonotes,tocloft,xpatch,hyperref}
% This is based on classicthesis chapter definition
\let\oldsec=\section
\renewcommand*{\section}{\secdef{\Sec}{\SecS}}
\newcommand\SecS[1]{\oldsec*{#1}}%
\newcommand\Sec[2][]{\oldsec[\texorpdfstring{#1}{#1}]{#2}}%
% https://tex.stackexchange.com/a/61267/11984
\makeatletter
\xapptocmd{\Sec}{\addtocontents{tdo}{\protect\todoline{\thesection}{#1}{}}}{}{}
\newcommand{\todoline}[1]{\@ifnextchar\Endoftdo{}{\@todoline{#1}}}
\newcommand{\@todoline}[3]{%
\@ifnextchar\todoline
{}
{\contentsline{section}{\numberline{#1}#2}{#3}{}{}}%
}
\let\l@todo\l@subsection
\newcommand{\Endoftdo}{}
\AtEndDocument{\addtocontents{tdo}{\string\Endoftdo}}
\makeatother
\begin{document}
\listoftodos
\tableofcontents
\section{My first Section}\todo{Section 1 note}
\subsection{A first Subsection}
\subsubsection{A first subsubsection}\todo{Section 1, section 1, subsection note}
\subsection{Another subsection}\todo{Section 1, section 1, subsection note 2}
\section{My second Section}
\subsection{Another subsection}\todo{Section 2, subsection 1 note}
\subsection{Another subsection}\todo{Section 2, subsection 2 note}
\section{A section without notes}
\subsection{A subsection without notes}
\subsubsection{A subsubsection without notes}
\end{document}
答案1
这是一个可能的解决方案;只有带有\todo
注释的部分的信息才会写入待办事项列表:
\documentclass{scrartcl}
\usepackage{todonotes,xpatch,hyperref}
% initial definitions for storing the section info (name and number)
\def\thissectiontitle{}
\def\thissectionnumber{}
\newtoggle{noTodos}
\makeatletter
% redefinition of \@sect so \section glbally stores its name and number
\def\@sect#1#2#3#4#5#6[#7]#8{%
\ifnum #2>\c@secnumdepth
\let\@svsec\@empty
\else
\refstepcounter{#1}%
\protected@edef\@svsec{\@seccntformat{#1}\relax}%
\fi
\@tempskipa #5\relax
\ifdim \@tempskipa>\z@
\begingroup
#6{%
\@hangfrom{\hskip #3\relax\@svsec}%
\interlinepenalty \@M #8\@@par}%
\endgroup
\csname #1mark\endcsname{#7}%
\addcontentsline{toc}{#1}{%
\ifnum #2>\c@secnumdepth \else
\protect\numberline{\csname the#1\endcsname}%
\fi
#7}%
\else
\@xsect
\def\@svsechd{%
#6{\hskip #3\relax
\@svsec #8}%
\csname #1mark\endcsname{#7}%
\addcontentsline{toc}{#1}{%
\ifnum #2>\c@secnumdepth \else
\protect\numberline{\csname the#1\endcsname}%
\fi
#7}}%
\fi
\@xsect{#5}%
\ifnum#2=1\relax
\global\def\thissectiontitle{#8}
\global\def\thissectionnumber{\thesection}
\fi%
}
\pretocmd{\section}{\global\toggletrue{noTodos}}{}{}
% the \todo command does the job: the first time it is used after a \section command,
% it writes the information of the section to the list of todos
\AtBeginDocument{%
\xpretocmd{\todo}{%
\iftoggle{noTodos}{
\addtocontents{tdo}{\protect\contentsline {section}%
{\protect\numberline{\thissectionnumber}{\thissectiontitle}}{}{} }
\global\togglefalse{noTodos}
}{}
}{}{}%
}
\makeatother
\begin{document}
\listoftodos
\tableofcontents
\section{My first Section}\todo{Section 1 note}
\subsection{A first Subsection}
\subsubsection{A first subsubsection}\todo{Section 1, section 1, subsection note}
\subsection{Another subsection}\todo{Section 1, section 1, subsection note 2}
\section{My second Section}
\subsection{Another subsection}\todo{Section 2, subsection 1 note}
\subsection{Another subsection}\todo{Section 2, subsection 2 note}
\section{A section without notes}
\subsection{A subsection without notes}
\subsubsection{A subsubsection without notes}
\section{My fourth Section}
\subsection{Another subsection}\todo{Section 2, subsection 1 note}
\subsection{Another subsection}\todo{Section 2, subsection 2 note}
\end{document}
备注及说明
该
\todo
命令执行作业并决定是否将部分信息写入待办事项列表;每当\todo
在文档中使用该命令时,它都会检查布尔标志;如果布尔值为假,则将部分信息添加到待办事项列表中并将布尔值设置为真;否则,它不会添加任何信息。该
\section
命令也被修改以重置布尔值。\@sect
已被修改为全局存储有关该部分相应标题和编号的信息;然后将在写入待办事项列表时使用该信息。
使用版本titlesec
在评论中提到,该titlesec
包正在被使用(这可能会对 KOMA 类产生不良影响;请参阅KOMA-Script 与 titlesec 之间的不兼容性)。在这种情况下,使用该explicit
选项代码会大大简化,因为现在无需挂接来\@sect
检索章节的名称和编号:\documentclass{scrartcl} \usepackage{todonotes,xpatch,hyperref} \usepackage[explicit]{titlesec}
% initial definitions for storing the section info (name and number)
\def\thissectiontitle{}
\def\thissectionnumber{}
\newtoggle{noTodos}
\titleformat{\section}
{\normalfont\Large\bfseries\sffamily}
{\thesection}
{0.5em}
{\gdef\thissectiontitle{#1}\gdef\thissectionnumber{\thesection}#1}
\pretocmd{\section}{\global\toggletrue{noTodos}}{}{}
% the \todo command does the job: the first time it is used after a \section command,
% it writes the information of the section to the list of todos
\AtBeginDocument{%
\xpretocmd{\todo}{%
\iftoggle{noTodos}{
\addtocontents{tdo}{\protect\contentsline{section}%
{\protect\numberline{\thissectionnumber}{\thissectiontitle}}{}{} }
\global\togglefalse{noTodos}
}{}
}{}{}%
}
\begin{document}
\listoftodos
\tableofcontents
\section{My first Section}\todo{Section 1 note}
\subsection{A first Subsection}
\subsubsection{A first subsubsection}\todo{Section 1, section 1, subsection note}
\subsection{Another subsection}\todo{Section 1, section 1, subsection note 2}
\section{My second Section}
\subsection{Another subsection}\todo{Section 2, subsection 1 note}
\subsection{Another subsection}\todo{Section 2, subsection 2 note}
\section{A section without notes}
\subsection{A subsection without notes}
\subsubsection{A subsubsection without notes}
\section{My fourth Section}
\subsection{Another subsection}\todo{Section 2, subsection 1 note}
\subsection{Another subsection}\todo{Section 2, subsection 2 note}
\end{document}