LaTeX Beamer数理报告设计精通:主题制作

不得不说Beamer自带的主题实在是跟不上时代。本节介绍如何使用和制作主题(theme)。

Beamer的主题分为五部分:演示主题、颜色主题、字体主题、内部主题、外部主题。通常演示主题指的是由其他四个组合而成的一个完整的主题,而其他四类分别控制颜色、字体、内容元素、页眉页脚等。在指定了演示主题之后,默认其他的四个也会指定,但是也可以手动指定更换演示主题包含的子主题。在设计时,这几部分可以放在同一个主题文件中,也可以分开放到专门的文件。关于这些我们暂时不详细介绍,我们先从最基本最重要最有用的地方开始讲。

自定义的主题就是一个beamerthemeNAME.sty文件,这里NAME就是主题的名字。这个文件一般要放在与beamer的.tex文件相同的目录下。可以在网上找好看的主题文件直接使用。

使用主题:在导言区

1
2
\usetheme[⟨options⟩]{⟨name list⟩}
% ⟨name list⟩就是主题的名字,例如上面的NAME

例如:

1
\usetheme{default}

一个基本的测试tex文档,用于展示主题(首页)的效果(假设主题名字为general,并且在.tex文档目录下有beamerthemegeneral.sty主题文件):

pres.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\title{There Is No Largest Prime Number}
\subtitle{A Math Talk}
\date[ISPN ’80]{27th International Symposium of Prime Numbers}
\author[Euclid]{Euclid\\\texttt{euclid@alexandria.edu}}
\institute{U of Alexandria}

\usetheme{general}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

\begin{frame}{Hello world}
Hi!
\begin{itemize}
\item First
\item Second
\end{itemize}
\end{frame}

\end{document}

注意:一般标题、作者、时间等首页信息需要放在\usetheme{}之前。

一个最简单的主题

下面我们介绍用最少的知识做出一个最简单、最漂亮的主题。

一个最简单的主题至少要包含对presentation部分的描述,所以第一行写:

1
2
\mode<presentation>
% 关于presentation的部分

这一行标志着从这里到下一个\mode或者至文件结束都是关于presentation部分的设计。

主题颜色

调用颜色包:

1
\RequirePackage{xcolor}

这里\RequirePackage{}相当于文档中的\usepackage{}。之后就可以使用包中定义过的颜色。

除了xcolor包自带颜色,还可以自定义主题中用到的颜色:

1
2
3
4
% 两种取色方法
\definecolor{Gblue}{RGB}{0,32,91}
\definecolor{Gred}{HTML}{E32235}
\definecolor{Ggray}{HTML}{303030}

然后可以指定主题中各个部分文字的颜色:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
% 首页
\setbeamercolor{title}{fg=Gblue}
\setbeamercolor{subtitle}{fg=Gred}
\setbeamercolor{author}{fg=black}
\setbeamercolor{institute}{fg=green}
\setbeamercolor{date}{fg=Ggray}

% 每一页标题
\setbeamercolor{frametitle}{fg = Gblue}

% itemize列表文字
\setbeamercolor{item}{fg = Gred}
\setbeamercolor{subitem}{fg = gray}

% 正文普通文字
\setbeamercolor{normal text}{fg=Ggray}

除此之外,还可以设置block的颜色、页面背景色以及文本背景色等等。见Beamer手册\setbeamercolor

首页设计

这里列出了常用的insert。更多选择可以看beamer的手册第8章。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\defbeamertemplate*{title page}{general}[1][]{%
\begin{center}
{\usebeamercolor[fg]{title}\inserttitle}

{\usebeamercolor[fg]{subtitle}\insertsubtitle}

{\usebeamercolor[fg]{author}\insertauthor}

{\usebeamercolor[fg]{institute}\insertinstitute}

{\usebeamercolor[fg]{date}\insertdate}

\end{center}
}

如果你愿意,可以使用textblock等继续折腾。

进阶

禁用导航条

导航(按钮)条是beamer的一个特色,但是实际上很少用到。如果你希望禁用导航条,直接在\mode<presentation>下面写:

1
2
% disable navigation symbols
\setbeamertemplate{navigation symbols}{}

