2

In the following MWE, how can I add section titles into \listofusecase? I'd like the use cases to be listed below their respective section titles in the same way as subsections are listed in the regular \tableofcontents.

enter image description here

\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}
  • tocloft and KOMA? ;-) – user31729 Oct 1 '17 at 15:10
  • @ChristianHupfer Apparently, the two are not compatible (see the comments to this question). – Casimir Oct 1 '17 at 18:59
  • Yes, it's a well-known fact that they are incompatible, that's why I don't use KOMA – user31729 Oct 1 '17 at 19:59
2

You can add

\renewcommand*{\addsectiontocentry}[2]{%
  \addtocentrydefault{section}{#1}{#2}%
  \addxcontentsline{cas}{section}[{#1}]{#2}%
}

to the preamble of your MWE.

enter image description here

But I would suggest to remove package tocloft: You could use the commands of KOMA-Script package tocbasic to define a new list and to format the entries.

\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}

Result:

enter image description here

| improve this answer | |
  • This is great! I learned a lot from your answer. One thing I didn't foresee in my question: Is it possible to add section titles to \listofusecases only if the section contains a usecase environment? (I realize this might be difficult. If it is I might ask a new question.) – Casimir Oct 1 '17 at 19:07
  • Maybe its better to ask a new question. – esdd Oct 1 '17 at 20:02
  • See here. – Casimir Oct 2 '17 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.