
[オプション引数を実際に使用したい場所を示すために編集しました]
この質問への回答は逐語的なコンテンツを含む環境にオプションの引数を渡すにはどうすればよいでしょうか?しかし、その回答を自分の問題に適用するのは困難です。重複していると判断された場合、この質問は削除されます。
オプション引数 (以下の MWE では vb) を取る verbatim のような環境を利用する場合、オプション引数が指定されていない場合、環境内の最初のトークンが環境外で実行されることがあります。
\documentclass{article}
\usepackage{verbatim}
\parskip 1ex\parindent 0em
\makeatletter
\newenvironment{va}{%
\def\verbatim@processline{%
{\setbox0=\hbox{\the\verbatim@line}%
\hsize=\wd0 \the\verbatim@line\par}}%
\setbox0=\vbox\bgroup \verbatim
}
{%
\endverbatim
\unskip\setbox0=\lastbox %
\egroup
\usebox0
}
\newenvironment{vb}[1][]{%
\def\verbatim@processline{%
{\setbox0=\hbox{\the\verbatim@line}%
\hsize=\wd0 \the\verbatim@line\par}}%
\setbox0=\vbox\bgroup #1 \verbatim
}
{%
\endverbatim
\unskip\setbox0=\lastbox %
\egroup
\usebox0
}
\makeatother
\begin{document}
I created two environments based on \verb|boxedverbatim| environment.
Environment \verb|va| takes no arguments. Environment \verb|vb| is
identical but takes an optional argument (which is not actually used for
anything in this MWE). In all the following cases, no optional argument
is actually passed to the \verb|vb| environment
Starting either environment with a letter works:
\begin{va}I will set \def\x{1}\end{va}
\begin{vb}I will set \def\x{1}\end{vb}
But if I start the environments with a command like \verb|\Huge|, the
\verb|vb| environment executes that command outside the environment, even
though it was not in brackets:
\begin{va}\Huge I will set \def\x{1}\end{va}
\begin{vb}\Huge I will set \def\x{1}\end{vb}
If I start the verbatim with a \verb|\def|, the \verb|vb|
environment breaks
\begin{va}\def\x{1}\end{va}
%\begin{vb}\def\x{1}\end{vb}
\end{document}
答え1
すべての verbatim コマンドと同様に、引数を解析する前に catcode 方式を切り替える必要があります。文字がトークン化されると、verbatim によって設定された catcode 値は効果がありません。catcode は、作成されたトークンには影響せず、ファイル入力からトークンを作成する方法を決定するだけだからです。
で
\begin{vb}\Huge
\vb
次のものを見るトークンかどうかを確認します[
。TeX は次のトークンを生成するためにファイルを読み取る必要があるため、 をすべて読み取り\Huge
、 cs トークンを作成します。その後、 catcode の設定はファイルから読み取られる文字にのみ影響し、 には影響しません Huge
。
catcodes を設定した後は、引数を解析するだけです。
\newcommand\innervb[1][]{}
\newenvironment{vb}{%
\def\verbatim@processline{%
{\setbox0=\hbox{\the\verbatim@line}%
\hsize=\wd0 \the\verbatim@line\par}}%
\setbox0=\vbox\bgroup \verbatim\innervb
}
{%
\endverbatim
\unskip\setbox0=\lastbox %
\egroup
\usebox0
}