~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Code alongside Output in LaTex

Published on

Updated on

2 minute reading time

Working on a tool paper, I had the desire to share code alongside its output. With a combination of the listings, caption, and color packages, we can get a result that’s nice looking.

The imports needed

\usepackage{color}
\usepackage{listings}
\usepackage{caption}

I want a think topbar that labels which column is the code section and which column is the output section.

\DeclareCaptionFormat{listing}
    {\colorbox[cmyk]{0.73, 0.35, 0.15,.5}
    {\parbox{\textwidth}{#1#2#3}}}
\DeclareCaptionFont{white}{\color{white}}
\captionsetup[lstlisting]{
    format=listing,
    labelfont=white,
    textfont=white,
    font={bf,footnotesize}
}

Next I define some colors that I would like to use as the theme for the code output:

\definecolor{keyblue}{rgb}{0.1, 0.1, 0.6}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{stringcol}{rgb}{0.58,0.4,0.1}

Setup for the code output

\lstset{
  language=Python,
  columns=flexible,
  basicstyle={\small\ttfamily},
  keywordstyle=\color{keyblue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{stringcol},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=4
}

Then inside the document, you can use minipage to setup a two column layout.

% Column 1: Code
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[title=Code]
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)
# Let's test it out!
print(factorial(5))
\end{lstlisting}
\end{minipage}
% Column 2: Output
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[title=Output]
    120
\end{lstlisting}
\end{minipage}

Below is all these steps combined into a compile-able LaTex file.

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

% Imports
\usepackage{color}
\usepackage{listings}
\usepackage{caption}

% Define colors
\definecolor{keyblue}{rgb}{0.1, 0.1, 0.6}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{stringcol}{rgb}{0.58,0.4,0.1}

% Caption Setup
\DeclareCaptionFormat{listing}
    {\colorbox[cmyk]{0.73, 0.35, 0.15,.5}
    {\parbox{\textwidth}{#1#2#3}}}
\DeclareCaptionFont{white}{\color{white}}
\captionsetup[lstlisting]{
    format=listing,
    labelfont=white,
    textfont=white,
    font={bf,footnotesize}
}

% Code listing setup
\lstset{
  language=Python,
  columns=flexible,
  basicstyle={\small\ttfamily},
  keywordstyle=\color{keyblue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{stringcol},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=4
}


\begin{document}

% Column 1: Code
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[title=Code]
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)
# Let's test it out!
print(factorial(5))
\end{lstlisting}
\end{minipage}
% Column 2: Output
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[title=Output]
    120
\end{lstlisting}
\end{minipage}

\end{document}
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :