![将章节标题添加到自定义环境列表中](https://linux22.com/image/362875/%E5%B0%86%E7%AB%A0%E8%8A%82%E6%A0%87%E9%A2%98%E6%B7%BB%E5%8A%A0%E5%88%B0%E8%87%AA%E5%AE%9A%E4%B9%89%E7%8E%AF%E5%A2%83%E5%88%97%E8%A1%A8%E4%B8%AD.png)
在以下 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}
结果: