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.

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:

items

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.

share|improve this question
    
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
add comment

1 Answer 1

up vote 2 down vote accepted

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{%
      \advance\count@ by\@ne
      \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}

Result

share|improve this answer
add comment

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.