讲义

演讲结束,有人说「幻灯片发我一份」,你顺手把刚才投影的文件转了过去。对方打开的是一份 120 页的 PDF,同一张图重复出现四次——用 \pause 逐步显现的图,在屏幕上是分步展示,落到纸面上就只是重复。LaTeX 的 beamer 里,有一个 class 选项正是为这个问题而生handout。它把所有 overlay 折叠起来,让一个 frame 重新变回一页——实测中,两个各含三级 overlay 的 frame 组成的幻灯片,从 6 页变成了 2 页。本页讲的就是 handout、用 \note 写的讲者备注、用 pgfpages 把四张幻灯片排进一页的方法,以及另一条完全不同的路:把演讲内容当作文档发出去。

\documentclass[handout]{beamer}:把 overlay 折叠掉

只需在 class 选项里加一个词。写 \documentclass[handout,aspectratio=169]{beamer}\pause\item<2-> 就都不再起作用,每个 frame 恰好变成一页。来实测:一份两帧的幻灯片——一帧含两个 \pause,另一帧是带 \item<1-><2-><3-> 的列表——普通模式下是 6 页,加上 handout 后是 2 页。页面尺寸不变(两者都是 362.835 × 272.126 pt)。内容一个字都不用改,就能从同一份 .tex 得到两份 PDF。实务上最不容易出事的做法,是另写一个三行的文件,用 handout 设定 class,然后 \input 正文。

这里有个不知道就会毁掉讲义的坑。handout 并不是把 overlay 折叠成「最终状态」,而是把所有步骤的内容叠着一起印出来。 实际把含 \only<1>{ALPHA}\only<2>{BRAVO} 的 frame 用 handout 编译,出来的那一页上 ALPHA 和 BRAVO 都在。也就是说,用 \only 替换公式某一部分的常见写法,在讲义里会把替换前后并排印出来。解决办法是在 overlay 指定里写上模式名\only<2| handout:1>{...} 表示「演讲时在第 2 帧,讲义里在第 1 帧」,也就是照常显示;\item<2-| handout:0> 表示「讲义里不出现」。整帧切换也一样:\begin{frame}<handout:0> 把这一帧从讲义里去掉,\begin{frame}<beamer:0> 则让它只出现在讲义里——实测中,同时用了这两者的三帧幻灯片,在两种模式下各输出 2 页,而且内容互不相同。

latex
\documentclass[handout,aspectratio=169]{beamer}

% WRONG in a handout: both versions of the formula are printed
\only<1>{$x^2 + y^2$}
\only<2>{$x^2 + y^2 = r^2$}

% RIGHT: say what each mode should do
\only<1| handout:0>{$x^2 + y^2$}   % talk only
\only<2| handout:1>{$x^2 + y^2 = r^2$}  % talk step 2, handout step 1

% frame-level switches
\begin{frame}<handout:0>{Live demo}   % not in the handout
\begin{frame}<beamer:0>{Full derivation}  % handout only

\note{...} 默认什么也不输出:需要 \setbeameroption{show notes}

在 frame 里写上 \note{这里提一句经费},默认情况下 PDF 完全没有变化。实测:给三帧中的两帧加了 \note,输出仍是三页。这是设计使然,beamer 只是把备注存了起来。要取出来,就在导言区声明 \setbeameroption{...}。在同一份三帧幻灯片上测量:show notes 输出 5 页(三张幻灯片,加上写了备注的那两帧后面各插一张专用页),show only notes 输出 2 页(只有备注),show only slides with notes 输出 4 页(只有带备注的帧及其备注)。在 show notes 之后再写 hide notes 就回到三页——临上台前切换这一行,比到处注释代码安全得多。

写在导言区的一行输出内容三帧幻灯片的实测
(none)只有幻灯片;\note 被存起来但不输出3 页
\setbeameroption{show notes}幻灯片,其后各插入一张专用备注页5 页
\setbeameroption{show only notes}只有备注页,成为可以照着读的讲稿2 页
\setbeameroption{show only slides with notes}只有写了备注的帧及其备注4 页
\setbeameroption{hide notes}取消前面的 show notes;上台前切换用3 页
\setbeameroption{show notes on second screen=right}页数不变,每页横向展开为两个屏幕3 页,725.669 × 272.126 pt

