newenvironment/newcommand for existing environment in LaTeX-Beamer

I would like to have the solution of highlighting a line in listings-package as a command or environment. To bad I cannot get it working. I tried:

• a simple newenvironment: compile errors
• \newcommand: also compile errors
• the environ-package, see below

\NewEnviron{listingh}{
\vspace{-\baselineskip}
\begin{lstlisting}[backgroundcolor=\color{yellow}]
\BODY
\end{lstlisting}
\vspace{-\baselineskip}
}


Also, same kind of errors. missing {, missing number, threaded a zero etc.

I try to use it with the beamer-package and pdflatex.

-

migrated from stackoverflow.comApr 26 '11 at 12:03

This question came from our site for professional and enthusiast programmers.

Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Jan 15 '14 at 23:37

The lstlisting environment is like all verbatim environments special and doesn't work (well) inside another environments. You need to use \lstnewenvironment as described in the listings manual in section 4.16 Environments, p.40:

\lstnewenvironment
{<name>}[<number>][<opt. default arg.>]
{<starting code>}
{<ending code>}


Applied to your code:

\lstnewenvironment{listingh}[1][]{%
\vspace{-\baselineskip}%
\lstset{backgroundcolor=\color{yellow},#1}%
}{%
\vspace{-\baselineskip}%
}


Note the % at the end of lines to avoid spurious spaces.

You need to use the fragile option of the frame environment (or command) if you use listings or any other verbatim code.

-

In addition to Martin's answer, you need to take special care within the beamer class, because that class also drastically alters LaTeX's expansion habits. See section 2.6 of the beamer manual.

\documentclass{beamer}
\usepackage{listings}

\lstnewenvironment{listingh}[1][]{%
\vspace{-\baselineskip}%
\lstset{backgroundcolor=\color{yellow},#1}%
}{%
\vspace{-\baselineskip}%
}

\begin{document}

\defverbatim[colored]\mycode{%
\begin{listingh}[emph={PRINT,GOTO},emphstyle={\color{blue}}]
10 PRINT "HELLO, WORLD!"
20 GOTO 10
\end{listingh} }

\begin{frame}{BASIC}
\mycode
\end{frame}

% or
\begin{frame}[fragile]{C++}
\begin{listingh}[emph={cout,for},emphstyle={\color{blue}}]
for (;;) {
cout << "Hello, world!";
}
\end{listingh}

\end{document}


This looks bad but keep in mind you can redefine \mycode over and over again before each frame.

-
Why not just use the fragile option for the frame? It works fine in my test. –  Martin Scharrer Apr 26 '11 at 13:01
Good point, I've edited. Is there a situation where fragile doesn't work? Or is this workaround in the manual vestigial? –  Matthew Leingang Apr 26 '11 at 13:06
The section 2.6 referenced by you mentions a couple of times: "[...] either add the fragile option to the frame or use the \defverbatim command". So it seems that both are equal. For listings it says "tread it like a verbatim environment and then "When using \defverbatim that contains a colored lstlisting, use the colored option of \defverbatim". It doesn't explicitly state that fragile can or cannot be used, just tells you that colored` is required for colors. –  Martin Scharrer Apr 26 '11 at 13:28