Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers

up vote 4 down vote accepted

"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

\begin{annotation}[coding,counting]
  \annotationdate{February 1, 2011}
  Some remarks relevant to coding and also to counting.
\end{annotation}

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.

\documentclass{article}

%--------------------------------------------------------------------
%--------------------------------------------------------------------
\makeatletter

\newif\if@print
\@printfalse
\newcommand{\printall}{\@printtrue}

\newcommand\annotationdate[1]{%
  \begin{flushright}
    \textbf{#1}
  \end{flushright}
  \medskip
}

\newenvironment{annotation}[1][]%
  {%
    \@readtags{#1}%
    \if@print
    \else
      \setbox0=\vbox\bgroup\color@begingroup
    \fi
  }{%
    \if@print
    \else
      \color@endgroup\egroup
    \fi
  }%

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

\newcommand{\@readtags}[1]{%
  \def\@tmp{#1}%
  \@dotags
}
\def\@dotags{%
  \expandafter\onetag \@tmp,@@tagfence%
  \next@dotags  
}
\def\onetag#1,#2@@tagfence{%
  \def\tmp{#1}%
  \ifx\tmp\@empty
    \let\next@dotags=\relax
  \else
    \csname tag@@#1\endcsname
    \def\@tmp{#2}%
    \let\next@dotags=\@dotags
  \fi
}

\makeatother
%--------------------------------------------------------------------
%--------------------------------------------------------------------
%--------------------------------------------------------------------
\begin{document}

\begin{center}
  \textbf{\large The Annotations}
\end{center}

\printtagged{hoho}
\printtagged{haha}
%\printall

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

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

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

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

\end{document}
share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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