# Creating a list of labels automatically for use by datatool

The following is partly a followup to an earlier question, and Enrico's answer, as given in Automatically creating a table from datatool using references in the text.

The method outlined there works, but is rather slow. It occured to me that one way of improving the speed might be to write the row label information explicitly in a separate file. Consider the example below.

The line

\PrintDocTable{dDB}{Documents}


prints all rows of the table. However, I have the option of explicitly listing the rows I want to print, with

\PrintDocTable[2014.11.14,2013.10.05.powai]{dDB}{Documents}


As long as the rows listed as the same as those referenced, there is no problem. However, keeping these in sync with the references is work.

I think it should be possible to have the \ref macro write a list of the labels automatically in a separate file from the aux file, say example.lab, and then include that file directly inside the [] argument to \PrintDocTable. Something like this:

\PrintDocTable[\include{example.lab}]{dDB}{Documents}


where example.lab in this case would contain the text "2014.11.14,2013.10.05.powai". Is this feasible, and if so, how would I modify the \ref macro?

###########################################
example.tex
###########################################
\documentclass{letter}
\usepackage[verbose]{datatool}
\usepackage{longtable}
\usepackage{array}
\usepackage{url}
\newcounter{tabenum}\setcounter{tabenum}{0}
\newcommand{\nextnuml}[1]{\refstepcounter{tabenum}\thetabenum.\label{#1}}
\makeatletter
\let\oldref\ref
\def\ref#1{%
\immediate\write\@auxout{%
\string\gappto\string\ReferencedIDs{#1,}%
}%
\oldref{#1}%
}
\def\ReferencedIDs{}
\makeatother

\usepackage{fouriernc}
\signature{(Somebody)}
\newcommand*{\checkmissing}[1]{\DTLifnull{#1}{}{#1}}

\newcommand{\PrintDocTable}[3][]{%
% #1 = list of rowIDs
% #2 = database to search
% #3 =caption
\begin{longtable}{r l p{1.5in} c c p{2.5in}}
\caption{#3}\\
\DTLforeach
[\ifblank{#1}{\boolean{true}}{\DTLisSubString{#1}{\RowID}}]
{#2}{%
\RowID=RowID,%
\Date=Date,%
\Filename=Filename,%
\From=From,%
\To=To,%
\Subject=Subject%
}{%
\nextnuml{\RowID} & \Date & {\bfseries\expandafter\url\expandafter{\Filename} } & \checkmissing{\From} & \checkmissing{\To} & \Subject \\
}%
\end{longtable}
}%

\begin{filecontents*}{doc.csv}
familytree,26 Feb 2014,something.txt,subject
2013.10.05.kanjur,05 Oct 2013,something.txt,subject
2013.10.05.powai,05 Oct 2013,something.txt,subject
2014.11.14,14 Nov 2014,something.txt,FROM,TO,,subject
\end{filecontents*}

\begin{document}

\def\today{9th January, 2014}
\begin{letter}{
Someone\\
Somewhere\\
Subject: Some stuff
}

\opening{Dear Someone}

Here are some refs - [\ref{2014.11.14}]. [\ref{2013.10.05.powai}].

\closing{Yours Sincerely,}

%\PrintDocTable{dDB}{Documents}
\PrintDocTable[2014.11.14,2013.10.05.powai]{dDB}{Documents}
\end{letter}
\end{document}


Something like this?

\documentclass{letter}
\usepackage[verbose]{datatool}
\usepackage{longtable}
\usepackage{array}
\usepackage{url}
\newcounter{tabenum}\setcounter{tabenum}{0}
\newcommand{\nextnuml}[1]{\refstepcounter{tabenum}\thetabenum.\label{#1}}
\makeatletter
\AtBeginDocument{\providecommand\ReferencedIDs{}}
\AtEndDocument{\immediate\write\@auxout{\gdef\string\ReferencedIDs{\ReferencedIDs}}}
\let\oldref\ref
\def\ref#1{%
\oldref{#1}%
}
\makeatother

\usepackage{fouriernc}
\signature{(Somebody)}
\newcommand*{\checkmissing}[1]{\DTLifnull{#1}{}{#1}}

\newcommand{\PrintDocTable}[3][]{%
% #1 = list of rowIDs
% #2 = database to search
% #3 =caption
\begin{longtable}{r l p{1.5in} c c p{2.5in}}
\caption{#3}\\
\DTLforeach
[\ifblank{#1}{\boolean{true}}{\DTLisSubString{#1}{\RowID}}]
{#2}{%
\RowID=RowID,%
\Date=Date,%
\Filename=Filename,%
\From=From,%
\To=To,%
\Subject=Subject%
}{%
\nextnuml{\RowID} & \Date & {\bfseries\expandafter\url\expandafter{\Filename} } & \checkmissing{\From} & \checkmissing{\To} & \Subject \\
}%
\end{longtable}
}%

\begin{filecontents*}{doc.csv}
familytree,26 Feb 2014,something.txt,subject
2013.10.05.kanjur,05 Oct 2013,something.txt,subject
2013.10.05.powai,05 Oct 2013,something.txt,subject
2014.11.14,14 Nov 2014,something.txt,FROM,TO,,subject
\end{filecontents*}

\begin{document}

\def\today{9th January, 2014}
\begin{letter}{
Someone\\
Somewhere\\
Subject: Some stuff
}

\opening{Dear Someone}

Here are some refs - [\ref{2014.11.14}]. [\ref{2013.10.05.powai}].

\closing{Yours Sincerely,}