8
\$\begingroup\$

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 of tikzmark (and later on for hyperlinks to Web-based documentation).
  • Some diagrams using tikz.
  • Callouts with arrows and nodes, using tikz and tikzmark
    • Note that said arrows point to the middle of the character, not the baseline. That's what all the shifts are for.
    • Some callouts have multiple arrows emanating from them.
    • Most callouts have an anchor of west, 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.
  • 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:

Slide 1

Slide 2

Slide 3

\$\endgroup\$
2
  • \$\begingroup\$ Just a point about the slides themselves. The worst about heap allocation is that the management of the memory is done by yourself. Please make sure that is stated and clear. Allocating and freeing memory from heap itself may be a little bit more expensive than stack memory but is not such a big concern. \$\endgroup\$ Jan 28, 2016 at 23:12
  • \$\begingroup\$ Good point. That is true, I'll clarify that. \$\endgroup\$
    – JesseTG
    Jan 28, 2016 at 23:22

1 Answer 1

6
\$\begingroup\$

In my opinion, the code is reasonably clear. I would recomment couple changes to make it better:

  1. Externalize the C++ code; make it a separate file and load it from file using \inputminted. See the package documentation for details.

  2. Mark the ends of frames, it easies the navigation in the source code. I use this in my code:

    ...
    \end{frame}
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    \begin{frame}
    ...
    
  3. You are inconsistent with indentation: Sometimes it's 4 spaces or a tab, sometimes 2 spaces; sometimes you indent the corresponding \end differently and sometimes you do not.

  4. You are repeating [fill=#1, ultra thick, rounded corners] several times; it could use a TikZ style.

  5. I would avoid \today; put the date of the talk/seminar in \date.

  6. Do you ever use your \cppref? If not, do you need it?

  7. Add comments to everything that deserves them, especially in the preamble. Group related parts of preamble. Do not load hyperref (beamer does that for you). I would change the preamble to this:

    \documentclass[glossy]{beamer}
    
    % FONTS ETC.
    \usepackage[utf8]{inputenc}
    \usepackage{lmodern}
    \usepackage[T1]{fontenc}
    
    % BEAMER APPEARANCE
    \useoutertheme{wuerzburg}
    \useinnertheme[realshadow,corners=2pt,padding=2pt]{chamfered}
    \usecolortheme{shark}
    
    \setbeamertemplate{navigation symbols}{}
    
    % TIKZ
    \usepackage{tikz}
    \usetikzlibrary{tikzmark, arrows, decorations, decorations.pathreplacing}
    \tikzset{every picture/.style={font issue=\scriptsize},
             font issue/.style={execute at begin picture={#1\selectfont}}
    }
    
    
    % MINTED
    \usepackage{minted}
    \newminted{cpp}{autogobble, fontsize=\tiny, escapeinside=@@}
    \newmintinline{cpp}{}
    \usemintedstyle{vs}
    
    % METADATA    
    \title{C++ Boot Camp 1/2}
    \author{Jesse Talavera-Greenberg}
    \date{5th February 2016}
    
    % USER MACROS
    \newcommand{\cppref}[2]{\href{http://en.cppreference.com/w/cpp/#1}{\underline{#2}}}
    
    % BEGIN DOCUMENT
    \begin{document}
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    \begin{frame}
    ....
    
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.