0

I know there's an option in expl3, but how can I do the same in LaTeX2e (or TeX)?

\newenvironment*{myEnv}[1][]{}{}
\begin{myEnv}
  I first need to pass this to this environment as an argument.
\end{myEnv}
bp2017
  • 3,756
  • 1
  • 13
  • 33
  • 1
    This is what the environ package does, the body of the environment is then stored in \BODY. –  Nov 17 '19 at 07:31
  • 3
    You can do everything what packages do by copying the contents of these packages in the preamble (sandwiched between \makeatletter and \makeatother, of course). I do not know what defines an "official" package, but environ is definitely a very nice and robust package that gets widely used. The source of the package is not very long, so you can have a look at it. –  Nov 17 '19 at 07:38
  • 2
    Example: \documentclass{article} \usepackage{environ} \NewEnviron{myEnv}[1][]{\underline{\BODY}} \begin{document} \begin{myEnv} I first need to pass this to this environment as an argument. \end{myEnv} \end{document} –  Nov 17 '19 at 07:40
  • @Schrödinger'scat, for some reason my custom macro can't break down this \BODY parameter, but can break down regular text into letters. In other words \BODY doesn't behave like regular text. – bp2017 Nov 17 '19 at 07:51
  • 1
    Well, hard to tell what is going on without seeing your custom macro. You may have to replace \MyCustomMacro{\BODY} by \edef\temp{\noexpand\MyCustomMacro{\BODY}}\temp, but really hard to say. –  Nov 17 '19 at 07:55

1 Answers1

3

The idea of 'collecting the body' is nowadays available in xparse

\documentclass{article}
\usepackage{xparse}
\begin{document}
\NewDocumentEnvironment{myEnv}{O{}+b}{The argument was '#2'}{}
\begin{myEnv}
  I first need to pass this to this environment as an argument.
\end{myEnv}
\end{document}

Here, #2 is the second argument (the body).

As pointed out in comments, the long-standing environ package does the same but provides the result as \BODY.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 1
    @bp2017 You need a more sophisticated loop to deal with spaces: there are several approaches (and examples on the site), depending on the other requirements (working by expansion, treatment of braces, ...). I'll adjust my answer to take \par tokens. – Joseph Wright Nov 17 '19 at 08:18
  • When I pass \BODY to \zStart as \zStart{\BODY} I don't get processed text, it's as if \zStart didn't work. – bp2017 Nov 17 '19 at 08:20
  • @bp2017 Well no, because there is only one token, \BODY. You'll need to expand it once: \expandafter\zStart\expandafter{\BODY}. TeX macros receive the tokens you pass as written, not the expansion of the tokens. I think that was mentioned in comments on the question. – Joseph Wright Nov 17 '19 at 08:23
  • 1
    https://tex.stackexchange.com/questions/464950/iterate-over-tokens for example – Joseph Wright Nov 17 '19 at 08:39