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.

2
  • You could define your own ToC tex.stackexchange.com/questions/61086/… and then add to it your text with ` \addcontentsline`.
    – CarLaTeX
    Commented Oct 7, 2017 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
    Commented Oct 7, 2017 at 4:06

1 Answer 1

3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .