4

I am trying to construct a command which would automatically redefine an environment of given in both starred and not starred version. The goal is to have \setenviron{foo} redefining environments foo and foo*. I tried to use code from Use the s (star) argument with \NewDocumentEnvironment to work with star arguments, but I can't use variable name of environment there (replacing foo with #1 doesn't work, even after my attempt to write \csname #1* \endcsname instead of \foo* and expand it with \expandafter). What can I do?

I have no idea how \cs_new and \cs_new_eq work and I couldn't find any guides online (expl3 documentation on CTAN doesn't cover these) so I would be grateful for some little explanation of the solution.

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse}

\newenvironment{foo}{}{}

\ExplSyntaxOn
\NewDocumentCommand{\setenviron}{m}{%
\RenewDocumentEnvironment{#1}{s +b}{%
\IfBooleanTF{##1}{do~~ star~ things\par ##2}{##2}%
}{%
\IfBooleanTF{##1}{\par stop~ doing~ star~ things}{}%
}
\cs_new:cpn  {#1*}  {\foo*}       %I have problem with these two lines
\cs_new_eq:cN { end#1*} \endfoo   %
}
\ExplSyntaxOff


\begin{document}

\setenviron{foo}

\begin{foo}
normal foo
\end{foo}

\begin{foo*}
starred foo
\end{foo*}

\end{document}

1 Answer 1

4

An environment called foo* is no different (except by convention) from an environment called fooX, and neither mean that the environment is starred.

An environment called foo* is declared exaclty like you'd declare foo:

\NewDocumentEnvironment{foo*}{+b}
  {Starred: [##1]}
  {** starred end}%

and you use it with:

\begin{foo*}
  <content>
\end{foo*}

An environment foo that takes a * as optional argument is very different. You declare it as foo with an s argument:

\NewDocumentEnvironment{foo}{s +b}
  {\IfBooleanTF{#1}{Starred:}{Not starred:} (#2)}
  {env end}%

and you'd use it (note how the * is after the environment begin):

\begin{foo}*
  <content>
\end{foo}

In your case you just want your command to define two environments, one called #1 and the other called #1*:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse}

\newenvironment{foo}{}{}
\newenvironment{foo*}{}{}

\NewDocumentCommand{\setenviron}{m}{%
  \RenewDocumentEnvironment{#1}{+b}
    {Non starred: (##1)}
    {-- no star end}%
  \RenewDocumentEnvironment{#1*}{+b}
    {Starred: [##1]}
    {** starred end}%
}

\begin{document}

\setenviron{foo}

\begin{foo}
normal foo
\end{foo}

\begin{foo*}
starred foo
\end{foo*}

\end{document}

enter image description here

You must log in to answer this question.

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