也值得看看备注页里有什么。show notes 生成的页面顶部有该幻灯片的缩略图和日期,下面才是正文。连着写几个 \note{...} 会形成多个段落;想做成条目,就用 \note[item]{...},得到的是编号列表——实测中,写了两个 \note[item] 的帧,其备注页上排着「1. first bullet」「2. second bullet」。另一件值得知道的事,是把单独的 \note{...} 放在 \end{frame} 之后:它会变成不依附任何帧的独立备注页,正适合放较长的提醒,比如「进入下一节之前,记住这三点」。备注不像幻灯片内容,写多少行都不会破坏版式。这是本页唯一不必担心写多的工具。

latex
\documentclass{beamer}
\setbeameroption{show notes}   % swap for "hide notes" before you present

\begin{document}
\begin{frame}{Results}
  Body of the slide.
  \note[item]{mention the sample size}
  \note[item]{do not read the table aloud}
\end{frame}

% a standalone note page, bound to no frame
\note{Before section 3: water, timer, ask about questions.}
\end{document}

一页四张幻灯片:pgfpages 与 \pgfpagesuselayout

加载 \usepackage{pgfpages},写上 \pgfpagesuselayout{4 on 1}[a4paper,border shrink=5mm]。实测中,这让一份 handout 模式的五帧幻灯片变成了 两页 A4 纵向(595.276 × 841.89 pt)。改成 2 on 1 是三页;传入 [a4paper,landscape,border shrink=5mm] 则得到 A4 横向(841.89 × 595.276 pt)。border shrink 是把每张幻灯片缩小以留出空白的量,想留装订边或给读者写字的地方时正好用得上。pgfpages 出自 Till Tantau 本人之手——beamer 与 PGF/TikZ 都是他写的——它的版本字符串至今仍是 \ProvidesPackage{pgfpages}[2011/01/05 ver 0.02],把它读作「早已稳定下来」大概最恰当。

数一数 pgfpages.sty 里,一共有九种版式:2 on 14 on 16 on 18 on 116 on 1,加上 resize to(把一张缩放到另一种纸上)、rounded cornerstwo screens with lagging secondtwo screens with optional second。注意并没有 3 on 1。偶尔出现的「用 3 on 1 却报错」正是因为这个:三张的版式由下一节的 handoutWithNotes 提供。一点实务提醒——4 on 1 放在 A4 纵向时排成 2 × 2,因此 16:9 的幻灯片左右会留下明显的空白。16:9 的稿子要么加 landscape,要么用纵向的 2 on 1,都能把纸填得更好看。

latex
% the whole handout build, in five lines
\documentclass[handout]{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}[a4paper,border shrink=5mm]

% for a 16:9 deck, turn the sheet instead
\pgfpagesuselayout{4 on 1}[a4paper,landscape,border shrink=5mm]

% one slide per sheet, scaled up to fill A4
\pgfpagesuselayout{resize to}[a4paper]

在每张幻灯片旁边印备注栏:handoutWithNotes

加上 \usepackage{handoutWithNotes},就会给 pgfpages 添上「带备注」的版式。TeX Live 2024 里是 v1.3(2022/02/22,由 Marei Peischl 与 Guido Diepen 维护),.sty 中声明了六种版式:1 on 1 with notes2 on 1 with notes3 on 1 with notes4 on 1 with notes,以及前两者的 landscape 变体。实测中,用 \pgfpagesuselayout{3 on 1 with notes}[a4paper]\setbeameroption{show notes} 的三帧幻灯片,装进了两页 A4 纵向。忘写 show notes 是最常见的事故——备注栏会一片空白。不过对课堂讲义来说这未必是坏事:空栏正好当作学生的书写格,所以即使一条 \note 也没写,这个版式依然有用。

latex
\documentclass[handout]{beamer}
\usepackage{pgfpages}
\usepackage{handoutWithNotes}
\pgfpagesuselayout{3 on 1 with notes}[a4paper,border shrink=5mm]
\setbeameroption{show notes}   % without this the note column is empty

把同一份稿子当作文档发出去:beamerarticle

