3

This is a follow-up question to this one.

In the following MWE, how can I remove section and subsection titles from the \listofusecases if said section or subsection doesn't contain a single usecase environment? Also, how can I remove the page numbers and dotted lines on (sub-)section lines?

enter image description here

\documentclass{scrartcl}
\usepackage{multicol}
\usepackage{xcolor}

\DeclareNewTOC[
    type=usecase,
    listname=List of Use Cases,
    tocentryentryformat=\sffamily,
    tocentrypagenumberformat=\sffamily,
    indent=6ex
]{lou}
\renewcommand*{\addsectiontocentry}[2]{%
  \addtocentrydefault{section}{#1}{#2}%
  \addxcontentsline{lou}{section}{\large#2}%
}
\renewcommand*{\addsubsectiontocentry}[2]{%
    \addtocentrydefault{subsection}{#1}{#2}%
    \addxcontentsline{lou}{subsection}{\textbf{\sffamily #2}}%
}

\newcounter{usecase}
\newenvironment{usecase}[1]{%
    \refstepcounter{usecase}%
    \addxcontentsline{lou}{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}}


\begin{document}
\begin{multicols}{2}
\section{Section 1}
\section{Section 2}
\subsection{Subsection 2.1}
\begin{usecase}{Use Case 1}
  Foo bar
\end{usecase}
\subsection{Subsection 2.2}
\begin{usecase}{Use Case 2}
  Foo bar
\end{usecase}
\begin{usecase}{Use Case 3}
  Foo bar
\end{usecase}
\section{Section 3}
\section{Section 4}
\begin{usecase}{Use Case 4}
  Foo bar
\end{usecase}
\begin{usecase}{Use Case 5}
  Foo bar
\end{usecase}
\section{Section 5}
\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}
\section{Section 6}

\end{multicols}

\BeforeStartingTOC[lou]{\begin{multicols}{2}}
\AfterStartingTOC[lou]{\end{multicols}}
\listofusecases

\end{document}
  • you can do such things by using etoc package rather than koma-script facilities; however in the long run, it is probably better to stick to the latter as they are class-provided. – user4686 Oct 2 '17 at 7:52
  • @jfbu So you're saying this is possible using just koma-script but might be more difficult than using etoc? – Casimir Oct 2 '17 at 7:55
  • sorry, I didn't mean to imply it is possible with koma-script. I don't know if it is or not and could not provide a koma-script based solution due to my lack of knowledge. (I guess not printing dots etc.. is ok, the more demanding point is to avoid section/subsection without use cases) – user4686 Oct 2 '17 at 8:00
4

Here is a suggestion which bases on https://komascript.de/node/1949:

\documentclass{scrartcl}
\usepackage{multicol}
\usepackage{xcolor}

\DeclareNewTOC[
  tocentrylevel=\subsubsectiontocdepth,
  type=usecase,
  name=Case,
  listname=List of Use Cases,
  tocentryformat=\sffamily,
  tocentrypagenumberformat=\sffamily,
  tocentryindent=6ex% < name of the option changed
]{lou}
\BeforeStartingTOC[lou]{\begin{multicols}{2}}
\AfterStartingTOC[lou]{\end{multicols}}
\newcounter{usecase}

\newenvironment{usecase}[1]{%
    \refstepcounter{usecase}%
    \addxcontentsline{lou}{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}}

% bases on https://komascript.de/node/1949
\makeatletter

\DeclareTOCStyleEntry[
  level=\sectiontocdepth,
  indent=0pt,
  numwidth=1.5em,
  entryformat=\usekomafont{disposition},
  pagenumberformat=\@gobble,
  linefill=\hfill
]{tocline}{lousection}
\DeclareTOCStyleEntry[
  level=\subsectiontocdepth,
  indent=1.5em,
  numwidth=2.3em,
  entryformat=\usekomafont{disposition},
  pagenumberformat=\@gobble,
  linefill=\hfill
]{tocline}{lousubsection}

\newcounter{sectionID}
\renewcommand{\thesectionID}{secID@\arabic{sectionID}}

\newcommand*\sectionhas@original@addcontentsline{}
\let\sectionhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\sectionhas{\thesectionID}{#1}}%
  \immediate\write\@auxout{\string\subsectionhas{\thesubsection}{#1}}%
  \sectionhas@original@addcontentsline{#1}%
}
\newcommand*{\sectionhas}[2]{%
  \global\@namedef{sectionhas@#1@#2}{true}%
}
\newcommand*{\subsectionhas}[2]{%
  \global\@namedef{subsectionhas@#1@#2}{true}%
}

\renewcommand*{\addsectiontocentry}[2]{%
  \stepcounter{sectionID}%
  \addtocentrydefault{section}{#1}{#2}%
  \ifundefinedorrelax{sectionhas@\thesectionID @lou}
    {}{\addxcontentsline{lou}{lousection}{#2}}%
}
\renewcommand*{\addsubsectiontocentry}[2]{%
  \addtocentrydefault{subsection}{#1}{#2}%
  \ifundefinedorrelax{subsectionhas@\thesubsection @lou}
    {}{\addxcontentsline{lou}{lousubsection}{#2}}%
}
\makeatother

\begin{document}
\begin{multicols}{2}
\section{Section 1}
\section{Section 2}
\subsection{Subsection 2.1}
\begin{usecase}{Use Case 1}
  Foo bar
\end{usecase}
\subsection{Subsection 2.2}
\begin{usecase}{Use Case 2}
  Foo bar
\end{usecase}
\begin{usecase}{Use Case 3}
  Foo bar
\end{usecase}
\section{Section 3}
\section{Section 4}
\begin{usecase}{Use Case 4}
  Foo bar
\end{usecase}
\begin{usecase}{Use Case 5}
  Foo bar
\end{usecase}
\section{Section 5}
\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}
\section{Section 6}
\end{multicols}

\listofusecases
\end{document}

Run three times to get

enter image description here

Note you have to delete the aux file if the structure of the document is changed.

| improve this answer | |
  • Works great! And without any extra packages. I'm impressed. – Casimir Oct 2 '17 at 12:10
4

Here is with etoc. The challenge is to get to know whether a section or subsection contains a usecase. For this the TOC is executed a first time, à la etoc only to extract that information. Then it is executed for the real thing.

I know little about the macros used by KOMA for typesetting the TOC, and I decided to examin a trace to hack into the "dotfill" part. Indeed, there seems to be no option to deactivate them for subsections in scrartcl class.

With etoc any design can be achieved but this requires work and to fit it with the class layout one would need to extract from its code the macros to use, which I had no time to investigate.

Thus I followed method "Another compatibility mode" of manual. As I use \etocsavedsectiontocline etc.. , this requires etoc 1.08k at least, else one does \let\savedsectiontocline\l@section etc...

\documentclass{scrartcl}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{etoc}

\newcounter{usecase}
\etocsetlevel{usecase}{6}
\newenvironment{usecase}[1]{%
    \refstepcounter{usecase}%
    \etoctoccontentsline{usecase}{\string\numberline{\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}}

\makeatletter
\newif\ifsubsectionhasusecase
\newif\ifsectionhasusecase

% \cas@list will be storage for a sequence of 1 and 0 indicating
% which section or subsection contain subcases
% \cas@list will be obtained via a first \tableofcontents
\def\cas@popfirst{\expandafter\cas@popfirst@\cas@list\relax}
\def\cas@popfirst@#1#2\relax{\def\cas@tmp{#1}\def\cas@list{#2}}

\newcommand\listofusecases{%
    \begingroup
      \etocsetlevel{subsubsection}{6}%
      \etocsetlevel{usecase}{3}%
      \etocsetstyle{section}
          {\sectionhasusecasefalse\toks\tw@{}\etocskipfirstprefix}
          {\ifsectionhasusecase\toks@\expandafter{\the\toks@1}%
             \else
                        \toks@\expandafter{\the\toks@0}%
           \fi
           \toks@\expandafter\expandafter\expandafter
            {\expandafter\the\expandafter\toks@\the\toks\tw@}%
          }
          {\sectionhasusecasefalse\toks\tw@{}}
          {\ifsectionhasusecase\toks@\expandafter{\the\toks@1}%
             \else
                        \toks@\expandafter{\the\toks@0}%
           \fi
           \toks@\expandafter\expandafter\expandafter
            {\expandafter\the\expandafter\toks@\the\toks\tw@}%
           \sectionhasusecasefalse}%
      \etocsetstyle{subsection}
          {\subsectionhasusecasefalse\toks\tw@{}\etocskipfirstprefix}
          {\ifsubsectionhasusecase\toks\tw@\expandafter{\the\toks\tw@1}%
             \else
                        \toks\tw@\expandafter{\the\toks\tw@0}%
           \fi
          }
          {\subsectionhasusecasefalse}
          {\ifsubsectionhasusecase\toks\tw@\expandafter{\the\toks\tw@1}%
             \else
                        \toks\tw@\expandafter{\the\toks\tw@0}%
           \fi
           \subsectionhasusecasefalse
           }%
      \etocsetstyle{usecase}{\subsectionhasusecasetrue\sectionhasusecasetrue}{}{}{}%
      %
      \etocsettocstyle
         {\toks@{}}%
         {\xdef\cas@list{\the\toks@}}%
      % fetch whether units include subcases
      \tableofcontents
      % now for the real thing. But I don't want to dig into KOMA code
      % for typesetting TOCs.
      %\KOMAoptions{subsectionentrywithoutdots}% does not exist!
      % and even if existed would it obey group scope ?
      \etocsetstyle{section}
                   {}
                   {\cas@popfirst}
                   {\if1\cas@tmp
                      \etocsavedsectiontocline
                       {\numberline{\etocnumber}\etocname}{}%
                    \fi}
                   {}%
      \etocsetstyle{subsection}
                   {}
                   {\cas@popfirst}
                   {\if1\cas@tmp
                    {% hack to avoid dots (I had to dig into code expansion...)
                      \@namedef{\@backslashchar TOCLineLeaderFill}[####1]{\hfill}%
                      \etocsavedsubsectiontocline
                        {\numberline{\etocnumber}\etocname}{}%
                    }\fi}
                   {}%
      \etocsetstyle{usecase}
                   {}
                   {}
                   {% use KOMA style for subsubsections
                    \etocsavedsubsubsectiontocline
                       {\numberline{\etocnumber}\etocname}{\etocpage}%
                   }
                   {}%
      \etocsettocstyle{\section*{List of use cases}
                       \begin{multicols}{2}\sffamily}
                      {\end{multicols}}%
      \tableofcontents
    \endgroup
}

\makeatletter


\begin{document}

\tableofcontents

\begin{multicols}{2}
\section{Section 1}
\section{Section 2}
\subsection{Subsection 2.1}
\begin{usecase}{Use Case 1}
  Foo bar
\end{usecase}
\subsection{Subsection 2.2}
\begin{usecase}{Use Case 2}
  Foo bar
\end{usecase}
\begin{usecase}{Use Case 3}
  Foo bar
\end{usecase}
\section{Section 3}
\section{Section 4}
\begin{usecase}{Use Case 4}
  Foo bar
\end{usecase}
\begin{usecase}{Use Case 5}
  Foo bar
\end{usecase}
\section{Section 5}
\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}
\section{Section 6}

\end{multicols}

\clearpage % only for nicer view
\listofusecases


\end{document}

etoc uses only one file. Thus I issued a regular \tableofcontents to check it was not contaminated by the usecase entries.

enter image description here

| improve this answer | |
  • 1
    there is whitespace 1 Use Case 1 etc... but this due to subsubsection KOMA Toc style. If you know how to customize that, it will be fixed. Assuming (which I don't doubt) KOMA allows such things locally, so one does not have to worry to reestablish defaults later. Anyway, the main TOC was issued at start of document. – user4686 Oct 2 '17 at 9:53
  • 1
    better looking result from \vfill\hrule height0pt \end{multicols} (or rather \raggedcolumns\end{multicols}) and also \columnsep should be increased. – user4686 Oct 2 '17 at 10:02
  • I have fixed a bug from initial version. I had been using groups but I took them out at some point; using only one toggle is not possible then. New way uses two. (the bug would show if at end of a section containing usecase environments, there was a subsection without). – user4686 Oct 2 '17 at 10:24
  • You can also avoid the whitespace visible in image if using \etocsavedsubsubsectiontocline{\makebox[1.5em][l]{\etocnumber}\etocname}{\etocpage}, i.e. avoiding the \numberline, but typesetting the number directly as one wishes. – user4686 Oct 2 '17 at 10:48

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.