2

Consider this example:

\documentclass{article}

\newcommand{\baz}{}
\newenvironment{foo}[1]{#1}{\makeatletter\g@addto@macro\baz{#1}\makeatother}

\begin{document}

\begin{foo}{bar}
\end{foo}
\end{document}

The example does not work. What I am aiming to achieve is concatenating all parameters used with foo, so I can output them at the end of the document. What is the correct approach for that?

3
  • 2
    You cannot access the arguments from within the second body of \newenvironment. Jun 7, 2011 at 0:08
  • 2
    Somewhat unrelated to this problem: you can't use \makeatletter etc. inside command arguments because they are parsed when they are read, not when they are executed.
    – Philipp
    Jun 7, 2011 at 5:08
  • unlike \newenvironment, with \NewDocumentEnvironment{myenv}{m}{}{#1} the end-code argument can refer to the arguments #1, #2, etc. Nov 10, 2022 at 22:52

1 Answer 1

8

It would be easier to store the parameters at the start of the environment, rather than at the end.

\documentclass{article}
\newcommand{\baz}{}
\makeatletter
\newenvironment{foo}[1]{\g@addto@macro\baz{#1}}{}
\makeatother
\begin{document}
\begin{foo}{kidney}
\end{foo}
\begin{foo}{spleen}
\end{foo}
\begin{foo}{liver}
\end{foo}

\baz
\end{document}

If you need to do it at the end, you might find the solutions to this question useful.

1
  • I can make it work that way. The original problem required I add some information calculated at the end of the environment as well. I have now added the argument information at the beginning, and the calculated information at the end of the environment. Thanks.
    – ipavlic
    Jun 7, 2011 at 0:30

You must log in to answer this question.

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