检查另一个部分下是否存在非空子部分

检查另一个部分下是否存在非空子部分

我想尝试制作一个 LaTeX 类,强制存在一定数量的(非空)部分——如果用户没有手动定义它们,那么就会创建一些默认部分内容。

\begin{document}

\section{Section which requires subsections ``African swallow'' and ``European swallow''}

\subsection{African swallow}

The unladen airspeed is \ldots  % The section is defined and has content, so it is okay

% subsection ``European swallow'' doesn't exist (and therefore is also missing content, of course), so here the compiler adds:
% \subsection{European swallow}
%
% The unladen airspeed is \ldots, although when carrying a coconut it is much less.

\end{document}

如果我还可以指定某些部分,那就更酷了必须被定义(并且不为空),否则编译器会退出并出现错误。

我希望我的问题是可以理解的...我将非常感激任何提示。

答案1

我假设此功能用于某种“待办事项”标记。如果开始新的部分,则指定所需的部分(逗号分隔列表):

\require{African swallow, European swallow}

如果一个部分完成了,它就会以

\done{African swallow}

在文档的末尾\done报告了缺失的内容。

\documentclass{article}

\usepackage{kvsetkeys}
\makeatletter
\newcommand*{\req@list}{}
\newcommand*{\@require}[1]{%
  \xdef\req@list{%
    \req@list{\detokenize{#1}}%
  }%
}
\newcommand*{\require}[1]{%
  \comma@parse{#1}\@require
}
\newcommand*{\done}[1]{%
  \global\expandafter
  \let\csname done@\detokenize{#1}\endcsname\@empty
}
\newcommand*{\check@requirements}{%
  \begingroup
    \typeout{}%
    \typeout{ToDo list}%
    \typeout{=========}%
    \expandafter
    \@tfor\expandafter\x\expandafter:\expandafter=\req@list\do{%
      \@ifundefined{done@\x}{%
        \typeout{! ToDo: \x}%
      }{}%
    }%
    \typeout{}%
  \endgroup
}
\AtEndDocument{\check@requirements}
\makeatother

\begin{document}

\section{Section which requires subsections ``African swallow'' and ``European swallow''}
\require{African swallow, European swallow}

\subsection{African swallow}

The unladen airspeed is \ldots  % The section is defined and has content, so it is okay
\done{African swallow}

% subsection ``European swallow'' doesn't exist (and therefore is also missing content, of course), so here the compiler adds:
% \subsection{European swallow}
%
% The unladen airspeed is \ldots, although when carrying a coconut it is much less.

\end{document}

LaTeX 运行结束时的报告如下:

ToDo list
=========
! ToDo: European swallow

相关内容