15

Would it be possible to use the s argument specifier of xparse in \NewDocumentEnvironment so that we get an environment an a starred version of it?

Actually it works, but the star goes to the wrong place:

\documentclass{article}

\usepackage{xparse}

\NewDocumentEnvironment{baz}{s O{Foobar}}{%
   \begin{quote}
   \IfBooleanTF{#1}{%
      \textbf{#2 (starred)}\quad
   }{%
      \textbf{#2 (non-starred)}
   }%
}{%
   \end{quote}
}

\usepackage{lipsum}

\begin{document}
\lipsum[1]
\begin{baz}
   \lipsum[2]
\end{baz}
\lipsum[3]
% Star at the wrong place in a LaTeX2e sense
\begin{baz}*[Name]
   \lipsum[2]
\end{baz}
\lipsum[3]
\end{document}

It would be great if \NewDocumentEnvironment{baz}{s ... defines {baz} and {baz*}, otherwise one have to define them separately as in 2e syntax…

2
  • 3
    I have some thoughts on how to tackle this, but they are really 'for discussion' (i.e. they don't make a good Q&A answer). For example, you could imagine definition \begin to check for an optional star (there is a bit of complexity to it). However, with LaTeX2e that means messing with some internals: not sure if it's a great idea. One for LaTeX-L!
    – Joseph Wright
    Commented Feb 22, 2012 at 14:21
  • 3
    It is 2017 and the xparse neither mentions this problem nor does it suggest a possible fix. I see that reading the docs strictly implies the current behavior, but it doesn't seem desirable. Commented Sep 10, 2017 at 18:22

1 Answer 1

12

Just say

\NewDocumentEnvironment{baz}{s O{Foobar}}{%
   \begin{quote}
   \IfBooleanTF{#1}{%
      \textbf{#2 (starred)}\quad
   }{%
      \textbf{#2 (non-starred)}
   }%
}{%
   \end{quote}
}
\ExplSyntaxOn
\cs_new:cpn {baz*} {\baz*}
\cs_new_eq:cN {endbaz*} \endbaz
\ExplSyntaxOff

and now \begin{baz*}...\end{baz*} will do as expected.

I'm not certain that an extension of the syntax in this sense is good: the environment's name is the argument to \begin.

0

You must log in to answer this question.

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