Skip to content

Instantly share code, notes, and snippets.

@qxj
Forked from lmullen/Makefile
Last active December 4, 2016 11:55
Show Gist options
  • Save qxj/ca2a917789cb5b2fa7ddc638ce35063a to your computer and use it in GitHub Desktop.
Save qxj/ca2a917789cb5b2fa7ddc638ce35063a to your computer and use it in GitHub Desktop.
PDF slides and handouts using Pandoc and Beamer

Files to create slides and handouts from the same Markdown files using Pandoc and a custom Beamer template. Lots of ideas borrowed from Karl Browman's post. The Makefile will turn any *.md in the directory into PDF slides (*.md.slides.pdf) and PDF slides with notes for speaking or a handout (*.md.handout.pdf). The file default.beamer belongs in ~/.pandoc/templates. Use the metadata in the YAML header of the Markdown file to control various options. To get the handout to look right, use the \note{} macro after each slide in the Markdown file.

% 12pt by default; handout if the handout template variable is defined
\documentclass[$if(fontsize)$$fontsize$,$else$12pt,$endif$$if(lang)$$babel-lang$,$endif$$if(handout)$handout,$endif$$if(beamer)$ignorenonframetext,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}
\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamercolor{caption name}{fg=normal text.fg}
\beamertemplatenavigationsymbols$if(navigation)$$navigation$$else$empty$endif$
$if(fontfamily)$
\usepackage[$for(fontfamilyoptions)$$fontfamilyoptions$$sep$,$endfor$]{$fontfamily$}
$else$
\usepackage{lmodern}
$endif$
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[$if(fontenc)$$fontenc$$else$T1$endif$]{fontenc}
\usepackage[utf8]{inputenc}
$if(euro)$
\usepackage{eurosym}
$endif$
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
$for(fontfamilies)$
\newfontfamily{$fontfamilies.name$}[$fontfamilies.options$]{$fontfamilies.font$}
$endfor$
$if(euro)$
\newcommand{\euro}{€}
$endif$
% font setting
$if(mainfont)$
\setmainfont[$for(mainfontoptions)$$mainfontoptions$$sep$,$endfor$]{$mainfont$}
$endif$
$if(sansfont)$
\setsansfont[$for(sansfontoptions)$$sansfontoptions$$sep$,$endfor$]{$sansfont$}
$endif$
$if(monofont)$
\setmonofont[Mapping=tex-ansi$if(monofontoptions)$,$for(monofontoptions)$$monofontoptions$$sep$,$endfor$$endif$]{$monofont$}
$endif$
$if(mathfont)$
\setmathfont(Digits,Latin,Greek)[$for(mathfontoptions)$$mathfontoptions$$sep$,$endfor$]{$mathfont$}
$endif$
$if(CJKmainfont)$
\usepackage{xeCJK}
\setCJKmainfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$}
$endif$
\fi
% theme setting
$if(theme)$
\usetheme[$for(themeoptions)$$themeoptions$$sep$,$endfor$]{$theme$}
$endif$
$if(colortheme)$
\usecolortheme{$colortheme$}
$endif$
$if(fonttheme)$
\usefonttheme{$fonttheme$}
$endif$
$if(mainfont)$
\usefonttheme{serif} % use mainfont rather than sansfont for slide text
$endif$
$if(innertheme)$
\useinnertheme{$innertheme$}
$endif$
$if(outertheme)$
\useoutertheme{$outertheme$}
$endif$
% if it is a handout show the notes text; otherwise don't
$if(handout)$
\setbeameroption{show notes}
\setbeamertemplate{note page}[plain]
$else$
\setbeameroption{hide notes}
\setbeamertemplate{note page}[plain]
$endif$
% Don't show things we don't want to see
% \beamertemplatenavigationsymbolsempty
\hypersetup{pdfpagemode=UseNone} % don't show bookmarks on initial view
% Slide number in lower right
\definecolor{gray}{RGB}{155,155,155}
\setbeamertemplate{footline}{%
\raisebox{5pt}{\makebox[\paperwidth]{\hfill\makebox[20pt]{\color{gray}
\scriptsize\insertframenumber}}}\hspace*{5pt}}
% ensure text jusfication
\usepackage{ragged2e}
\justifying
\urlstyle{same}
\usepackage[overlay,absolute]{textpos}
\TPGrid[10 mm,8 mm]{9}{8}
% beamer's left and right margin is 10 mm. The top/bottom margin is ??
% or without a header ??
% the slide dimensions are 128 mm x 96 mm
% so the resulting \TPHorizModule = 12 mm and \TPVertModule = 10 mm
% Uncomment if you want biblatex for citations on slides
% \usepackage{csquotes}
% \usepackage[notes,short,noibid,backend=biber]{biblatex-chicago}
% \bibliography{course.bib}
% pandoc makes 2nd-lever headers into blocks, and this ensures justification
% in blocks too
\addtobeamertemplate{block begin}{}{\justifying}
% Space between paragraphs on notes page
\addtobeamertemplate{note page}{\setlength{\parskip}{12pt}}
% Color and shape of bullets
% \setbeamercolor{item}{fg=gray}
% \setbeamercolor{subitem}{fg=gray}
% \setbeamercolor{itemize/enumerate subbody}{fg=gray}
% \setbeamertemplate{itemize item}{{\textendash}}
% \setbeamertemplate{itemize subitem}{{\textendash}}
% \setbeamerfont{itemize/enumerate subbody}{size=\footnotesize}
% \setbeamerfont{itemize/enumerate subitem}{size=\footnotesize}
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
$if(lang)$
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,$for(babel-otherlangs)$$babel-otherlangs$,$endfor$main=$babel-lang$]{babel}
$if(babel-newcommands)$
$babel-newcommands$
$endif$
\else
\usepackage{polyglossia}
\setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$}
$for(polyglossia-otherlangs)$
\setotherlanguage[$polyglossia-otherlangs.options$]{$polyglossia-otherlangs.name$}
$endfor$
\fi
$endif$
\newif\ifbibliography
$if(natbib)$
\usepackage{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
$endif$
$if(biblatex)$
\usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex}
$for(bibliography)$
\addbibresource{$bibliography$}
$endfor$
$endif$
\hypersetup{
$if(title-meta)$
pdftitle={$title-meta$},
$endif$
$if(author-meta)$
pdfauthor={$author-meta$},
$endif$
$if(keywords)$
pdfkeywords={$for(keywords)$$keywords$$sep$; $endfor$},
$endif$
$if(colorlinks)$
colorlinks=true,
linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,
citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$,
urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,
$else$
pdfborder={0 0 0},
$endif$
breaklinks=true}
$if(listings)$
\usepackage{listings}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
$endif$
$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
\VerbatimFootnotes % allows verbatim text in footnotes
$endif$
$if(tables)$
\usepackage{longtable,booktabs}
\usepackage{caption}
% These lines are needed to make table captions work with longtable:
\makeatletter
\def\fnum@table{\tablename~\thetable}
\makeatother
$endif$
$if(graphics)$
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight0.8\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
$endif$
% Prevent slide breaks in the middle of a paragraph:
\widowpenalties 1 10000
\raggedbottom
% Comment these out if you don't want a slide with just the
% part/section/subsection/subsubsection title:
$if(section-titles)$
\AtBeginPart{
\let\insertpartnumber\relax
\let\partname\relax
\frame{\partpage}
}
\AtBeginSection{
\ifbibliography
\else
\let\insertsectionnumber\relax
\let\sectionname\relax
\frame{\sectionpage}
\fi
}
\AtBeginSubsection{
\let\insertsubsectionnumber\relax
\let\subsectionname\relax
\frame{\subsectionpage}
}
$endif$
$if(links-as-notes)$
% Make links footnotes instead of hotlinks:
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$
$if(strikeout)$
\usepackage[normalem]{ulem}
% avoid problems with \sout in headers with hyperref:
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
$endif$
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
$if(numbersections)$
\setcounter{secnumdepth}{5}
$else$
\setcounter{secnumdepth}{0}
$endif$
$if(dir)$
\ifxetex
% load bidi as late as possible as it modifies e.g. graphicx
$if(latex-dir-rtl)$
\usepackage[RTLdocument]{bidi}
$else$
\usepackage{bidi}
$endif$
\fi
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\TeXXeTstate=1
\newcommand{\RL}[1]{\beginR #1\endR}
\newcommand{\LR}[1]{\beginL #1\endL}
\newenvironment{RTL}{\beginR}{\endR}
\newenvironment{LTR}{\beginL}{\endL}
\fi
$endif$
$for(header-includes)$
$header-includes$
$endfor$
$if(title)$
\title{$title$}
$endif$
$if(subtitle)$
\subtitle{$subtitle$}
$endif$
$if(author)$
\author{$for(author)$$author$$sep$ \and $endfor$}
$endif$
$if(institute)$
\institute{$for(institute)$$institute$$sep$ \and $endfor$}
$endif$
\date{$date$}
\begin{document}
$if(title)$
{
\setbeamertemplate{footline}{} % no page number here
\frame{
\titlepage
% Include a note for the title slide here, because we cannot do so in the
% Pandoc markdown file. If you want the title note to contain actual text,
% use the titlepage-note field in the YAML header. Otherwise, we use a
% non-breaking space so that it is not empty.
\note{$if(titlepage-note)$$titlepage-note$$else$~$endif$}
}
}
%\frame{\titlepage}
$endif$
$for(include-before)$
$include-before$
$endfor$
$if(toc)$
\begin{frame}
\tableofcontents[hideallsubsections]
\end{frame}
$endif$
$body$
$if(natbib)$
$if(bibliography)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\begin{frame}[allowframebreaks]{$biblio-title$}
\bibliographytrue
\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
\end{frame}
$endif$
$endif$
$if(biblatex)$
\begin{frame}[allowframebreaks]{$biblio-title$}
\bibliographytrue
\printbibliography[heading=none]
\end{frame}
$endif$
$for(include-after)$
$include-after$
$endfor$
\end{document}
SLIDES := $(patsubst %.md,%.slides.pdf,$(wildcard *.md))
HANDOUTS := $(patsubst %.md,%.handout.pdf,$(wildcard *.md))
all : $(SLIDES)
%.slides.pdf : %.md
pandoc --latex-engine=xelatex --toc $^ -t beamer --slide-level 2 -o $@
%.handout.pdf : %.md
pandoc $^ -t beamer --slide-level 2 -V handout -o $@
pdfnup $@ --nup 1x2 --no-landscape --keepinfo \
--paper letterpaper --frame true --scale 0.9 \
--suffix "nup"
mv $*.handout-nup.pdf $@
clean :
rm -f $(SLIDES)
rm -f $(HANDOUTS)

author:

  • Lincoln Mullen title: 测试pandoc转LaTeX subtitle: 副标题 titlepage-note: | This is a the note that goes on the title page. This talk is to be given at Doing DH. date: 2016-12-01 theme: Madrid

colortheme: seagull fonttheme: professionalfonts fontsize: 12pt CJKmainfont: PingFang SC mainfont: Gill Sans sansfont: Gill Sans monofont: Menlo ...

第一章节

Slide with text and a note

Regular text size

\tiny Jonathan Sarna, American Judaism (New Haven: Yale University Press, 2014)

\note{ NOTES: This is a note page and you ought to be able to tell. }

Footnote and URL

Surely this is true.^[Jane Doe, Says It Here (New York: Oxford University Press, 2050).]

Here are Google and Facebook

Another long footnode 1

\note{I am sure about this point.}

Insert images

PRML Page 658, Figure 14.1

AdaBoost Pic{ width=80% }

第二章节

Combine Two Sections

This is a heading

Regular text on a slide:

  • One
  • Two
  • Three

\note{ More notes:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. }

Four space rule

  • First paragraph.

    Continued.

  • Second paragraph. With a code block, which must be indented eight spaces:

    { code }
    

代码示例

puts "Hello world."
def my_awesome_variable
  puts "My awesome variable"
end

\note{ This might be easier in R or Ruby. }

Math example

  • Gradient descent for $f(x)$ $$ w_{i+1}=w_i - \eta \frac{\partial f}{\partial w} $$ continued
  • QN method $$ w_{i+1}=w_i - \eta \frac{f'}{f''} $$

第三章节

Incremental List

  • first item
  • second item

Definition lists

Term 1

: Definition 1

Term 2 with inline markup

: 定义

    { some code, part of Definition 2 }

Third paragraph of definition 2.

Variant Tables

Multiline tables


Centered Default Right Left Header Aligned Aligned Aligned


First row 12.0 Example of a row that spans multiple lines.

Second row 5.0 Here's another one. Note the blank line between rows.

Table: Here's the caption. It, too, may span multiple lines.

Grid table

: Sample grid table.

+---------------+---------------+--------------------+ | Fruit | Price | Advantages | +===============+===============+====================+ | Bananas | $1.34 | - built-in wrapper | | | | - bright color | +---------------+---------------+--------------------+ | Oranges | $2.10 | - cures scurvy | | | | - tasty | +---------------+---------------+--------------------+

Footnotes

  1. Long footnode example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment