I would like to do the following, by I am not sure how.

Say I have a LaTeX file with series of annotations, each with zero or more "tags" (say, about their contents or the project they are related to) and the day it was written down. I would like to be able to compile the same file in two ways:

  • in its entirety, with the annotations in chronological order, with the dates in some evidence, as a kind of log;
  • including only the annotations pertaining to one or more given tags (with or without the dates), as a survey of, say, the project those tags refer to.

Would you think it can be done in a not too complicated way?

  • Do you already have a way to do the annotations? The excluding is a quite different thing. – Martin Scharrer Feb 1 '11 at 23:41
  • Not quite: for now I have a not too structured file, with just some section-like organisation. – DaG Feb 2 '11 at 13:31

"Not too complicated" is in the eye of the beholder.

That being said: Here's a crude way to create a file of "annotations", each of which can have one or more tags, and to print some or all depending on their tags. This won't help you with sorting them by date; it requires that they're already sorted by date, and you just want to pick out certain of them according to how they're tagged.

We define an "annotation" environment, which takes one optional argument. The argument (if it appears) should be a list of tags, separated by commas, with no spaces. We also define the command \annotationdate which takes one argument and prints that argument right justified in boldface; you'd make it the first line in each annotation. For example, you may have

  \annotationdate{February 1, 2011}
  Some remarks relevant to coding and also to counting.

We also define the commands \printtagged and \printall:

  • To print all the annotations with the tag "coding", you'd give the command \printtagged{coding} at the beginning of the file.

  • To print all annotations, whether tagged or not, you'd give the command \printall at the beginning of the file.

Here's a complete latex file that defines all this stuff in the preamble (in between the \makeatletter and the \makeatother), and then demonstrates the use of these things.






% To print the annotations tagged with ``tag'',
% give the command \printtagged{tag}
  \expandafter\def\csname tag@@#1\endcsname{\@printtrue}%

  \expandafter\onetag \@tmp,@@tagfence%
    \csname tag@@#1\endcsname


  \textbf{\large The Annotations}


  \annotationdate{April 18, 1775}
  This is the first annotation.  It was tagged with ``haha''.

  \annotationdate{July 4, 1776} This is the second annotation.  It was
  tagged with both ``haha'' and ``hoho''.

  \annotationdate{July 4, 1776}
  This is the third annotation.  It was tagged with ``hehe''.

  \annotationdate{July 4, 1776}
  This is the fourth annotation.  It wasn't tagged at all.

  • Great, thanks! Exactly what I was looking for, and instructive too. – DaG Feb 2 '11 at 13:30
  • @DaG Happy it helps. Of course, it can be improved, e.g., by making the annotation environment use 2 arguments, with the first still being optional but the second one required and assumed to have the date (thus eliminating the \annotationdate command. That is, perhaps use \newenvironment{annotation}[2][]{\@readtags{#1}\if@print \bigbreak\hfill\textbf{#2}\par\ignorespaces \else etc. – Phil Hirschhorn Feb 3 '11 at 2:05

In ConTeXt, such conditional processing is done using modes. Essentially, Phil Hirschhorn's solution is a reimplementation of \startmode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.