When using xparse
to define a new environment with an optional argument O{}
, the first end of line character is gobbled as can be seen in the following example document:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new_protected:Npn \__my_end_of_line: { X }
\NewDocumentEnvironment{mycode}{ !O{} }{
\char_set_catcode_active:N \^^M
\char_set_active_eq:nN {`\^^M} \__my_end_of_line:
}{}
\ExplSyntaxOff
\begin{document}
\begin{mycode}
abc
\end{mycode}
\end{document}
which outputs
abcX
where I would expect
XabcX
There already is this question which is similar, but doesn't make use of active characters. Also the comments in that question don't help here. Neither is prefixing the argument specifier with !
working nor is it working in a newer xparse
version (my version is xparse 2019-05-28
).
Is there a way to work around this problem?
EDIT: My use case is to have a special verbatim-like environment with some optional setup at the beginning. The problem is that I need to read ahead until I find the end of the first line/the first ^^M
to discard anything that is on that line (it's usually empty). This works fine if an optional argument is present, but without an optional argument the first actual line of code is considered the line to be discarded, which is unwanted.
[
occurs, change it back for the option processing, and afterwards set them active again, which seems quite messy.\NewDocumentEnvironment { foo } { } { \group_begin: \char_set_catcode_active:N \^^M \peek_meaning_ignore_spaces:NTF [ { \group_end: \parse_opt_arg:w } { \group_end: \dont_parse_opt_arg:w }
, doesn't seem that messy.