# Document configuration via tags or labels?

This question led to a new package:
tagging

I'm trying to work out options for configuring a large memoir to multiple audiences via multiple editions.

Obviously one method of doing this is by building a master set of sections and then combining them into several different master documents which are basically a string of \input{} commands.

Another might be to define a set of section id environments and then include or exclude them with \newcomment{} \commentson{} and commentsoff{}

Yet another way would be an inverse of this: to tag each section with one or more labels. Something like this:

\begin{tagged}{managers,library}
\chapter{A Simple Introduction}
...
\end{tagged}
\begin{tagged}{sysprogs,library}
\chapter{A Complex Introduction}
...
\end{tagged}
\chapter{How to erase the system}
...
\end{tagged}


where I could then do something at the start like:

\usetag{sysprogs}


to build the "System Programmer" edition.

Is there a package that will do something like this?

-
FWIW, in ConTeXt this feature is called modes. –  Aditya May 17 '12 at 3:32

There is a xcomment package, it isn't tagged, but useful for this kind of question. See also

http://www.tex.ac.uk/cgi-bin/texfaq2html?label=conditional

Here is what you want:

\documentclass{minimal}

\usepackage{verbatim}
\usepackage{etoolbox}

\newcommand\usetag[1]{%
\def\do##1{\csdef{tagged@##1}{}}%
\docsvlist{#1}}

\newenvironment{tagged}[1]%
{\def\do##1{%
\ifcsname tagged@##1\endcsname
\let\comment\relax
\let\endcomment\relax
\fi}%
\docsvlist{#1}
\comment}%
{\endcomment}

\begin{document}

\usetag{a,b,c}

\begin{tagged}{a}
aaaa
\end{tagged}

\begin{tagged}{x}
xxxx
\end{tagged}

\begin{tagged}{a, x}
aaaa xxxx
\end{tagged}

\begin{tagged}{b, c, x}
bbbb cccc xxxx
\end{tagged}

\end{document}


It's not difficult to implement. Whitespace after commas is ignored.

-
That looks brilliant, and much simpler than I had feared. Going off to have a play with it, back soon... –  Brent.Longborough Feb 13 '11 at 19:17
Accepted, +1. Brilliant. Worth an extra upvote just for the line \usepackage{etoolbox}. –  Brent.Longborough Feb 13 '11 at 19:39
A previous version does not contain etoolbox. I used \@for and \@namedef, which is not more complex. I use etoolbox to ignore the extra spaces. However, unlike keyval, spaces before commas is not ignored. You can use \ForEach in forarray package or other trick to ignore more spaces. –  Leo Liu Feb 13 '11 at 19:55

The tagging' package is now on CTAN, providing the following commands:

\tagged{list}{yes}            % Conditional inclusion
\untagged{list}{no}           % Conditional exclusion
\iftagged{list}{yes}{no}      % Conditional inclusion/exclusion
\begin{taggedblock}{list}     % Conditionally included environment
\end{taggedblock}         %
\begin{untaggedblock}{list}   % Conditionally excluded environment
\end{taggedblock}         %
\usetag{list}                 % Turn tags on
\droptag{list}                % Turn tags off
`

My thanks to Leo Liu, for his original answer on which the work is based.

-
Good job. I've just seen this package on CTAN-ann mailing list. –  Leo Liu Aug 29 '11 at 17:31
Thanks, @LeoLiu, the credit's largely yours... –  Brent.Longborough Aug 29 '11 at 23:41