1

I would like to create a command that: when an argument is given, it saves it "somewhere", and when no argument is given, it prints all saved arguments. Something like this:

\example{text1}
\example{text2}
\example{text3}

(not output)

\example

Output: text1,text2,text3

If this is not possible with the same command, there could be 2 different commands. I guess this is possible since some templates print the information of "\author" when calling \maketitle.

kuonb
  • 111

1 Answers1

2

I'd not use the same command with two different purposes.

\documentclass{article}

%\usepackage{xparse} % uncomment for LaTeX prior to 2020-10-01

\ExplSyntaxOn \seq_new:N \g_kuonb_example_seq

\NewDocumentCommand{\example}{m} { \seq_gput_right:Nn \g_kuonb_example_seq { #1 } }

\NewDocumentCommand{\printexample}{+O{,~}} { \seq_use:Nn \g_kuonb_example_seq { #1 } } \ExplSyntaxOff

\example{text1} \example{text2} \example{text3}

\begin{document}

\printexample

\printexample[\par]

\end{document}

The optional argument to \printexample is what is set between any two items.

This could be extended if you give more details about the intended working.

enter image description here

egreg
  • 1,121,712