调用package

在主题的设计中经常要调用其他package,最典型的有图像包、颜色包、画图包、字体包等。一般导入包的语句紧接着\mode<presentation>。一个常用的导入如下:

1
2
3
4
5
6
7
8
9
10
11
% REQUIREMENTS
% we use TikZ for drawing the background network on the slides
\RequirePackage{tikz}
\usetikzlibrary{positioning}

% we need this packages for importing images and using colors
% especially in tables
\RequirePackage{graphicx,xcolor,colortbl}

% change the font size of captions
\RequirePackage[font=footnotesize]{caption}

这里\RequirePackage{}相当于.tex文件中的\usepackage{}

页边距

1
2
3
4
% GEOMETRY
% setting the parskip and the margins
\parskip=1ex
\setbeamersize{text margin left=7mm,text margin right=7mm}

字体

定义颜色

例如:

1
\definecolor{⟨color name⟩}{RGB}{0,32,91}

RGB取值从0到255。

封面页

封面页也就是beamer的第一页,一般要显示标题、作者、日期等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
% TEMPLATE FOR THE TITLE PAGE
\defbeamertemplate*{title page}{NAME}[1][]{%
%We use TIKZ for the whole layout.
\begin{tikzpicture}
% the title is displayed in a colored box with white sepration lines
% between the lines of the title
\useasboundingbox (0,0) rectangle (\the\paperwidth,\the\paperheight);
\node[anchor = south west, align = left, text width = 8cm, color = white,
inner xsep = 0cm,inner ysep = 1mm, fill = \titlecolor] (title) at (-.7,1.75)
{\hspace{.7cm}\parbox{7cm}{
\LARGE \baselineskip=8mm \textbf{\MakeUppercase{\inserttitle}}\par
}};
\draw[ultra thick,white] ([yshift=2.4cm]title.south west)--([yshift=2.4cm]title.south east);
\draw[ultra thick,white] ([yshift=.8cm]title.south west)--([yshift=.8cm]title.south east);
\draw[ultra thick,white] ([yshift=1.6cm]title.south west)--([yshift=1.6cm]title.south east);
\fill[\titlecolor] ([yshift=-.5mm,xshift=.07mm] title.north west)rectangle([yshift=1.5mm,xshift=-.07mm]title.north east);

% display the other presentation informations according to the UPB template
\node[anchor = south west, align = left, color = upbblue, inner sep = 0cm,
xshift=7mm, yshift = 3mm]
at (title.north west) {\textbf{\insertinstitute}};

\node[anchor = north west, align = left, color = upbblue, inner sep = 0cm,
xshift=7mm, yshift = -3mm] (subtitle)
at (title.south west) {\textbf{\MakeUppercase{\insertsubtitle}}};

\node[anchor = north west, align = left, color = upbblue, inner sep = 0cm,
xshift=0mm, yshift = -3mm] (author)
at (subtitle.south west) {\footnotesize \insertauthor};

\node[anchor = north east, align = right, color = upbblue, inner sep = 0cm,
xshift=\the\paperwidth-14mm, yshift = 0mm]
at (author.north west) {\footnotesize \insertdate};
\end{tikzpicture}
}

空间布局

逻辑要素

叠层:单击后显示

结构性目录

准备演讲

调整显示比例

双屏下显示备注

打印讲义

一个sty文件结构主要分为两大块:

1
2
3
4
\mode<presentation>
% 关于presentation的部分
\mode<handout>
% 关于handout的部分

关于讲义handout,简单的处理就是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\mode<handout>{
\RequirePackage{pgfpages}
\ifnum\theslidesperhandout=1\relax
\pgfpagesuselayout{resize to}[letter, border shrink = 2.5mm,landscape]
\fi

\ifnum\theslidesperhandout=2\relax
\pgfpagesuselayout{2 on 1}[letter, border shrink = 2.5mm]
\fi

\ifnum\theslidesperhandout=4\relax
\pgfpagesuselayout{4 on 1}[letter, border shrink = 2.5mm,landscape]
\fi
}

这样就能够应对一般的单页(letter)1张、2张和4张slides的情况。关于如何使用.tex文件输出handout,我们在后文介绍。