![xparse argspec を使用して \verb のような動作を実現するにはどうすればよいですか?](https://rvso.com/image/281594/xparse%20argspec%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%20%5Cverb%20%E3%81%AE%E3%82%88%E3%81%86%E3%81%AA%E5%8B%95%E4%BD%9C%E3%82%92%E5%AE%9F%E7%8F%BE%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%99%E3%81%8B%3F.png)
以前にスキャンしたトークンを 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
まったく明らかではありません。