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}
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).

  • Also there should be \newcommand{\code}{\lstinline[<options>]} without argument. – egreg May 21 '14 at 19:22
  • @egreg I've introduced your suggestion. Thanks. – Gonzalo Medina May 21 '14 at 19:25
  • In this way also the alternative syntax \code!<code>! (! any character like for \verb) is allowed, but it's not the only reason. – egreg May 21 '14 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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