在以下 MWE 中,如何将章节标题添加到\listofusecase
?我希望用例列在各自的章节标题下,就像在常规 中列出子章节一样\tableofcontents
。
\documentclass{scrartcl}
\usepackage{multicol,tocloft,xcolor}
\newlistof{usecase}{cas}{}
\newlength\lcolwidth\setlength{\lcolwidth}{1.35cm}
\newcommand{\uchead}[1]{%
\parbox[t]{\dimexpr\linewidth-2\fboxsep}{\bfseries\sffamily#1}}
\newenvironment{usecase}[1]{
\refstepcounter{usecase}\setlength\parindent{0pt}
\colorbox{gray!20}{\makebox[\dimexpr\linewidth-2\fboxsep][l]{\uchead{Case~\theusecase~--~#1}}}\par\hrule
\phantomsection\addcontentsline{cas}{usecase}{\protect\numberline{\theusecase}#1}\par}
{\bigbreak}
\begin{document}
\begin{multicols}{2}
\section{Section 1}
\begin{usecase}{Use Case 1}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 2}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 3}
Foo bar
\end{usecase}
\section{Section 2}
\begin{usecase}{Use Case 4}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 5}
Foo bar
\end{usecase}
\section{Section 3}
\begin{usecase}{Use Case 6}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 7}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 8}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 9}
Foo bar
\end{usecase}
\end{multicols}
\begin{multicols}{2}[\section*{List of use cases}]
\listofusecase
\end{multicols}
\end{document}
答案1
你可以加
\renewcommand*{\addsectiontocentry}[2]{%
\addtocentrydefault{section}{#1}{#2}%
\addxcontentsline{cas}{section}[{#1}]{#2}%
}
到你的 MWE 的序言中。
但我建议删除包tocloft
:您可以使用 KOMA-Script 包的命令tocbasic
来定义新列表并格式化条目。
\documentclass{scrartcl}
\usepackage{multicol}
\usepackage{xcolor}
\DeclareNewTOC[
type=usecase,
tocentryindent=0pt,
name=Case,
listname=List of Use Cases
]{cas}
\BeforeStartingTOC[cas]{\begin{multicols}{2}}
\AfterStartingTOC[cas]{\end{multicols}}
\newcounter{usecase}
\newenvironment{usecase}[1]{%
\refstepcounter{usecase}%
\addxcontentsline{cas}{usecase}[\theusecase]{#1}%
\setlength\parindent{0pt}%
\colorbox{gray!20}{\makebox[\dimexpr\linewidth-2\fboxsep][l]{%
\uchead{Case~\theusecase~--~#1}}}%
\par\hrule\par
}
{\bigbreak}
\newcommand{\uchead}[1]{%
\parbox[t]{\dimexpr\linewidth-2\fboxsep}{\bfseries\sffamily#1}}
\renewcommand*{\addsectiontocentry}[2]{%
\addtocentrydefault{section}{#1}{#2}%
\addxcontentsline{cas}{section}[{#1}]{#2}%
}
\begin{document}
\begin{multicols}{2}
\section{Section 1}
\begin{usecase}{Use Case 1}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 2}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 3}
Foo bar
\end{usecase}
\section{Section 2}
\begin{usecase}{Use Case 4}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 5}
Foo bar
\end{usecase}
\section{Section 3}
\begin{usecase}{Use Case 6}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 7}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 8}
Foo bar
\end{usecase}
\begin{usecase}{Use Case 9}
Foo bar
\end{usecase}
\end{multicols}
\listofusecases
\end{document}
结果: