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 am working on an ever evolving document, which will contain my notes on certain topics which I learned outside school. I would like to use Latex to document it as it can sometimes contains math and on the side not it just looks better.

Here is the an example:

Topic: What not to do in Latex.

Tag: latex,

Content: My tip, learning on how not to screw up things in latex.


Topic: Do not short +ve and -ve of battery.

Tag: Engineering

Content: Never do this, as you could die.


Topic: Always use jack-stand when changing oil

Tag: Cars

Content: Seriously don't do this, you could kill yourself.


Topic: Latex again

Tag: Latex

Content: Some new content, notes, tips on latex. etc. etc.

The sections above are in random order thus I am planning to use sections*{Topic}. However I would like to create a hyperlinked table of content to organize them which should look like following:

Cars

  1. Always use jack-stand when changing oil.

Latex

  1. What not to do in Latex.
  2. Latex again

Engineering

  1. Do not short +ve and -ve of battery.

… and so on.
In above example I used only one tag per topic, but multiple tags should contain the topic listed in each one of them (duplicate content)

So questions I have:

  1. Is this doable in Latex?
  2. Is there an alternate way of organizing ever-evolving document with content in random order?

One solution I have is to manually create the Tags as sections and content as subsections, but I can see this getting tedious as the document length increases. Thus looking for an automated solution.

share|improve this question
1  
Welcome to TeX.SE. This is definitely doable in LaTeX. I would recommend you add at least one example that contains multiple tags. My recommendation would be to keep each of these random notes in a different files, and I was going to recommend a directory for each tag, but that won't really work if you can have multiple tags. So the question would come down to how would you want to name/manage the files that contain these notes? –  Peter Grill Oct 21 '12 at 0:52
    
Another thing to consider would be if it would matter what order the notes of one tag were presented in within a tag. Have a look at my answer for Automating error message references, even though that had a different purpose it could be adapted for this use case. –  Peter Grill Oct 21 '12 at 0:56
    
Have you ever thought about the usage of an index instead the TOC? Then you could use makeindex/xindy to perform the ordering. –  knut Dec 3 '12 at 22:15

3 Answers 3

Here's one possibility; three new lists (one for each tag) are defined using \@starttoc; the \tags command is in charge of including the name of the section in the corresponding list(s):

\documentclass{article}
\usepackage{xstring}
\usepackage[explicit]{titlesec}
\usepackage[colorlinks,linkcolor=cyan]{hyperref}

