逐字环境中的可选参数

逐字环境中的可选参数

这个问题是带有可选参数的逐字环境表现不佳。根据 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}

相关内容