I'm giving a presentation in a few days. I'm preparing my material in LaTeX; however, I am relatively new to it. How can I make my LaTeX code more concise and less repetitive?
Here is a reduced version of my presentation, which contains the following features that I'm seeking to improve and tighten up:
- Code samples using
minted
, with lots of embedded escapes for use oftikzmark
(and later on for hyperlinks to Web-based documentation). - Some diagrams using
tikz
. - Callouts with arrows and nodes, using
tikz
andtikzmark
- Note that said arrows point to the middle of the character, not the baseline. That's what all the
shift
s are for. - Some callouts have multiple arrows emanating from them.
- Most callouts have an
anchor
ofwest
, although in this sample very few of them do. - Occasionally, there's also text on the arrow itself.
- Callouts may have text that extends to more than one line.
- Note that said arrows point to the middle of the character, not the baseline. That's what all the
- Columns, a little bit.
The code (with representative slides) follows, as does the output. The vast majority of the slides (which are available on GitHub if you're so inclined) most closely resemble the third example here.
I also wouldn't mind any suggestions for LaTeX packages or the actual content of the presentation, even though they're not the purpose of this question.
\documentclass[glossy]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{minted}
\usepackage{tikz}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\useoutertheme{wuerzburg}
\useinnertheme[realshadow,corners=2pt,padding=2pt]{chamfered}
\usecolortheme{shark}
\setbeamertemplate{navigation symbols}{}
\usetikzlibrary{tikzmark, arrows, decorations, decorations.pathreplacing}
\newminted{cpp}{autogobble, fontsize=\tiny, escapeinside=@@}
\newmintinline{cpp}{}
\usemintedstyle{vs}
\tikzset{every picture/.style={font issue=\scriptsize},
font issue/.style={execute at begin picture={#1\selectfont}}
}
\title{C++ Boot Camp 1/2}
\author{Jesse Talavera-Greenberg}
\date{\today}
\begin{document}
\newcommand{\cppref}[2]{\href{http://en.cppreference.com/w/cpp/#1}{\underline{#2}}}
\begin{frame}
\maketitle
\end{frame}
\begin{frame}[fragile=singleslide]
\frametitle{Memory Model and Lifetime (cont'd)}
\begin{columns}
\begin{column}{6cm}
\begin{itemize}
\item Stack allocation is fast, but size must be known at compile time
\item Heap allocation is flexible, but slow
\item Details vary by compiler, OS, and hardware
\item \textbf{All objects of a given type are the same size.}
\end{itemize}
\end{column}
\begin{column}{6cm}
\begin{tikzpicture}
\draw [fill=pink, ultra thick, rounded corners] (current page.north west) rectangle (6cm, 2cm);
\draw [fill=purple, ultra thick, rounded corners] (0cm, 2cm) rectangle (6cm, 4cm) node [align=center, anchor=center, fill=white] at (3cm, 3cm) {\huge{Stack}};
\draw [fill=green, ultra thick, rounded corners] (current page.north west) rectangle (6cm, 6cm);
\draw [fill=olive, ultra thick, rounded corners] (2cm, 9cm) rectangle +(1cm, 0.5cm) (3cm, 8cm) rectangle +(1cm, 0.5cm) (5cm, 8cm) rectangle +(1cm, 0.5cm) (0, 7cm) rectangle +(3cm, 0.5cm);
\draw node [align=center, anchor=center, fill=white, rounded corners] at (3cm, 8cm) {\huge{Heap}};
\end{tikzpicture}
\end{column}
\end{columns}
\begin{tikzpicture}[remember picture, ->, >=stealth, overlay, red, ultra thick, align=left]
\draw (4cm, 22em) node [anchor=east] {\shortstack{Find enough space (expensive)}} -> (6.25cm, 5.25cm);
\draw (4cm, 3em) node [anchor=east] {\shortstack{Increment an address (cheap)}} -> (6.25cm, 1cm);
\end{tikzpicture}
\end{frame}
\begin{frame}[fragile=singleslide]
\frametitle{(Con|De)structors, RAII, and the Rule of 3}
\begin{cppcode}
#include <cstdint>
#include <cstring>
using std::memcpy;
using std::size_t;
class FloatArray@\tikzmark{raii_dont}@ {
public:
FloatArray(size_t size) : _size@\tikzmark{raii_init}@(size), _array(new float[size]) {}@\tikzmark{raii_ctor}@
FloatArray(const FloatArray& other) : _size(other._size), _array(new float[other._size]) {@\tikzmark{raii_copyctor}@
memcpy(_array, other._array, other._size * sizeof@\tikzmark{raii_sizeof}@(float));
}
FloatArray& operator=(const FloatArray& other) {@\tikzmark{raii_copyeq}@
if (this != &other) { // Watch for self-assignment!
float* temp = new float[other._size];
memcpy(temp, other._array, other._size * sizeof(float));
delete[] _array;
_array = temp;
return *this;
}
}
~FloatArray() {@\tikzmark{raii_dtor}@
delete[] floats;@\tikzmark{raii_dtor_b}@
}
private:
size_t _size;
float* _array;
};
\end{cppcode}
\begin{tikzpicture}[remember picture, ->, >=stealth, overlay, red, ultra thick, align=left]
\draw (3cm, 27em) node [anchor=west] {\shortstack{Don't write this class\\(STL does it better)}} -> ([shift={(0em,.25em)}]pic cs:raii_dont);
\draw (5cm, 23em) node [anchor=south] {\shortstack{Member initialization syntax}} -> ([shift={(0em,.25em)}]pic cs:raii_init);
\draw (8cm, 22em) node [anchor=south west] {\shortstack{Anything else\\(nothing right now)}} -> ([shift={(0em,.25em)}]pic cs:raii_ctor);
\draw (9cm, 7em) node [anchor=north] {\shortstack{Rule of 3: You need to write one,\\you need to write them all}} -> ([shift={(0em,.25em)}]pic cs:raii_copyctor) node [pos=.5, above, sloped, anchor=north] {Copy constructor};
\draw (9cm, 7em) -> ([shift={(0em,.25em)}]pic cs:raii_dtor) node [pos=.5, above, sloped, anchor=north] {Destructor};
\draw (9cm, 7em) -> ([shift={(0em,.25em)}]pic cs:raii_copyeq) node [pos=.5, above, sloped, anchor=north] {Copy assignment};
\draw (4.5cm, 3em) node [anchor=north] {\shortstack{RAII: Create in ctor, delete in dtor}} -> ([shift={(0em,.25em)}]pic cs:raii_dtor_b);
\end{tikzpicture}
\end{frame}
\end{document}
These are the resulting three images: