4

Why does the MWE below compile with errors only if I use ths \lstnewenvironment line? And how to define the verbatim macro inside an environment? I heard it's possible using plain TeX?

\documentclass{article}
\makeatletter
\IfFileExists{randompackageyoudonthave.sty}{
}{
\usepackage{listings}
\lstnewenvironment{Rcode}{\renewcommand\lstlistingname{Rcode}}% deleting this line makes this work
\newcommand{\code}[1]{\lstinline{##1}}
}
\makeatother
\begin{document}
\code{yeah! some code}
\end{document}
1010011010
  • 6,357

1 Answers1

6

You were missing the end part of the environment; the syntax (without arguments) is

\lstnewenvironment{<name>}{<begin-env>}{<end-env>}

Your example code:

\documentclass{article}

\IfFileExists{randompackageyoudonthave.sty}{
}{
\usepackage{listings}
\lstnewenvironment{Rcode}{\renewcommand\lstlistingname{Rcode}}{}
\newcommand{\code}{\lstinline}
}

\begin{document}
\code{yeah! some code}

\code+yeah! some code+
\begin{Rcode}
yeah! some code
\end{Rcode}
\end{document}

Also, as egreg mentioned in his comment, it should be

\newcommand{\code}{\lstinline}

or

\newcommand{\code}{\lstinline[<options>]}

if some [<options>] should be used. This allows for the use of alternative delimiters, as in \code+text+ (see the example code above).

Gonzalo Medina
  • 505,128