有时候要发出去的并不是幻灯片的复本,而是真正能读的文字。beamer 为此准备了 beamerarticle:在 \documentclass{article} 的文件里加上 \usepackage{beamerarticle}frame 环境就会作为普通正文流下来。实测中,一份两帧的稿子在 article 一侧编译后,输出为 一页 A4,两帧的内容连成了连续的文章。\pause 被忽略,也不会套上幻灯片边框或主题。beamer 的 README 所说的「可以从讲义生成幻灯片、从幻灯片生成讲义」指的就是这套机制;beamer 本身还随包附上了同源生成的 beamerexample-lecture-beamer-version.pdfbeamerexample-lecture-print-version.pdf 两份文件作为示例。想让某段文字只出现在其中一边,就用 \mode<presentation>{...}\mode<article>{...} 包起来。

latex
% talk.tex holds only the frames; two wrappers build two documents

% slides.tex
\documentclass[aspectratio=169]{beamer}
\input{talk}

% notes.tex
\documentclass[a4paper,11pt]{article}
\usepackage{beamerarticle}
\input{talk}

% inside talk.tex, when the two versions must differ
\mode<presentation>{Three points.}
\mode<article>{The argument rests on three points, each of which is developed below.}

不改源文件就导出多份:\PassOptionsToClass 与 -jobname

改一行 \documentclass 然后编译三遍——这种做法迟早会让你在开讲前五分钟投出 handout 版。改为在命令行传选项,源文件就一个字都不用动:pdflatex -jobname=slides-handout "\PassOptionsToClass{handout}{beamer}\input{slides.tex}"。实测中,同一份 slides.tex 普通编译是 3 页,而这一行产出了 2 页的 slides-handout.pdf(两者都是 453.543 × 255.118 pt)。-jobname 才是关键的那一半——不加它,两次编译会争抢同一份 .aux.nav,帧总数和目录都会串。备注也一样,beamer 有一个 class 选项 notesbeamer.cls 接受的值是 hideshowonlyonlyslideswithnotes 四个。于是 \setbeameroption{show only slides with notes} 可以写成一个词:\documentclass[notes=onlyslideswithnotes]{beamer}(注意这种写法里空格都消失了)。

terminal
# the talk itself
pdflatex -jobname=slides slides.tex

# the handout, from the identical unedited source
pdflatex -jobname=slides-handout \
  "\PassOptionsToClass{handout}{beamer}\input{slides.tex}"

# your own script: the note pages only
pdflatex -jobname=slides-notes \
  "\PassOptionsToClass{notes=only}{beamer}\input{slides.tex}"

# remember: every one of these needs two runs for .nav and .toc

真要印在纸上时:用纸、墨粉与最后的核对

打印之前,先看看主题的背景色。beamer 的一些配色主题是深色的:albatrossbeamercolorthemealbatross.sty 中声明 \setbeamercolor*{normal text}{fg=yellow!50!white,bg=blue!50!black}——不是黑字白底,而是深蓝底上的淡黄字。印四十份,纸会翘、墨粉会耗尽,而且本来就不好读。做讲义时,要么退回 \usecolortheme{default}(即 fg=black,bg=white),要么用类似 \mode<handout>{\usecolortheme{default}} 的写法按模式切换。像 beetle 这种背景为 bg=black!40(40 % 灰)的主题同理。纸张方面,留出大约 5 mm 的 border shrink,可以同时吸收装订边和复印机裁掉的那一点。最后,按下打印之前先用 pdfinfo 看一眼——最常见的失败,是发现自以为的 A4 其实还是 128 × 96 mm。

  • 演讲用 — 保留 overlay,\documentclass[aspectratio=169]{beamer}。会场上投影的那一份。
  • 纸质讲义\documentclass[handout]{beamer}pgfpages4 on 1。凡是用了 \only 的地方,别忘了写上 handout: 那一段。
  • 自己的讲稿\setbeameroption{show only notes}。只印备注,放在讲台上。
  • 给听众的书写页handoutWithNotes3 on 1 with notes。忘写 show notes 就是空栏,不过那样也能用。
  • 真正可读的材料\documentclass{article}beamerarticle,适合讲义应当是文章而非幻灯片的场合。
  • 共同的坑 — 这几种输出不能共用 .aux.nav。每一份都要有自己的 job 名,用独立目录或 -jobname