website/content/blog/codeoutputlatex.md
2022-01-29 20:20:13 -05:00

3.1 KiB

title date draft tags math
Code alongside Output in LaTex 2022-01-27T09:15:10-05:00 false
LaTex
false

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}