# How to create a label, and refer to it within a \newcommand environment

I've searched quite a bit but haven't found a solution quite yet. I am working on a LaTeX labbook, and in addition the "project" sorting of the experiments, I would like to add a chapter called "log" where I have a chronological list of what I did and a reference to the page in which the experiment is done. To do so I created 2 new commands. The first is \logg{}{} in which I add the date in the first argument, and a short description of what I did in that day for a specific project in the second argument. The second command is called \showlogg and should sort all the logg entries by the date, and display them using "date" "description" and the page that was referencing.

Here is an example:

\documentclass[a4paper]{report}

\usepackage{hyperref}
\usepackage{datatool} %pour créer des databases, surtout nécessaire pour les macros
\usepackage{ifthen}

\DTLnewdb{list} %important pour la commande \logg
\newcounter{logglabel}
\setcounter{logglabel}{0}
\newcommand\logg[2]{% fait une entree dans le labbook de type \logg{date}{résumé de ce qui a été fait}
{\vspace{0.5cm}\large{#1}}%
\stepcounter{logglabel}
\label{\thelogglabel}
\DTLnewrow{list}
\DTLnewdbentry{list}{Date}{#1}
\DTLnewdbentry{list}{Description}{#2}
\DTLnewdbentry{list}{Page}{\pageref{\thelogglabel}}
}

\newcommand\showlogg{% fait un index avec toutes les entrées de \logg triées par date, avec pour infos date, résumé, et page de l'experience.
\DTLsort{Date}{list}
\DTLforeach*{list}{\date=Date,\desc=Description,\pa=Page}{\textbf{\date} \hspace{0.25cm} \desc \dotfill \pa \\}
}

\begin{document}

\chapter{My labbook}
\section{My first experiment}
Here is an experiment that I did on \logg{20171006}{I tried banana and coco}\\
I tried to grow banana but bananas wouldn't grow, I tried to grow coco but coco wouldn't grow.
\newpage %just to show what happens when both \logg are on different pages

\section{My second experiment}
Here is something I did on \logg{20171002}{I tried to grow apples and oranges}\\
Actually before bananas and coco I also tried apples and oranges.
\newpage

\section{The log of my experiments}
\showlogg

\end{document}


My current issue is that when I put several \logg entries in my file, the command \showlogg shows the correct dates, the correct descriptions but always the page of the last \logg entry. Is there a way to make sure that the page is referring to the right place?

Or more generally formulated: is it possible to create a command, that automatically creates a label, where the tag within the label (\label{tag}) is of incrementing numbers, and the \pageref used right afterwards is with the exact same number?

• I am not familiar with DTBnewdbentry, but if it uses \def then \thelogglabel will not be expanded until it is used, at which point the value will have changed. In fact, any temporary macro will not be expanded until later. – John Kormylo Oct 6 '17 at 15:39
• BTW, you should also probably be using \getrefbykeydefault (refcount or hyperref) instead of \pageref. – John Kormylo Oct 6 '17 at 15:43
• This is related: tex.stackexchange.com/questions/202574/… – Steven B. Segletes Oct 6 '17 at 17:49
• DTBnewdbentry is part of the package datatools. From what I know from this package and the way I used it here, is that I created a database, and each time I use the command \logg, it will add an entry to this database. This is the reason why I am not understanding why it wouldn't work, but your explanations seems good. I will have a look into it. I will have a deeper look into \getrefbykeydefault, I am not familiar with this package. Thank you for your inputs! – krak Oct 6 '17 at 17:51
• @Christian: the more you know... That tiny button was not really obvious! :) – krak Nov 1 '17 at 18:46

The \DTLnewdbentry macro does not expand the value (i.e. the 3rd argument) by default, so \DTLnewdbentry{list}{Page}{\thelogglabel} will always write \thelogglabel into the list and this will expanded on the usage of the list display, using the current value of logglabel but not the value at the time of definition.

Use \dtlexpandnewvalue in order to enable the expansion of values.

The \stepcounter macro must be replaced by \refstepcounter.

In order to provide hyperlinks, extract the anchor value with \getrefbykeydefault{\thelogglabel}{anchor}.

Also replace \\ by empty lines.

\documentclass[a4paper]{report}

\usepackage{datatool} %pour créer des databases, surtout nécessaire pour les macros
\usepackage{ifthen}

\usepackage{hyperref}

\DTLnewdb{list} %important pour la commande \logg
\newcounter{logglabel}
\newcommand\logg[2]{% fait une entree dans le labbook de type \logg{date}{résumé de ce qui a été fait}
{\vspace{0.5cm}\large{#1}}%
\refstepcounter{logglabel}%
\label{\thelogglabel}
\DTLnewrow{list}
\DTLnewdbentry{list}{Date}{#1}
\DTLnewdbentry{list}{Description}{#2}
\dtlexpandnewvalue
\dtlnoexpandnewvalue
}

\newcommand\showlogg{% fait un index avec toutes les entrées de \logg triées par date, avec pour infos date, résumé, et page de l'experience.
\DTLsort{Date}{list}
\DTLforeach*{list}{\date=Date,\desc=Description,\pa=Page}{\textbf{\date} \hspace{0.25cm} \desc \dotfill \pa \\}
}

\begin{document}

\chapter{My labbook}
\section{My first experiment}
Here is an experiment that I did on \logg{20171006}{I tried banana and coco}

I tried to grow banana but bananas wouldn't grow, I tried to grow coco but coco wouldn't grow.

\clearpage%just to show what happens when both \logg are on different pages

\section{My second experiment}
Here is something I did on \logg{20171002}{I tried to grow apples and oranges}

Actually before bananas and coco I also tried apples and oranges.
\clearpage

\section{The log of my experiments}
\showlogg

\end{document}