How to Highlight Parts of a Listing in Beamer
At a glance
- 1. Make the frame fragile
Add the fragile option to any Beamer frame containing a listings environment.
- 2. Mark the code span
Use listings escape delimiters around the code text that should be highlighted.
- 3. Apply an overlay
Put Beamer’s alert command inside the escaped span and choose the slide on which it appears.
To highlight code in a Beamer presentation, distinguish between highlighting the entire listing and emphasizing a particular part of the code. The listings environment reads its contents in a verbatim-like way, so Beamer commands placed directly in the code are not normally interpreted as commands. The reliable approach is to make the frame fragile and use listings escape delimiters to mark a small region as LaTeX.
Why \alert around a listing fails
A construction such as \alert{\begin{lstlisting} ... \end{lstlisting}} is not a dependable way to color a listing. lstlisting reads its contents specially, while \alert expects ordinary LaTeX text. Likewise, typing \alert<2>{...} directly among the code usually prints it as code instead of executing it.
Beamer requires frames containing verbatim-like material to be declared with [fragile]. Then, escapeinside tells listings which delimiters mark LaTeX material. Within such a region, you can use overlay commands such as \alert<2>{...}.
Complete example: highlight code on slide 2
This example shows the same listing on both overlays and alerts the return statement on the second one.
\documentclass{beamer}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily\small,
columns=fullflexible,
keepspaces=true,
escapeinside={(*@}{@*)}
}
\begin{document}
\begin{frame}[fragile]{Highlighting a listing}
\begin{lstlisting}
int main() {
(*@{\alert<2>{\texttt{return 0;}}}@*)
}
\end{lstlisting}
\end{frame}
\end{document}
Compile the document and move between the two overlays: the marked statement is normally colored on the first overlay and alerted on the second. The spaces before the opening delimiter remain part of the listing, so the line stays indented. \texttt keeps the escaped text in a code-like font; \alert supplies Beamer’s alert styling.
Highlighting other text or lines
To emphasize a different token, move the escape delimiters around that token and change the overlay specification. For example, \alert<3>{...} targets overlay 3. You can also use another Beamer command inside the escaped region if it suits the effect you want.
The example deliberately escapes only simple text. Once listings switches to LaTeX inside an escape region, the contents are LaTeX, not literal code: characters such as _, %, #, and & may need escaping, and code formatting options no longer apply to that text. For more complicated code, keep the escaped span short or use a listing method designed for overlay-aware highlighting.
If your goal is to color the entire listing, rather than highlight it on a particular slide, use a listings style or options such as basicstyle and backgroundcolor. Those change the listing’s appearance without involving Beamer overlays.
Common mistakes
- Forgetting
[fragile]: A frame containinglstlistingcan fail to compile or behave incorrectly without it. - Putting overlay syntax directly in the code: Without escape delimiters,
\alert<2>{...}is treated as listing content. - Escaping too much: LaTeX special characters inside an escape region must be handled as LaTeX, not as literal source code.
- Expecting
\alertto highlight a whole verbatim environment: Apply it to a small escaped span, or set the listing’s style if you want a static effect. - Using delimiters that appear in the source: Pick escape delimiters that do not occur in the code you are displaying, or
listingsmay interpret them as the start or end of LaTeX content.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.