TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

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

share|improve this question

migrated from stackoverflow.com Apr 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:

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

Applied to your code:


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.

share|improve this answer

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.




20 GOTO 10
\end{listingh} }


% or
for (;;) {
    cout << "Hello, world!";


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

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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