2

Am i able to do something like this: (without defining new environment)

\newcommand{\code}[2] { 
    \begin{lstlisting}[language=#1]
        #2
    \end{lstlisting} 
}

I don't want to write:

\begin{code}{python}
    >>> print "Hello World!"
\end{code}

I want to be able to write:

\code{python}{>>> print "Hello World!"}
jub0bs
  • 58,916
  • 5
    Welcome to TeX.SX! Unfortunately it's not possible. For one liners, as you seem to want, listings provides \lstinline. – egreg Jul 29 '14 at 19:45
  • You can wrap around \lstinline without reading the argument: \newcommand*\code[1]{\lstinline[language=#1]}. As @egreg says this only works for short inline code – cgnieder Jul 29 '14 at 19:58
  • What egreg said. lstlisting is like a verbatim environment; it's fragile and can't really be included in the arguments of other commands. This post might help: http://tex.stackexchange.com/questions/59715/package-listings-warning-text-dropped-after-begin-of-listing-when-calling-funct It contains some more egreg wisdom, too. As an aside, try searching the archive before asking a question; you're probably not the first to run into a given problem. – dgoodmaniii Jul 29 '14 at 19:59

0 Answers0