# Add another optional argument for an environment

I have defined an Example environment that I use as follows to produce a numbered example and send the example title to an example index:

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


But then, if I have another related example, and do

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


I get separate index entries,

Cooking eggs                      1
Cooking eggs: microwave method    1


But I'd rather have the second be a subentry (ie, use Cooking eggs!microwave method for the index entry), yet still print the title as Cooking eggs: microwave method

Cooking eggs                  1
microwave method          1


So, I'd rather redefine my Example environment to use it as something like:

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


or even (two avoid dealing with another optional argument, at the expense of parsing)

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


How can I re-write the environment commands to do this?

Here is a MWE with my current definition of the Example environment:

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

• the \ixe stuff seems to be complicated. I suggest to use imakeidx and it's separate index features – user31729 Feb 12 '15 at 14:04
• you ask for another optional argument (which is possible) but \begin{Example}[two]{Cooking eggs}{microwave method} has one optional and two mandatory arguments, whicch is already supported by \newenviornment Did you mean to use two optional arguments? – David Carlisle Feb 12 '15 at 14:37
• Is it possible to use Small Caps with Bold Face? You are using \textbf{\textsc{}}. The bold face will be applied only to the number and : I guess. – Sigur Feb 12 '15 at 15:04
• @Sigur latex supports \textbf{\textsc{}} but the default cm fonts don't have such a font so it substitutes, so it will work or not in the real document depending on font choice. – David Carlisle Feb 12 '15 at 15:22

This uses xparse, adding a second optional argument, but used as 3rd. argument, which makes more sense (since it's an addition to the 2nd (mandatory) argument.

In addition, I switched to imakeidx which provides for multiple index files.

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


• This solves the problem of the index entries. I edited your code to also use the other optional argument in the example title. – user101089 Feb 12 '15 at 20:25