1

I define a new command taking an arguments, e.g., \mycmd{some text here}.

This command is used many times in the documents like:

\documentclass{a-latex-class}

\begin{document}

\mycmd{text number 1}
\mycmd{text number 2}
\mycmd{text number 3}
\end{document} 

Here is my question. How do I define another command, \whatiwant, that print all of texts given as arguments in \mycmd?

Moreover, I expect to use the command \whatiwant before using of any \mycmd, for example:

\documentclass{a-latex-class}

\begin{document}
\whatiwant
\mycmd{text number 1}
\mycmd{text number 2}
\mycmd{text number 3}
\end{document} 

What I have try is using a temporary/auxiliary file storing the texts I want. However, it seems that I need to typeset the document before using the temporary file which is not convenient to do in that way.

  • You could define your own ToC tex.stackexchange.com/questions/61086/… and then add to it your text with ` \addcontentsline`. – CarLaTeX Oct 7 '17 at 3:57
  • You won't get around using an auxiliary file and compile at least twice. TeX typesets a page, ships it out (i.e. appends it to the output file) and then forgets it. After that, it is impossible to change the page. – Mike Oct 7 '17 at 4:06
2

You can use a ToC-like interface, making \whatiwant act similar to \tableofcontents:

enter image description here

\documentclass{article}

\makeatletter
\newcommand{\whatiwant}{\@starttoc{wiw}}
\newcommand{\l@whatiwant}[2]{#1\par}
\newcommand{\mycmd}[1]{\addcontentsline{wiw}{whatiwant}{#1}}
\makeatother

\begin{document}

Lorem ipsum 1 \ldots

\whatiwant

Lorem ipsum 2 \ldots

\mycmd{text number 1}
\mycmd{text number 2}
\mycmd{text number 3}

Lorem ipsum 3 \ldots

\whatiwant% Doesn't print anything

\end{document}

Note that a ToC-like interface generally is used only once. As such, \whatiwant only prints its contents once, but this could be changed.

Since it is a ToC-like setup, you will only see changes after the second compilation.

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.