Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
You only want to re-scan #1 the optional argument don't you? –  David Carlisle Apr 16 '13 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 { –  David Carlisle Apr 16 '13 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. –  Steven B. Segletes Apr 16 '13 at 15:09

1 Answer 1

up vote 3 down vote accepted

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}
share|improve this answer
1  
Your facility with arcane TeX structures is most impressive. Thanks so much! –  Steven B. Segletes Apr 16 '13 at 15:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.