xparse argspec を使用して \verb のような動作を実現するにはどうすればよいですか?

xparse argspec を使用して \verb のような動作を実現するにはどうすればよいですか?

以前にスキャンしたトークンを argspec の引数として使用するにはどうすればよいですu{}xparse?

\documentclass{article}
\usepackage{xparse}

\NewDocumentEnvironment {some-cool-env} {D<>{\stopscan} u{#1}} {%
  Here is the text up until the stopping token: ``#2'' \newline
  Here's the rest of the environment:                  \par
} {}

\begin{document}
\begin{some-cool-env}
  I am a\stopscan teapot
\end{some-cool-env}

(some inner text)

\begin{some-cool-env}<|>
  A custom built|teapot.
\end{some-cool-env}
\end{document}

それは

Here is the text up until the stopping token: "I am a"
Here's the rest of the environment:

teapot

(some inner text)

Here is the text up until the stopping token: "A custom built"
Here's the rest of the environment:

teapot.

希望する構文を実現するにはどうすればよいでしょうか? (xparseの内部を壊さずにできれば…)

上記の単純なアプローチでは、次のエラーが発生します。

ERROR: Illegal parameter number in definition of \__xparse_grab_arg:w.

--- TeX said ---
<to be read again> 
                   1
l.9 \DumbVerb
             <|>test|
--- HELP ---
This is probably caused by a \newcommand, \renewcommand,
\newenvironment, or \renewenvironment command in which a # is used
incorrectly.  A # character, except as part of the command name \#,
can be used only to indicate an argument parameter, as in #2, which
denotes the second argument. This error is also caused by nesting one
of the above four commands inside another, or by putting a parameter
like #2 in the last argument of a \newenvironment or \renewenvironment
command.

@egreg の回答以来、質問の本文をかなり編集しましたが、質問のタイトルは同じままです。@egreg が回答した質問は、 で定義された単一のコマンドに関するものであったことに注意してください。xparseそのような場合、彼の回答は機能しますが、残念ながら、同様の環境の定義にすぐに一般化することはできません。(まあ、それは真実ではありません。補助環境を定義することはできますが、それでも不正行為のように感じます…)

答え1

xparseこれはおそらく無理があるかもしれませんが、うまくmu{#1}いくようなので、2 段階で実行できます。

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\DumbVerb{D<>{|}}{\DumbVerbAux{#1}}
\NewDocumentCommand\DumbVerbAux{mu{#1}}{Here's #2}
\begin{document}
\DumbVerb test|

\DumbVerb<x>testx

\DumbVerb<\xyz>test\xyz
\end{document}

答え2

egregからの回答おそらく現時点では最善のアプローチですが、望ましいアプローチが失敗する理由という観点からこれを検討したいと思います。これには基本的に 2 つの部分があります。

技術的なレベルでは、引数が以前に取得した情報を使用するようにすることは(おそらく)実行可能ですが、完全に一般的に行うのは非常に面倒です。9つの引数を持つ関数を想像してください。ここでは、8つの以前に取得した引数を許可する必要があります。egregの回答のように専用の補助関数を使用すると、この複雑さを回避できますが、このために基礎。

概念レベルでは、xparse「LaTeX2eのような」、かつ「一貫性のある」ドキュメントレベルのインターフェースを作成することが意図されています。ここでの要求の問題は、インターフェースがない一貫性: コマンドが使用されるたびに、異なるマーカーをスキャンする必要があります。理想的には、このようなアプローチはドキュメントに出現すべきではなく、特に定期的に出現すべきではないため、これをサポートする必要性はxparseまったく明らかではありません。

関連情報