根据过滤规则的条件目录

根据过滤规则的条件目录

我希望文档中有多个目录,每个目录都基于一个简单的过滤器,例如包含标题/章节标题中的特定单词。我可能有多个但定义明确的关键字,并且我希望每个关键字都有一个单独的目录,即:

  • \tableofcontents“实施”<- 包含单词“实施”及其父级的所有章节/部分的目录
  • \tableofcontents“设计”<- 包含单词“设计”及其父级的所有章节/小节的目录
  • 等等

可以 grep/sed tex 文件来找到这些标题,然后生成链接列表,但也许可以在 latex 中直接完成此操作?

以下是模式详细示例:

\usepackage{mdframed}

\definecolor{MISRARuleBackgroundColor}{rgb}{0.86,0.88,0.94}
\definecolor{RuleBackgroundColor}{rgb}{0.86,0.94,0.88}
\definecolor{RuleBorderColor}{rgb}{0.29,0.39,0.68} 

\newcommand{\MISRARule}[3]{\bigskip\begin{mdframed}[backgroundcolor=MISRARuleBackgroundColor,linecolor=RuleBorderColor,linewidth=1pt]\textbf{Rule #1 (#2)} \newline \textbf{#3} \label{#1}\end{mdframed}\nopagebreak See MISRA C++ 2008 \cite{MISRAC++2008}}

\newcommand{\Rule}[3]{\bigskip\begin{mdframed}[backgroundcolor=MISRARuleBackgroundColor,linecolor=RuleBorderColor,linewidth=1pt]\textbf{Rule #1 (#2)} \newline \textbf{#3} \label{#1}\end{mdframed}\nopagebreak}


.....

\Rule{A0-4-1}{required, toolchain, non-automated}{Floating-point implementation shall comply with IEEE 754 standard.}

\Rule{A0-4-3}{required, toolchain, automated}{The implementations in the chosen compiler shall strictly comply with the C++14 Language Standard.}

\MISRARule{M1-0-2}{required, implementation, non-automated}{Multiple compilers shall only be used if they have a common, defined interface.}

\MISRARule{M15-0-3}{required, implementation, automated}{Control shall not be transferred into a try or catch block using a goto or a switch statement.}

\Rule{A15-0-2}{required, implementation, partially automated}{At least the basic guarantee for exception safety shall be provided for all operations. In addition, each function may offer either the strong guarantee or the nothrow guarantee}

...

Then I  would write something like

\begin{appendix}

\chapter{Allocation of rules to work products}

\section{Rules related to toolchain}

# list here the table of contents / table of references to all rules that are allocated to "toolchain"
\tableofcontents_mframeds_that_contain_string{toolchain}

\section{Rules related to implemtentation}

# list here the table of contents / table of references to all rules that are allocated to "implementation"
\tableofcontents_mframeds_that_contain_string{implementation}


\end{appendix}}

答案1

我不确定“父母”是什么意思,这个问题有点模棱两可,因为我不完全清楚在哪里搜索关键词,以及我们是否应该匹配整个单词,或者是否implementations应该算作匹配implementation。许多目录的格式也没有指定。因此,代码可能不完全符合 OP 的要求,但修改它应该很简单——除了可能没有解释的“父母”。

我的代码定义了四个宏:

  • \DefineContentKeywords-- 定义要跟踪的关键字。例如,\DefineContentKeywords{design,implementation,toolchain}
  • \AddContentsLine-- 用于\Rule将当前“规则”添加到不同的目录索引中(“内部”命令)
  • \ContentsForKeyword-- 将打印某个特定关键字的目录。例如,\ContentsForKeyword{design}。此命令会自动创建节标题。
  • \AllContentsTables打印所有目录。不同表格的打印顺序与定义顺序相同。此命令创建一个\appendix并处理所有标题。

在内部,代码的作用是创建各种LaTeX3序列来存储关键词和相应的目录列表。使用regexLaTeX3 中的宏来搜索关键词。与 OP 一样,目录必须出现所有规则。如果它们可能需要在指定规则之前出现,那么就需要采用不同的方法,将数据写入一个或多个文件。这很容易做到,但问题的表述方式似乎很自然。

我对使用 LaTeX3 如此轻松地完成这项工作感到非常印象深刻,所以我想我可能已经不再说 LaTeX3 中的语法很糟糕了,因为这段代码展示了它实际上有多么强大:)

对于 OP 中的 MWE,代码生成以下“目录”:

在此处输入图片描述

OP 没有说明这些应该采用什么格式,所以我只给出了规则编号和页码(适用1于 MWE 中的所有规则,但我检查了当页码更改时这确实可以正常工作)。我添加了超链接并将页码设为超链接。

#3在 的参数 (keywords?) 和#4(description?)中搜索关键字\Rule,如果它们出现在单词开头,则将它们添加到目录中。因此,design将匹配designdesignsdesigned不匹配indesign。[顺便说一句,\label{...}OP 内部的\Rule并没有做太多事情,因为此时除了页码外,没有任何标签。我不确定这个标签将如何使用,但最好放在\def\@currentlabel{#2}之前\label{#2},这需要在 之间夹入宏定义\makeatletter...\makeatother。]

完整代码如下:

\documentclass{article}

\usepackage{mdframed}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn

\seq_new:N \contents_keywords_seq% a sequence of the keywords to track

% Usage: \DefineContentKeywords{ keyword } define a new keyword to be tracked
\NewDocumentCommand\DefineContentKeywords{ m }{
  \seq_set_from_clist:Nn \contents_keywords_seq {#1}
  \seq_map_inline:Nn \contents_keywords_seq {
    \seq_new:c {contents_for_##1}  % create sequence for each keyword
  }
}

% Usage: \AddContentsLine{number}{description}
\NewDocumentCommand\AddContentsLine{ mm }{
  \seq_map_inline:Nn \contents_keywords_seq {
    \regex_match:nnT { \b##1 }{#2}{ \seq_put_right:cx {contents_for_##1}{#1}}
  }
}

\cs_generate_variant:Nn  \int_compare:nNnT { xNNT }

% Usage: \ContentsForKeyword{keyword}
\NewDocumentCommand\ContentsForKeyword{ m }{
  \subsection*{Rules~related~to~#1}
  \int_compare:xNNT {\seq_count:c {contents_for_#1}} > 0{
    \begin{enumerate}
      \seq_map_inline:cn {contents_for_#1}{\item[] ##1\dotfill\pageref{##1}}
    \end{enumerate}
  }
}
\NewDocumentCommand\AllContentsTables{}{
  \appendix
  \section{Allocation~of~rules~to~work~products}
  \seq_map_inline:Nn \contents_keywords_seq {\ContentsForKeyword{##1}}
}

\ExplSyntaxOff
\definecolor{MISRARuleBackgroundColor}{rgb}{0.86,0.88,0.94}
\definecolor{RuleBackgroundColor}{rgb}{0.86,0.94,0.88}
\definecolor{RuleBorderColor}{rgb}{0.29,0.39,0.68}

% define keywords to track
\DefineContentKeywords{design,
                       implementation,
                       toolchain
}

% \Rule[background colour]{number}{keywords}{description}
\newcommand{\Rule}[4][]{%
  \AddContentsLine{#2}{#3 #4}% search "#3 #4" for keywords
  \bigskip%
  \begin{mdframed}[backgroundcolor=#1RuleBackgroundColor,
                   linecolor=RuleBorderColor,
                   linewidth=1pt]
    \textbf{Rule #2 (#3)} \newline \textbf{#4} \label{#2}
  \end{mdframed}\nopagebreak%
}

\newcommand{\MISRARule}[3]{%
  \Rule[MISRA]{#1}{#2}{#3}%
  See MISRA C++ 2008 \cite{MISRAC++2008}%
}

\begin{document}

  \Rule{A0-4-1}{required, toolchain, non-automated}{Floating-point implementation shall comply with IEEE 754 standard.}

  \Rule{A0-4-3}{required, toolchain, automated}{The implementations in the chosen compiler shall strictly comply with the C++14 Language Standard.}

  \MISRARule{M1-0-2}{required, implementation, non-automated}{Multiple compilers shall only be used if they have a common, defined interface.}

  \MISRARule{M15-0-3}{required, implementation, automated}{Control shall not be transferred into a try or catch block using a goto or a switch statement.}

  \Rule{A15-0-2}{required, implementation, partially automated}{At least the basic guarantee for exception safety shall be provided for all operations. In addition, each function may offer either the strong guarantee or the nothrow guarantee}

  ...

  \newpage
  \AllContentsTables

\end{document}

我还对代码进行了合理化(并修复了背景颜色的使用),以便\MISRARule现在调用\Rule。我使用了\sections 和\subsections 而不是\chapters 和\sections 来缩短输出。

相关内容