5

This question is a follow up to verbatim-like environment with optional arguments poorly behaved. Based on David Carlisle's suggestion, I manually looked for an optional argument and employed \scantokens to retokenize the text when there were no arguments found. The only problem that remains is that there is a linefeed after the \scantoken that I need to eliminate

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

enter image description here

3
  • You only want to re-scan #1 the optional argument don't you? Commented Apr 16, 2013 at 14:10
  • Also you don't want to use a normal \def#1 to look for [` it will fail completely if the first character is { Commented Apr 16, 2013 at 14:12
  • @DavidCarlisle The MWE makes it look like I could avoid the optional argument altogether and just issue the optional argument prior to invoking an argument-free environment. However, all of this stuff is going to get stuffed into a box, and so I was hoping to use the optional argument to pre-condition (e.g. define font size or such things) the subsequent verbatim environment. Commented Apr 16, 2013 at 15:09

1 Answer 1

5

enter image description here

\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}
1
  • 1
    Your facility with arcane TeX structures is most impressive. Thanks so much! Commented Apr 16, 2013 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .