entorno textual con argumentos opcionales que se comportan mal

entorno textual con argumentos opcionales que se comportan mal

[EDITADO para mostrar dónde realmente quiero usar el argumento opcional]

Esta pregunta puede ser respondida en¿Cómo pasar un argumento opcional a un entorno con contenido textual?, pero me cuesta mucho intentar aplicar esa respuesta a mi problema. Eliminaré esta consulta si se considera duplicada.

Cuando utilizo un entorno similar textualmente que toma un argumento opcional (vb en el MWE a continuación), en los casos en los que no se especifica ningún argumento opcional, el primer token en el entorno a veces se ejecuta fuera del entorno.

\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}

ingrese la descripción de la imagen aquí

Respuesta1

Como ocurre con todos los comandos textuales, debe cambiar el régimen del código cat antes de analizar los argumentos. Una vez que un carácter ha sido tokenizado, los valores de catcode establecidos textualmente no tienen ningún efecto ya que los catcodes no afectan a los tokens una vez creados, solo determinan cómo se crean los tokens a partir de la entrada del archivo.

En

\begin{vb}\Huge

\vbmira el siguientesimbólicopara ver si lo es [. TeX tiene que leer el archivo para generar el siguiente token, por lo que lee todo \Hugey crea un token cs, después de lo cual cualquier configuración de catcode solo afecta los caracteres que se leen del archivo; no afectan el Huge.

Sólo necesita analizar los argumentos después de configurar 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
}

información relacionada