DEV Community

Brandon Rozek
Brandon Rozek

Posted on • Originally published at brandonrozek.com on

Code in LaTex Beamer

I commonly use the listings package to showcase code in my LaTex documents. I tried doing the same in my Beamer slidedecks and I ran into an issue where the LaTex source code failed to compile. After digging around, I figured out its because every slide or frame that includes code (or any verbatim environment) needs to be marked as fragile. A minimal example is presented below:

\documentclass{beamer}
\usepackage[utf8]{inputenc}

% Beamer Packages
\usepackage{harvard}
\usetheme{Copenhagen}

% Code Rendering Packages
\usepackage{listings}
\lstset{
  language=Java,
  columns=flexible,
}

% Begin Slidedeck
\begin{document}
\begin{frame}[fragile]{Code Example}
    \begin{lstlisting}
    int x = 5;
    \end{lstlisting}
\end{frame}
\end{document}

Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay