为环境添加另一个可选参数

为环境添加另一个可选参数

我已经定义了一个示例环境,我使用它如下来生成编号示例并将示例标题发送到示例索引:

\begin{Example}[one]{Cooking eggs}
To cook an egg, ...
\end{Example}

但是,如果我有另一个相关的例子,并且

\begin{Example}[two]{Cooking eggs: microwave method}
To microwave an egg, ...
\end{Example}

我得到了单独的索引条目,

Cooking eggs                      1
Cooking eggs: microwave method    1

但我宁愿让第二个成为子条目(即用于Cooking eggs!microwave method索引条目),但仍然将标题打印为Cooking eggs: microwave method

Cooking eggs                  1
    microwave method          1

因此,我宁愿重新定义我的示例环境以将其用作以下内容:

\begin{Example}[two]{Cooking eggs}{microwave method}
To microwave an egg, ...
\end{Example}

甚至(两个避免处理另一个可选参数,以解析为代价)

\begin{Example}[two]{Cooking eggs!microwave method}
To microwave an egg, ...
\end{Example}

我该如何重写环境命令来执行此操作?

下面是我对示例环境的当前定义的 MWE:

\documentclass[12pt]{book}
\usepackage{index}

% Define a separate Examples Index
\newcommand{\ixe}[1]{\index[xmp]{#1}}
\newcommand{\ixeon}[1]{\ixe{#1|(}}      % when not automatically done by Example
\newcommand{\ixeoff}[1]{\ixe{#1|)}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Numbered examples that can be referenced and produce index entries
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\newcounter{example}[chapter]
\renewcommand{\theexample}{\thechapter.\arabic{example}}
\newenvironment{Example}[2][\theexample]{%
  \refstepcounter{example}%
  \label{ex:#1}%
  \def\theexamplename{#2}%
  \begin{trivlist}%
  \item[%
  % \hskip-\labelsep % idiosyncrasy that needs learning
    \textbf{\textsc{Example \theexample}:}] %
    \textbf{#2}\par
  \ixe{#2|(}%
  }{%
    \expandafter\ixe\expandafter{\theexamplename|)}%   magic from Bernd
  \hfill$\triangle$
  \end{trivlist}%
}

\begin{document}

\chapter{First chapter}
\section{First section}
asdfasfasfs

\begin{Example}[one]{Cooking eggs}
To cook an egg, ...
\end{Example}

sfafsf
\begin{Example}[two]{Cooking eggs: microwave method}
To microwave an egg, ...
\end{Example}

\end{document}

答案1

这使用了xparse,添加了第二个可选参数,但用作第三个参数,这更有意义(因为它是第二个(强制)参数的补充。

此外,我已切换至imakeidx提供多个索引文件的版本。

\documentclass[12pt]{book}
\usepackage{imakeidx}
\usepackage{xparse}

\usepackage{blindtext}

\makeindex[name=example]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Numbered examples that can be referenced and produce index entries
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\newcounter{example}[chapter]
\renewcommand{\theexample}{\thechapter.\arabic{example}}
\NewDocumentEnvironment{Example}{+O{\theexample}+m+o}{%
  \refstepcounter{example}%
  \label{ex:#1}%
  \def\theexamplename{#2}%
  \begin{trivlist}%
  \item[%
  % \hskip-\labelsep % idiosyncrasy that needs learning
    \textbf{\textsc{Example \theexample}:}] %
    \IfValueTF{#3}{%
    \textbf{#2 -- #3}\par
      \index[example]{#2!#3}
    }{%
    \textbf{#2}\par
      \index[example]{#2}
    }}{%
    \hfill$\triangle$
  \end{trivlist}%
}


\begin{document}

\chapter{First chapter}
\section{First section}
\blindtext

\begin{Example}[one]{Cooking eggs}
To cook an egg, ...
\end{Example}

\blindtext
\begin{Example}[two]{Cooking eggs}[microwave method]
To microwave an egg, ...
\end{Example}


\blindtext
\begin{Example}[three]{Cooking eggs}[Solar method]
To cook an egg using the sun just position it outside and wait
\end{Example}



\printindex[example]

\end{document}

在此处输入图片描述

相关内容