标签 Linux 下的文章

在TeX文档中插入源代码

近期有了在TeX文档中插入源代码的需要。TeX的\verbatim可以帮助你保留输入text的原始格式,但用于输入源代码还是显得不够专业。Google了一下发现TeX中支持插入源代码的包也有不少,如LGrind、Listings等。LGrind似乎没有包含在TeX Live的默认安装包中,用apt-get尝试安装LGrind,发现居然要占用近200M的空间,遂放弃之,最后我选择了Listings宏包。

Listings宏包短小而强大,其典型应用方式如下:

\usepackage{listings}
\lstset{…}

\begin{lstlisting}
#include
int main(int argc, const char *argv[]) {
    printf("Hello World!\n");
    return 0;
}
\end{lstlisting}

\lstinputlisting{HelloWorld.c}

其中\lstset用于全局设置插入源代码的类型、各种语法元素的样式、边框和行号设置。你的源码只需包裹在\begin{lstlisting}和\end{lstlisting}之间,源码就能按照之前设置的格式显示。\lstinputlisting支持将一个独立的源代码文件load进来,并按\lstset的格式显示。下面是一个插入C语言源码的例子:

\lstset{ language={[ANSI]C},
         showspaces=false,
         showtabs=false,
         tabsize=4,
         frame=single,
         framerule=1pt,
         framexleftmargin=5mm,
         framexrightmargin=5mm,
         framextopmargin=5mm,
         framexbottommargin=5mm,
         %numbers=left,
         %numberstyle=\small,
         basicstyle=\tt,
         directivestyle=\tt,
         identifierstyle=\tt,
         commentstyle=\tt,
         stringstyle=\tt,
         keywordstyle=\color{blue}\tt }

\begin{lstlisting}
#include
int main(int argc, const char *argv[]) {
    printf("Hello World!\n");
    return 0;
}
\end{lstlisting}

上面lstset中每种语法元素的style都设置为\tt。说到\tt,就不能不提到西方字母字族的种类,分为serif、sans serif和monospace三类。其中serif来源于荷兰语, "衬线"的意思,又称为Roman,一般用于正文的主字体,感觉很正式,我们常用的"Times New Roman"字体就归于此族; sans serif中的sans来源自法文,意为“非”,这类字体比较平滑,字体较大,适于在标题中使用,如"Arial"字体。monospace是等宽字族,也称为typewriter,程序源代码用此族字体表示更为美观,常见的字体包括Courier New、Lucida Console等。其中\tt指的就是使用monospace字族; \rm表示使用serif字族,\sf则是使用sans serif字族的意思。

确定了字族后,我们可以通过TeX preamble区的字体设置得知具体的字体,如在上面例子中,我们是这么设置字体的:
\setCJKmainfont{WenQuanYi Micro Hei}
\setCJKsansfont{WenQuanYi Micro Hei}
\setCJKmonofont{WenQuanYi Micro Hei}

\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}

CJK相关的字体设置影响的是中文字体,而真正对代码起作用的是后面的英文字体设置。这里我们的mono字体设置为了"Courier New",这样我们的源码就会以Courier New的形式展现出来。

我更新了之前制作的book和ppt的TeX模板,以支持插入源代码,有意者可在此下载

一个制作朴素幻灯片的TeX模板

自从有了For book的中文TeX模板后,我对TeX的热情便"继续"一发而不可收拾^_^。上周原本计划为内部的一个交流准备一个PPT,但在开始构思之前却突然想到:是否可以使用TeX完成幻灯片制作呢?Google了一下,果然有成熟解决方案-使用BEAMER

有了TeX基础后,学习使用Beamer构建幻灯片就显得容易了许多,用TeX创建幻灯片文档与编写普通文档差别并不大。TeX制作的幻灯片文档也是由三个部分组成:文档类声明、Preamble区和正文区。

文档类声明中的选项为beamer,表示我们要创建幻灯片文档。
\documentclass{beamer} % 文档类声明

Preamble区甚至可以复用普通TeX文档中的那些设置,这里不再赘述^_^。

正文区的内容大多与普通TeX文档也类似,只是幻灯片使用frame来组织。每个幻灯片由一组frame构成,而每个frame又包含多个slide。\section和\subsection依然可以在幻灯片中使用,不过我还似乎没有发现他们的实际价值在哪里,所以我在模板中也没有使用它们。但itemize、enumerate以及block在幻灯片制作中的作用却甚是重要。以下是模板正文区内容:

\begin{document}

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

\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}

\begin{frame}
\frametitle{first frame}
\framesubtitle{usage of itemize}
    This is the first frame using \XeTeX~and beamer.
    \begin{itemize}
        \item xx    % first slide of this frame
        \item yy    % second slide of this frame
        \item zz    % third slide of this frame
    \end{itemize}
\end{frame}

\begin{frame}
\frametitle{second frame}
\framesubtitle{usage of enumerate}
    This is the second frame.
    \begin{enumerate}
        \item xx
        \item yy
        \item zz
    \end{enumerate}
\end{frame}

\begin{frame}
\frametitle{third frame}
\framesubtitle{usage of block}
    This is the third frame.
    \begin{block}{Advantage}
        The obvious disadvantage of this approach is that you have to know LaTeX in order to use Beamer.
    \end{block}
    \begin{block}{disadvantage}
        The advantage is that if you know LaTeX, you can use your knowledge of LaTeX also when creating a presentation, not  only when writing papers.
    \end{block}
\end{frame}
\end{document}

之所以称之为朴素幻灯片模板,是因为这里并不包含一些很炫的特效。Beamer手册(texdoc beamer)有240多页,相信其中可能会包含如何制作一些特效的内容。

完整的幻灯片模板可从这里下载。

如发现本站页面被黑,比如:挂载广告、挖矿等恶意代码,请朋友们及时联系我。十分感谢! Go语言第一课 Go语言进阶课 Go语言精进之路1 Go语言精进之路2 Go语言第一课 Go语言编程指南
商务合作请联系bigwhite.cn AT aliyun.com

欢迎使用邮件订阅我的博客

输入邮箱订阅本站,只要有新文章发布,就会第一时间发送邮件通知你哦!

这里是 Tony Bai的个人Blog,欢迎访问、订阅和留言! 订阅Feed请点击上面图片

如果您觉得这里的文章对您有帮助,请扫描上方二维码进行捐赠 ,加油后的Tony Bai将会为您呈现更多精彩的文章,谢谢!

如果您希望通过微信捐赠,请用微信客户端扫描下方赞赏码:

如果您希望通过比特币或以太币捐赠,可以扫描下方二维码:

比特币:

以太币:

如果您喜欢通过微信浏览本站内容,可以扫描下方二维码,订阅本站官方微信订阅号“iamtonybai”;点击二维码,可直达本人官方微博主页^_^:
本站Powered by Digital Ocean VPS。
选择Digital Ocean VPS主机,即可获得10美元现金充值,可 免费使用两个月哟! 著名主机提供商Linode 10$优惠码:linode10,在 这里注册即可免费获 得。阿里云推荐码: 1WFZ0V立享9折!


View Tony Bai's profile on LinkedIn
DigitalOcean Referral Badge

文章

评论

  • 正在加载...

分类

标签

归档



View My Stats