首页 技术 正文
技术 2022年11月14日
0 收藏 982 点赞 4,329 浏览 1205 个字

Latex是一个文本排版的语言,能排版出各种我们想要的效果。而且用代码排版的优点是易于修改板式,因此在文本内容的排版时,Latex应用十分广泛。

当我们需要在Latex中插入代码时,就需要用到 \usepackage{listings} 宏包。例如插入一个简单的C语言代码

#include <stdio.h>
int main(int argc, char ** argv)
{
printf("Hello, world!\n");
return ;
}

要将上面 Hello,world! 这段C语言代码用Latex实现排版的效果,Latex的脚本如下

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor} %代码着色宏包
\usepackage{CJK} %显示中文宏包\lstset{
basicstyle=\tt,
%行号
numbers=left,
rulesepcolor=\color{red!20!green!20!blue!20},
escapeinside=``,
xleftmargin=2em,xrightmargin=2em, aboveskip=1em,
%背景框
framexleftmargin=.5mm,
frame=shadowbox,
%背景色
backgroundcolor=\color[RGB]{245,245,244},
%样式
keywordstyle=\color{blue}\bfseries,
identifierstyle=\bf,
numberstyle=\color[RGB]{0,192,192},
commentstyle=\it\color[RGB]{96,96,96},
stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},
%显示空格
showstringspaces=false
}\begin{document}
\begin{CJK*}{GBK}{song}
\lstset{language=C}
\begin{lstlisting} %插入要显示的代码
#include <stdio.h>
int main(int argc, char ** argv)
{
/*`打印`Hello,world*/
printf("Hello, world!\n"); return 0;
}\end{lstlisting}
\end{CJK*}
\end{document}

上面的Latex脚本可以显示出C语言中的注释(包括中文注释),代码着色,并添加了代码行号。效果如下
Latex中插入C语言代码

参考:

[1] https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings

[2] Latex论坛,http://tex.stackexchange.com/

[3] Latex Beginning’s Guide pdf  提取码: ac4n

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848