# Replace lstlisting with verbatim depending on a boolean

I use latex2rtf to convert latex documents to .rtf and thence to MS-word (see this thread). To do this, I set up a custom class and limit which packages are used as latex2rtf only accepts a limited subset of commands (see manual pages). latex2rtf provides a booelan, \iflatextortf, that is set to true if you use latex2rtf as your compiler.

I now need to display code listings and maintain the latex2rtf functionality. That means, I want to define something in the preamble to replace calls to \lstlisting{} with something like \begin{verbatim} ... \end{verbatim} when \iflatextortf is true.

Question: Is it possible to use switch between the lstlisting and verbatim environment depending on the value of \iflatextortf, keeping the content of the environment?

The solution would not require any packages to be installed and should work with the limited subset of commands in latex2rtf (see section 8.6.1). This should help me run compile using latex2rtf. Solutions using the verbatim package won't work unfortunately.

My ideal solution looks like this:

\newif\iflatextortf

\iflatextortf
\documentclass[12pt,letterpaper]{report}
% whatever my replacement for lstlistings is %
\else
\documentclass[10pt,letterpaper]{report}
\usepackage{listings}
% something fancy with listings
\lstnewenvironment{codeenv}[1][]{\lstset{basicstyle=\small\ttfamily}#1}{}
\fi


And I would like something a little more elegant than using find & replace on the raw .tex!

-
Best I can tell this is impossible without the verbatim or fancyvrb package, as my ideal solution requires me to use verbatim within another environment. –  Andy Clifton Jul 18 '13 at 1:54

My suggestion is to allow loading both listings and verbatim and use a common (new) environment to hold all your code. Under listings, you can define this environment using

\lstnewenvironment{codeenv}[1][]{}{}%


Using verbatim you can define this environment using

\newenvironment{codeenv}[1][]{\verbatim}{\endverbatim}%


Now you're able to condition on whether listings should be loaded using a traditional \@ifundefined{lstlisting}{<undefined>}{<defined>}. Here's a use case:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{verbatim}% http://ctan.org/pkg/verbatim

\makeatletter
\@ifundefined{lstlisting}{% If listings is loaded
\newenvironment{codeenv}[1][]{\verbatim}{\endverbatim}%
}{% listings is not loaded
\lstnewenvironment{codeenv}[1][]{\lstset{basicstyle=\ttfamily,#1}}{}%
}
\makeatother

\begin{document}

\begin{codeenv}[basicstyle=\ttfamily]
This is some verbatim text.
\end{codeenv}

\end{document}


Now all you have to do is wrap the

\usepackage{listings}% http://ctan.org/pkg/listings


in your "boolean switch".

If the verbatim package is not allowed, then some code can be taken from the LaTeX kernel the revolves around the definition of the verbatim environment:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings

\makeatletter
\begingroup \catcode |=0 \catcode [= 1
\catcode]=2 \catcode \{=12 \catcode \}=12
\catcode\\=12 |gdef|@xverbatim#1\end{codeenv}[#1|end[codeenv]]
|endgroup
\@ifundefined{lstlisting}{
\newcommand{\codeenv}[1][]{\@verbatim \frenchspacing\@vobeyspaces \@xverbatim}
\def\endcodeenv{\if@newlist \leavevmode\fi\endtrivlist}
}{%
\lstnewenvironment{codeenv}[1][]{\lstset{basicstyle=\ttfamily,#1}}{}%
}
\makeatother

\begin{document}

\begin{codeenv}[basicstyle=\ttfamily]
This is some verbatim text.
\end{codeenv}

\end{document}


A final, fairly elementary approach might be to use the verbatim environment throughout your document for code examples, and redefine this environment to work as a regular listing is listings is loaded:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings

\makeatletter
\@ifundefined{lstlisting}{}{%
\let\verbatim\relax%
\lstnewenvironment{verbatim}{\lstset{basicstyle=\ttfamily}}{}%
}
\makeatother

\begin{document}

\begin{verbatim}
This is some verbatim text.
\end{verbatim}

\end{document}


Since listings does not provide \lstrenewenvironment, \letting \verbatim to \relax frees up the verbatim environment for redefinition. For ease-of-use, it's best avoid making verbatim accept optional arguments.

-
This would be great except I made a mistake in my post and should have emphasized I meant the verbatim environment, not the package. The version of the listing that I use with latex2rtf should be generated using as few packages as possible (preferably none) to make it compatible with latex2rtf. Unfortunately verbatim is not compatible. –  Andy Clifton Jul 17 '13 at 4:29
@LostBrit: I've updated the code to remove the verbatim package requirement. –  Werner Jul 17 '13 at 5:07
Apparently \catcode doesn't work with Latex2rtf either (see documentation). Latex2rtf includes the boolean, \iflatextortf. Ideally, I would just define codeenv as you suggest, and replace lstlisting with verbatim if \iflatextortf were true , but AFAIK, you can't use verbatim as the input to an environment. –  Andy Clifton Jul 17 '13 at 21:01
@LostBrit: I've dumbed it down even further. See if the last addition works for you. –  Werner Jul 18 '13 at 5:24
This works really well. Accepted, thank you! –  Andy Clifton Jul 19 '13 at 1:26