如果部分不包含所述环境,则从自定义环境列表中删除部分标题

如果部分不包含所述环境,则从自定义环境列表中删除部分标题

这是这个

在以下 MWE 中,\listofusecases如果所述章节或小节不包含单个usecase环境,我该如何从中删除章节和小节标题?此外,我该如何删除(子)章节行上的页码和虚线?

在此处输入图片描述

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

答案1

以下是基于以下建议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}

运行三次即可获得

在此处输入图片描述

aux请注意,如果文档结构发生变化,则必须删除该文件。

答案2

以下是埃托克。挑战在于了解某个部分或小节是否包含usecase。为此,将首先执行 TOC,以便etoc提取该信息。然后执行实际操作。

我对 KOMA 用于排版目录的宏知之甚少,因此我决定检查一下踪迹,以侵入“点填充”部分。事实上,scrartcl 类中似乎没有选项可以停用这些宏。

任何设计都etoc可以实现,但这需要工作,并且为了使其与类布局相适应,需要从其代码中提取要使用的宏,而我没有时间去研究。

因此我按照手册中的“另一种兼容模式”方法操作。当我使用\etocsavedsectiontocline等时,这etoc 1.08k至少需要,否则就不需要\let\savedsectiontocline\l@section等...

\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用途只有一个文件。因此我定期\tableofcontents检查它是否被这些usecase条目污染。

在此处输入图片描述

相关内容