# Custom LaTeX commands involving auxiliary files

Can someone with low-level LaTeX knowledge please help me create new customized commands? The commands should be named \storeitem and \typesetitems, and they should work like this:

\typesetitems
\storeitem{hello}

some other stuff

\typesetitems
\storeitem{world}


This is what the commands should do:

1. On first run of LaTeX, the \storeitem commands should put their arguments into an auxiliary file, in this case "hello" and "world".
2. Each instance of \typesetitems (which is assumed to occur the equal number of times as the \storeitem command - twice in the above example) should put all the stored items in a regular bulleted list (itemize) with a normal font, with the exception that the N-th occurrence of \typesetitems should typeset the N-th item in an italic font.

Thus, the results of the above commands would be something like:

A few other things that should be included if possible, for more friendliness and robustness:

1. If the number of occurrences of \storeitem and \typesetitems don't match, produce a warning. (And do something graceful with the "overflowing" commands, which could be either of them.)

2. If one edits the arguments of existing \storeitems, adds new \storeitems, or deletes existing \storeitems, produce some kind of a "Labels changed, run again" warning message. (Similar to the one that LaTeX does by default when labels have changed.)

3. Maybe later I may not want itemize. Maybe I want numbered lists. Or maybe I do not want lists at all. And maybe I want to do something other to the N-th item than italicize it. For these reasons, it should be rather straightforward to edit the custom commands to suit one's needs.

-
why have \typesetitems\storeitem{world} and worry about not having the commands in pairs rather then just having \storeandtypesetitem{world} which does both jobs. printing the whole list but italicising the current one. –  David Carlisle Oct 6 '12 at 22:11

The following stores the items in references (item1, item2, item3, …) in the .aux file. And the last number is remembered in reference itemlast. \typesetitems sets the items in environment itemize getting the stored items from references item1 up to item<itemlast>. Two counters are used, storeitem counts the stored items and typesetitems the number of \typesetitems. If at the end of document the two counters differ, a warning is given.

\documentclass{article}
\usepackage{atveryend}
\usepackage{zref-base}
\newcounter{storeitem}
\newcounter{typesetitems}

\makeatletter
\newcommand*{\storeitem}[1]{%
\@bsphack
\begingroup
\stepcounter{storeitem}%
\zref@setcurrent{default}{#1}%
\zref@wrapper@immediate{%
\zref@labelbyprops{item\the\value{storeitem}}{default}%
}%
\endgroup
\@esphack
}
\AfterLastShipout{%
\zref@wrapper@immediate{%
\zref@setcurrent{default}{\the\value{storeitem}}%
\zref@labelbyprops{itemlast}{default}%
}%
\ifnum\value{storeitem}=\value{typesetitems}%
\else
\@latex@warning@no@line{%
Mismatch store/typeset items:\MessageBreak
\string\storeitem: \the\value{storeitem}\MessageBreak
\string\typesetitems: \the\value{typesetitems}%
}%
\fi
}
\newcommand*{\typesetitems}{%
\stepcounter{typesetitems}%
\zref@refused{itemlast}%
\ifnum\zref@extractdefault{itemlast}{default}{0}>0 %
\begin{itemize}
\count@=0 %
\@whilenum\count@<\zref@extractdefault{itemlast}{default}{0}\relax
\do{%
\item
\ifnum\value{typesetitems}=\count@
\expandafter\textit
\else
\expandafter\@firstofone
\fi
{\zref@extract{item\the\count@}{default}}%
\zref@refused{item\the\count@}%
}%
\end{itemize}%
\fi
}
\makeatother

\begin{document}
\typesetitems
\storeitem{hello}

some other stuff

\typesetitems
\storeitem{world}
\end{document}


-