%--------------------------------------------------------------------
%	PACKAGES AND DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass{beamer}

% add numbers to figures: see https://tex.stackexchange.com/a/127150
\setbeamertemplate{caption}[numbered]

\mode<presentation>{\usetheme{boxes}}

\newcommand{\code}[1]{\mbox{\texttt{#1}}}

% Adjusting margins to personal my need

% Graphics
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{subcaption}
\graphicspath{{figures/}}

% Math
\usepackage{amssymb}
\usepackage{amsmath} % Required for some math elements

% Other
%\usepackage{algorithmic}
\usepackage{array}
%\usepackage[caption=false,font=footnotesize]{subcaption}
\usepackage{lipsum}
\usepackage{hyperref}
\hypersetup{colorlinks=true}

\usepackage{listings}

% lstset adapted from https://tex.stackexchange.com/questions/147512/latex-a-single-code-listing-over-multiple-pages
\lstset{ %
basicstyle=\tiny\ttfamily,       % the size of the fonts that are used for the code
numbers=left,           % where to put the line-numbers
numberstyle=\tiny,  % the size of the fonts that are used for the line-numbers
stepnumber=1,           % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt,          % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,           % show spaces adding particular underscores
showstringspaces=false,     % underline spaces within strings
showtabs=false,         % show tabs within strings adding particular underscores
frame=single,           % adds a frame around the code
tabsize=2,              % sets default tabsize to 2 spaces
captionpos=b,           % sets the caption-position to bottom
breaklines=true,            % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)},      % if you want to add a comment within your code
float=H
}

% Macro to display code excerpts with correct line numbers, and file source labels
\newcommand{\excerpt}[3] {
  \small{\texttt{\detokenize{#1}}:}
  \lstinputlisting[firstline=#2,lastline=#3,firstnumber=#2]{#1}}

%----------------------------------------------------------------------------------------
%	MAIN PART
%----------------------------------------------------------------------------------------

\begin{document}

\title{Terrain2STL Review} % Title
%\author{Thatcher Chamberlin}
\author{Thatcher Chamberlin}
\date{\today} % Date for the report

% have this if you'd like a recurring outline
\AtBeginSection[]  % "Beamer, do the following at the start of every section"
{
\begin{frame}<beamer>
\frametitle{Outline} % make a frame titled "Outline"
\tableofcontents[currentsection]  % show TOC and highlight current section
\end{frame}
}

% this prints title, author etc. info from above
\begin{frame}
\titlepage
\end{frame}

\begin{frame}
  \frametitle{Code Reference}
  Code referenced in these slides is from
  \begin{itemize}
    \item Terrain2STL \ttsbranch, commit \ttshash
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Small Code Block}
  \lstinputlisting[firstline=61,lastline=69]{../Terrain2STL/src/STLWriter.c}
\end{frame}

% make the line number labels match the source code line numbers
\begin{frame}
  \frametitle{Small Code Block - Correct Line Numbers}
  \lstinputlisting[firstline=61,lastline=69,firstnumber=61]{../Terrain2STL/src/STLWriter.c}
  % would be nice to find a way to make firstnumber always equal firstline...
\end{frame}

\begin{frame}
  \frametitle{Small Code Block - Language Label}
  Here I've told Beamer to present these lines as C code
  \lstinputlisting[language=C,firstline=61,lastline=69,firstnumber=61]{../Terrain2STL/src/STLWriter.c}
\end{frame}

\begin{frame}
  \frametitle{Small Code Block - \texttt{linerange} Test}
  Here I'm taking lines 61-63 and 67-69 - note how the line numbers don't reflect the discontinuity! Not ideal for my purposes
  \lstinputlisting[language=C,linerange={61-63,67-69},firstnumber=61]{../Terrain2STL/src/STLWriter.c}
\end{frame}

\begin{frame}
  \frametitle{Large Code Block}
  On this slide I've tried to include too many lines...
  \lstinputlisting[firstline=1,lastline=69]{../Terrain2STL/src/STLWriter.c}
\end{frame}

\begin{frame}[allowframebreaks]
  \frametitle{Large Code Block - Overflow Into Next Slides}
  ... but here I've made them overflow onto subsequenct slides!
  \lstinputlisting[firstline=1,lastline=69]{../Terrain2STL/src/STLWriter.c}
\end{frame}

\begin{frame}
  \frametitle{Added Source Name}
  Finally I defined a macro to encapsulate this code excerpt pattern and add a title
  containing the source file, so my final \texttt{\textbackslash excerpt} macro produces results like:
  \vskip 0.2in
  \excerpt{../Terrain2STL/src/STLWriter.c}{61}{69}
\end{frame}

\end{document}
