Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

While trying to use listings and beamer, for highlight some parts of the code I found the following: How to make overlay still work inside lstlisting environment?

However I notice that I can't overlay comment in the same line as code. If I place the delimiter in the next line, it works, but gives an undesired extra space that is not removed even with ``emptylines=1'' option.

Is there a way to also highlight the comments that are in the same line as source code? A MWE is the following:



\begin{frame}[fragile]{Listings overlay}

\begin{lstlisting}[style=base, gobble=0]
@% This comment will be highlighted; it is also bigger than the frame size so it is expected that line is breaked into at least two. Note that unwanted empty line may be generated

@a = 2@ % aaa
@b = 1 % aaa @
@c = 1 % aaa @
@c = 1 % aaa



As you can see, comments in code (a=... % comment) is not highlighted. How can I do it, without generating extra lines (@ in next line)?

share|improve this question
The @ after a % is simply not processed by the moredelim option, so the next one is used (which is the reason the first c = 1 line isn’t even highlighted). You could do @@b and @@c with omitting the last @ (to avoid an empty line at the end). –  Qrrbrbirlbel Mar 2 '13 at 4:17
Yes I know. My approach would be: 1 - detect if after '@' there is a blank line; 2 - If true, remove the line, like 'emptylines=1' does. But I have no clue how to implement it. –  cacamailg Mar 2 '13 at 14:35

Your Answer


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

Browse other questions tagged or ask your own question.