Beamer-LaTeX学习(教程批注版)【4】
众所周知,随着显示器普及宽屏化,PPT排版需要多列化来充分利用页面的横向空间。
这节课我们来学习如何创建列,即在beamer中增加列内容,以及如何对齐列。
1 使用不同的宽度来创建列
在beamer中我们通过在页面上使用columns环境来创建列。接着,在最开始的地方我们使用\column
命令,再在后面添加上列的宽度,或者使用\begin{column}...\end{column}
。
在接下来的这个实例中,我们创建了两个不同宽度的列:
% !TEX program = xelatex\documentclass[aspectratio=169]{beamer}
\usetheme{AnnArbor}
\usecolortheme{crane}
\usepackage{fontspec}
\usepackage{xeCJK}
\usepackage{tikz}
\usetikzlibrary{intersections}
\setmainfont{AlibabaPuHuiTi-3-55-Regular.ttf}
\setCJKmainfont{AlibabaPuHuiTi-3-55-Regular.ttf}
\setsansfont{Arial}
\setmonofont{Consolas}\title[中间脚注]{耳东小白的beamer学习测试}
\subtitle{哆哆嗦嗦的历险之旅}
\author[左侧脚注]{耳东小白\inst{1} \and 耳东大白\inst{2}}
\institute[]{\inst{1} 耳东小白工作室 \and \inst{2} 耳东大白工作室}
\date[右侧脚注]{\today}
\titlegraphic{\begin{tikzpicture}[overlay, remember picture]\node[left=0.2cm] at (current page.15){\includegraphics[width = 1cm]{./pics/Logo.png}};\end{tikzpicture}
}\begin{document}\begin{frame}{Columns in beamer}\begin{columns}\column{0.6\textwidth}\centeringThis is column one with 0.6 text width.\column{0.4\textwidth}\centeringThis is column two with 0.4 text width.\end{columns}\end{frame}\end{document}
这里可以忽略一下小白这个风格的相关设置,仅关注今天这个话题的核心部分:
% Columns in beamer
\documentclass{beamer}% Theme choice:
\usetheme{AnnArbor}
\usecolortheme{crane}\begin{document}\begin{frame}{Columns in beamer}\begin{columns}\column{0.6\textwidth}\centeringThis is column one with 0.75 text width.\column{0.4\textwidth}\centeringThis is column two with 0.25 text width.\end{columns}
\end{frame}\end{document}
初步评论:
- 我们使用了
AnnArbor
主题,因为小白喜欢这个主题,载入的方法是\usetheme{AnnArbor}
; - 页面的标题为"Columns in beamer";
- 我们创建了两列:其中一个占文本宽度的60%,另一个占文本宽度的40%;
- 文本在每列中使用
\centering
来居中文本。
2 beamer中文字旁边的附图
与以上的方法相同,我们可以把文本和图片像下面这样放在同一页中:
\begin{document}\begin{frame}{Text and Image in beamer}\begin{columns}\column{0.4\textwidth}This is an example of text and image in the same slide using columns environment.\column{0.6\textwidth}\begin{figure}\centering\includegraphics[width=\textwidth]{Neural-Network.jpg}\caption{Neural Network with 5 neurons in the hidden layer. }\end{figure}\end{columns}
\end{frame}\end{document}
其中,页面右列插入了一张神经网络图。这里插入的是一张jpg图片,名字就叫Neural-Network.jpg.
需要注意的是,右侧的图像是通过一个叫tikz的包绘制的,而这个包的作者和beamer的作者是同一人——Till TanTau,小白目前正在努力学习这个强大的绘图包,相信很快也会有一个系列的文章来和大家一同学习如何使用它。
至于如何绘制这张图,可以参考以下网址的博客:
read the post
那么小白因为略微看了一下Tantau大神的tikz的文档(这位神是一个文档狂热爱好者,tikz的文档长达上千页,你敢信),所以暂时帮大家把这段绘图代码直接嵌入到beamer中:
\begin{frame}{Text and Image in beamer}\begin{columns}\column{0.4\textwidth}This is an example of text and image in the same slide using columns environment.\column{0.6\textwidth}\begin{figure}% Input layer neurons'number\newcommand{\inputnum}{3} % Hidden layer neurons'number\newcommand{\hiddennum}{5} % Output layer neurons'number\newcommand{\outputnum}{2} \begin{tikzpicture}% Input Layer\foreach \i in {1,...,\inputnum}{\node[circle, minimum size = 6mm,fill=orange!30] (Input-\i) at (0,-\i) {};}% Hidden Layer\foreach \i in {1,...,\hiddennum}{\node[circle, minimum size = 6mm,fill=teal!50,yshift=(\hiddennum-\inputnum)*5 mm] (Hidden-\i) at (2.5,-\i) {};}% Output Layer\foreach \i in {1,...,\outputnum}{\node[circle, minimum size = 6mm,fill=purple!50,yshift=(\outputnum-\inputnum)*5 mm] (Output-\i) at (5,-\i) {};}% Connect neurons In-Hidden\foreach \i in {1,...,\inputnum}{\foreach \j in {1,...,\hiddennum}{\draw[->, shorten >=1pt] (Input-\i) -- (Hidden-\j); }}% Connect neurons Hidden-Out\foreach \i in {1,...,\hiddennum}{\foreach \j in {1,...,\outputnum}{\draw[->, shorten >=1pt] (Hidden-\i) -- (Output-\j);}}% Inputs\foreach \i in {1,...,\inputnum}{ \draw[<-, shorten <=1pt] (Input-\i) -- ++(-1,0)node[left]{$x_{\i}$};}% Outputs\foreach \i in {1,...,\outputnum}{ \draw[->, shorten <=1pt] (Output-\i) -- ++(1,0)node[right]{$y_{\i}$};}\end{tikzpicture}\caption{Neural Network with 5 neurons in the hidden layer.}\end{figure}\end{columns}
\end{frame}
绘制的效果大家可以和直接导入jpg的效果比较下:需要说明的是,tikz直接绘制出来的是矢量图,效果非常赞, 图像细节无惧放大和缩小。
3 两列之间的分割线
为了更明显地区分两列或更多列的区域,我们可以创建一竖直线来分割它们。这可以通过简单地加上一句\rule
命令,在两列中间添另一根极窄的线来完成。以下是在两列间添加分割线的示例:
\begin{frame}{Vertical line between columns}\begin{columns}
% Column 1\begin{column}{0.49\textwidth}\begin{itemize}\item Input layer: 2 neurons.\item Hidden layer: 5 neurons.\item Output layer: 2 neurons.\end{itemize}\end{column}% Column 2 (vertical line)\begin{column}{.02\textwidth}\rule{.1mm}{0.7\textheight}\end{column}% Column 3 \begin{column}{0.49\textwidth}\includegraphics[width=\textwidth]{Neural-Network.jpg}\end{column}\end{columns}
\end{frame}
得到的效果如下所示:
总结一下:
- 这里创建了三列,宽度分别为页面文本宽度的0.49, 0.02, 0.49倍;
- 第一列包含一个无序列表,第二列包含这条垂直分割线,第三列包含一张图片;
- 垂直分割线的宽度为0.1mm,其高度为文本区域高度的70%,这是通过命令
\rule{<width>}{<height>}
来实现的。
注意:前面我们使用
\column
命令而这里使用\begin{column}...\end{column}
, 这两种用法是等价的。
4 列的垂直对齐
列内容的对齐,对演讲稿的美观度来说非常重要。文本和图像可以被放置在页面的三种位置:顶部、底部和居中。通过在column环境开始之后指定[c]
,[T]
,[b]
,即可分别将短内容对齐到中心、顶部和底部。
4.1 顶部对齐
\begin{frame}{Vertical alignment (top)}\begin{columns}[T]
% Column 1\begin{column}{0.5\textwidth}This is a neural network with two inputs and two outputs. It has the following parameters:\begin{itemize}\item Input layer: 2 neurons.\item Hidden layer: 5 neurons.\item Output layer: 2 neurons.\end{itemize}The neural network is drawn in \LaTeX{} using Ti\textit{k}Z package. Check latexdraw.com for more details.In Xiaobai's Example, we need to write more text to show the effect of alignment.So these words are just for space occupying.\end{column}% Column 2 \begin{column}{0.5\textwidth}\includegraphics[width=\textwidth]{Neural-Network1.jpg}\end{column}\end{columns}
\end{frame}
注意小白因为采用了宽屏比,为了凸显出对齐效果,特意在文本中加长了一部分“废话”,目的是为了把文本增高,这样才能看出图片与文本块的对齐关系。
4.2 中间对齐
\begin{frame}{Vertical alignment (center)}\begin{columns}[c]
% Column 1\begin{column}{0.5\textwidth}This is a neural network with two inputs and two outputs. It has the following parameters:\begin{itemize}\item Input layer: 2 neurons.\item Hidden layer: 5 neurons.\item Output layer: 2 neurons.\end{itemize}The neural network is drawn in \LaTeX{} using Ti\textit{k}Z package. Check latexdraw.com for more details.In Xiaobai's Example, we need to write more text to show the effect of alignment.So these words are just for space occupying.\end{column}% Column 2 \begin{column}{0.5\textwidth}\includegraphics[width=\textwidth]{Neural-Network1.jpg}\end{column}\end{columns}
\end{frame}
\begin{frame}{Vertical alignment (bottom)}
4.3 底部对齐
\begin{columns}[b]
% Column 1\begin{column}{0.5\textwidth}This is a neural network with two inputs and two outputs. It has the following parameters:\begin{itemize}\item Input layer: 2 neurons.\item Hidden layer: 5 neurons.\item Output layer: 2 neurons.\end{itemize}The neural network is drawn in \LaTeX{} using Ti\textit{k}Z package. Check latexdraw.com for more details.In Xiaobai's Example, we need to write more text to show the effect of alignment.So these words are just for space occupying.\end{column}% Column 2 \begin{column}{0.5\textwidth}\includegraphics[width=\textwidth]{Neural-Network1.jpg}\end{column}\end{columns}
\end{frame}
全文总结
- 在beamer中可通过column环境创建多列。我们在主列中通过预定义的宽度来实现添加内部列:
\begin{columns}{<width>}...\end{columns}
; - 通过使用命令
\rule{<width>}{<height>}
可以在两列间使用极小的宽度创建垂直分割线; - 不同列的内容,其对齐方式可以通过添加
[T]
,[b]
和[c]
,分别对应顶部对齐、底部对齐和中间对齐。