\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection}{1em}{#1\gdef\sectiontitle{#1}}

\newcommand\carsname{Cars}
\newcommand\LaTeXname{\LaTeX}
\newcommand\engineeringname{Engineering}

\makeatletter
\newcommand\tableofcars{%
    \section*{\carsname}\@starttoc{loc}}
\newcommand\tableofLaTeX{%
    \section*{\LaTeXname}\@starttoc{lax}}
\newcommand\tableofengineering{%
    \section*{\engineeringname}\@starttoc{loe}}

\newcommand\tags[1]{\phantomsection%
\@for\@tempa:=#1\do
  {
    \IfStrEq{\@tempa}{cars}{\addcontentsline{loc}{section}{\sectiontitle}}{}
    \IfStrEq{\@tempa}{LaTeX}{\addcontentsline{lax}{section}{\sectiontitle}}{}
    \IfStrEq{\@tempa}{engineering}{\addcontentsline{loe}{section}{\sectiontitle}}{}
}
}
\makeatother

\begin{document}

\tableofcars
\tableofLaTeX
\tableofengineering

\section*{What not to do in \LaTeX}
\tags{LaTeX}
My tip, learning on how not to screw up things in \LaTeX

\section*{Do not short +ve and -ve of battery}
\tags{engineering}
Never do this, as you could die.

\section*{Always use jack-stand when changing oil}
\tags{cars}
Seriously don't do this, you could kill yourself

\section*{\LaTeX\ again}
\tags{LaTeX}
Some new content, notes, tips on latex. etc. etc..

\section*{A test section with two tags}
\tags{cars,engineering}
Some test here

\end{document}

enter image description here

share|improve this answer
1  
Creating a ToC with ltx extention may cause confusion, say for a filename latex.tex the output ToC file would be latex.ltx. –  mkota Oct 21 '12 at 3:41
    
@Jay you're right. Extension changed. Thanks. –  Gonzalo Medina Oct 21 '12 at 3:42
    
In this case, you need to provide a different file extension for each case (as well as defining everything for each tag). Is there a way to automate this to any number of tags, so it's easy to randomly add tags in the future. –  fabikw Oct 21 '12 at 5:19
    
@fabikw sure, it can be done! –  Gonzalo Medina Oct 21 '12 at 5:29
    
@fabikw it's too late here now (1a.m.), so I can add some notes about this in some hours. –  Gonzalo Medina Oct 21 '12 at 5:42

I started from Gonzalo Medina's code. My proposed coding uses only the standard .toc file. The command \newtag has two arguments. The first one is the tag as will be used in the toc file, should not contain commas or braces or anything suspicious, and the second is the tag name which will serve as title for the corresponding table of. Arbitrarily many tags may be used.

To display the entries for a given tag, one uses tableof{mytag}. After having displayed what one wants, one issues \OpenTocFileForWriting which will open the toc file for writing. It is important that this is done after all the \tableof commands. As usual two latex runs are necessary.

\documentclass{article}
\usepackage[colorlinks,linkcolor=blue]{hyperref}

\usepackage[explicit]{titlesec}
\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection}{1em}{#1\gdef\sectiontitle{#1}}

\makeatletter

\let\savedl@section\l@section

\newcommand\newtag[2]{%
    \@namedef{#1name}{#2}%
    \expandafter\let\csname if@#1.\endcsname\iffalse
    \@namedef{l@#1@}##1##2{%
      \csname if@#1.\endcsname\savedl@section{##1}{##2}\fi
      \expandafter\let\csname if@#1.\endcsname\iffalse}}

\newcommand\setflagtrue[1]{\expandafter\let\csname if@#1.\endcsname\iftrue}
\newcommand\nextentryfor@tags[1]{\@for\@tempa:=#1\do{\setflagtrue{\@tempa}}}

\newcommand\tableof[1]{%
   \begingroup
   \section*{\@nameuse{#1name}}%
   \expandafter\let\expandafter\l@section\csname l@#1@\endcsname
   \makeatletter
   \InputIfFileExists {\jobname .toc}{}{}%
   \endgroup}

\newcommand\tags[1]{\phantomsection
\addtocontents{toc}{\string\nextentryfor@tags\string{#1\string}}%
\addcontentsline{toc}{section}{\sectiontitle}}

% as I have learned the hard way, as soon as one does \openout
% the file is overwritten and becomes empty. This is why we have 
% to extract this from the \@starttoc routine and call it only
% after having used the toc file.

\newcommand\OpenTocFileForWriting{%
\expandafter\newwrite \csname tf@toc\endcsname
\immediate \openout \csname tf@toc\endcsname \jobname .toc\relax\@nobreakfalse}

\makeatother

\newtag{cars}{Cars}
\newtag{latex}{\LaTeX}
\newtag{engineering}{Engineering}

\setcounter{tocdepth}{1}

\begin{document}

\tableof{cars}
\tableof{latex}
\tableof{engineering}
\OpenTocFileForWriting

\clearpage

\section*{What not to do in \LaTeX}
\tags{latex}
My tip, learning on how not to screw up things in \LaTeX

\section*{Do not short +ve and -ve of battery}
\tags{cars,engineering}
Never do this, as you could die.

\section*{Always use jack-stand when changing oil}
\tags{cars}
Seriously don't do this, you could kill yourself

\section*{\LaTeX\ again}
\tags{latex}
Some new content, notes, tips on latex. etc. etc..

\section*{A test section with two tags}
\tags{cars,engineering}
Some test here

\section*{A test section with three tags}
\tags{cars,engineering,latex}
Some test here

\end{document}

table of for each tag

share|improve this answer
    
for better compatibility with other packages wanting to use the toc file, the macro \nextentryfor@tags should be defined to gobble its argument, and the \tableof command should give it temporarily its acting meaning. Also, if one puts the \tags command before the section, there is no need to use the starred section and then \tags does not have to do the \addcontentsline and \phantomsection stuff. Also one could imagine then also deal with \subsection at least. –  jfbu Dec 4 '12 at 18:27

My sugestion: One file for each entry, and include (or input) them at will. If each one has the full Topic title, your main file has the section/subsection titles. You could even place all the cars topics in a directory called cars...

To go completely wild, you could have e.g. main.tex, cars/00-intro.tex, cars/01-battery.tex, ..., cars/42-oil-change.tex, LaTeX/00-intro.tex, LaTeX/01-math-tips.tex, ..., LaTeX/99-stackexchange.tex, ... It would be easy to use some string manipulating language (Perl, perhaps) to fill in a minimal main.tex skeleton (or even create it from scratch by just using the directory names, or some directory/TITLE.tex file to make titles) to assemble the whole collection into a sorted document.

(Yes, LaTeX is Turing complete, but it is rather far from my programming language of choice for such a task. And I'm a Unix guy who isn't afraid of the command line.)

share|improve this answer

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.