0

I would like to loop through a LaTeX document and collect every instance of a key with multiple entries.

i.e.

\begin{document}
\lipsum[1-1]
\key{name=Peter, age=50, gender=male}
\lipsum[1-1]
\key{name=Max, age=20, gender=male}
\lipsum[1-1]
\key{name=Lena, age=90, gender=female}

\makekeylist

\end{document}

\makekeylist should generate a \tabularx like table with the above entries as well as theire respective page they apear first.

My tries to do this with makeidx so far did not yield a result - and I am a bit clueless towards the right ansatz.

Thanks for all help.

2

enter image description here

\documentclass[a5paper]{article}
\usepackage{keyval,tabularx}
\usepackage{lipsum}


\makeatletter
\def\zz@table{}
\newcount\zz@count
\define@key{zz}{name}{\def\zz@name{#1}}
\define@key{zz}{age}{\def\zz@age{#1}}
\define@key{zz}{gender}{\def\zz@gender{#1}}

\newcommand\key[1]{%
\global\advance\zz@count\@ne
\label{zz\the\zz@count}%
\setkeys{zz}{name=?,age=?,gender=?}%
\setkeys{zz}{#1}%
\xdef\zz@table{\expandafter\unexpanded\expandafter{\zz@table}%
  \zz@name&\zz@age&\zz@gender&\noexpand\pageref{zz\the\zz@count}\noexpand\\}%
}

\newcommand\makekeylist{%
  \begin{center}
    \begin{tabularx}{\textwidth}{XXXX}
    Name&Age&Gender&Page\\
\hline
  \zz@table
\end{tabularx}
\end{center}}

\begin{document}
\lipsum[1-1]
\key{name=Peter, age=50, gender=male}
\lipsum[1-1]
\key{name=Max, age=20, gender=male}
\lipsum[1-1]
\key{name=Lena, age=90, gender=female}

\makekeylist

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.