# Use aux file for package

I am writing a Latex package for myself. Just for the learning purpose. Currently it draws some basic shapes which each represents an event. I created a function \event[year][month][year][month][label] which adds a new event to the graphic. The first two parameters are the start date and the second two is the end date.

Now the idea is to draw the events below each other. But because the shapes for the event contains also the label for the event which is sometimes longer than the text, I want to draw them below all other events. Sometimes shapes overlap on the x-axis.

To draw the label, if it is longer than the shape of the event, below all overlapping events, I need to know how many events will be added. So my idea was to use a similar approach like the TOC does. Basically you need two compile runs to generate the final graphics. As far as I know the TOC generation uses the .aux file. But I am not sure how to use it for my own purpose or if there is a better approach?

Graphic which shows my idea:

------
|    -EVENT LABEL-
| -EVENT LABEL-
|
LABEL IS TOO LONG


The write stream for the aux file is \@auxout. With \AtEndDocument there is a hook for delaying some code to the end of the document. You need to be aware that \write expands the code like an \edef before writing it to the stream. You need to protect expandable tokens not to be expanded with \string or \noexpand:

\RequirePackage{filecontents}
\begin{filecontents*}{mypackage.sty}
\newcounter{event}
\newcommand*\event{\stepcounter{event}Event~\theevent}
\newcommand*\numberofevents{0}

\AtEndDocument{%
\immediate\write\@auxout{%
\gdef\string\numberofevents{\arabic{event}}%
}
}
\end{filecontents*}
\documentclass{article}
\usepackage{mypackage}
\begin{document}

There are \numberofevents\ events.

\event, \event, \event

\end{document}

• So I could also just move the draw code into the \AtEndDocument function, without using the .aux file? – Pascal Feb 22 '17 at 12:27
• @Pascal probably not a good idea but it's hard to say more without knowing the specifics – cgnieder Feb 22 '17 at 13:02