这个问题是带有可选参数的逐字环境表现不佳。根据 David Carlisle 的建议,我手动查找可选参数,并\scantokens
在未找到参数时使用该参数重新标记文本。剩下的唯一问题是,\scantoken
我需要删除后面的换行符
\documentclass{article}
\usepackage{verbatim}
\usepackage{ifthen} \normalsize
\parskip 1ex\parindent 0em
% SEEMS LIKE IT SHOULD WORK, BUT LACK OF OPTIONAL ARGUMENT MAKES 1ST
% ITEM OF ENVIRONMENT GET EXECUTED AS IF IT WERE OPTIONAL ARGUMENT
%\newenvironment{jenv}[1][]{#1\verbatim}{\endverbatim}
% THIS ALMOST FIXES IT, BUT PUTS LINEFEED AFTER \scantokens
\def\jin#1#2#3{\ifthenelse{\equal{#1}{[}}%
{#2\verbatim}%
{\verbatim\scantokens{#1#2#3}}%
}
\newenvironment{jenv}{\jin}{\endverbatim}
\begin{document}
\normalsize
\begin{jenv}[\LARGE]
this is a test
\end{jenv}
\normalsize
\begin{jenv}[\LARGE]
\tiny this is a test
\end{jenv}
\normalsize
\begin{jenv}
this is a test
\end{jenv}
\normalsize
\begin{jenv}
\tiny this is a test
\end{jenv}
\end{document}
答案1
\documentclass{article}
\usepackage{verbatim}
\usepackage{ifthen} \normalsize
\parskip 1ex\parindent 0em
% SEEMS LIKE IT SHOULD WORK, BUT LACK OF OPTIONAL ARGUMENT MAKES 1ST
% ITEM OF ENVIRONMENT GET EXECUTED AS IF IT WERE OPTIONAL ARGUMENT
\newenvironment{jenv}{\verbatim\jenvinner}{\endverbatim}
\makeatletter
\newcommand\jenvinner[1][]{{\nfss@catcodes\scantokens{\gdef\tmp{#1}}}\tmp}
\makeatother
\begin{document}
\normalsize
\begin{jenv}[\LARGE]
this is a test
\end{jenv}
\normalsize
\begin{jenv}[\LARGE]
\tiny this is a test
\end{jenv}
\normalsize
\begin{jenv}
this is a test
\end{jenv}
\normalsize
\begin{jenv}
\tiny this is a test
\end{jenv}
\end{document}