继上次学会在ppt里面插入带有高亮的c程序代码之后,zyy在这条不归路上越走越远…… 好的,长话短说,今天我介绍的是怎么在LaTeX中插入高亮的c程序代码。(其实不止c程序的代码,别的语言也是可以的,本文以c代码为例)(zyy你作业写完了吗!一天就知道写博客不去好好学习挂科了怎么办!)(……写完就去好好学习别着急…… ♪(^∇^*))

本文将介绍两种方法,当然两种都需要LaTeX(废话……),第二种需要额外的一个软件——Highlight。

[方法一]使用Listings宏包对代码进行高亮显示

这里不讲原理,直接说例子。

比如我们想插入这样的一段代码:

 1 /*
2 PROG:LeTeX test
3 NAME:zyy
4 */
5 #include <stdio.h>
6 int main()
7 {
8 //Note
9 printf("Hello world!\n");
10 return 0;
11 }

给出对应的tex的代码,注意listings对代码进行高亮要使用xcolor宏包(这里我找不到tex的高亮…… 醉了…… ):

 1 \documentclass[12pt]{article}
2 \usepackage{listings}
3 \usepackage{xcolor}
4 \lstset{
5 %行号
6 numbers=left,
7 %背景框
8 framexleftmargin=10mm,
9 frame=none,
10 %背景色
11 %backgroundcolor=\color[rgb]{1,1,0.76},
12 backgroundcolor=\color[RGB]{245,245,244},
13 %样式
14 keywordstyle=\bf\color{blue},
15 identifierstyle=\bf,
16 numberstyle=\color[RGB]{0,192,192},
17 commentstyle=\it\color[RGB]{0,96,96},
18 stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},
19 %显示空格
20 showstringspaces=false
21 }
22
23 \begin{document}
24 \begin{lstlisting}[language={C}]
25 /*
26 PROG:LeTeX test
27 NAME:zyy
28 */
29 #include <stdio.h>
30 int main()
31 {
32 //Note
33 printf("Hello world!\n");
34 return 0;
35 }
36 \end{lstlisting}
37 \end{document}

实现的样子如下:

这里有一个弊端,就是似乎不能插入中文,我试过了用CJK的环境也不行,可能是因为我对LaTeX了解还很少,如果有人实现了代码中的中文显示,请不吝赐教!

当然,我个人是觉得这个不太好看,所以建议不怕麻烦的同学看后一种方法。

[方法二]使用Highlight对代码进行高亮处理

Highlight是一款软件,可以生成许多种程序语言的高亮,并以多种形式输出,其中包括LaTeX。

这里给出下载地址(真的没有广告费):http://www.andre-simon.de/zip/download.php

Highlight的使用也相当简单,下面是它的界面:

注意到左下角的选项版,选择LaTeX输出即可,其他参数可以自己设置。这里值得一提的是,高亮主题非常多,我没有全部尝试过,不过相对来说比较喜欢bclear,当然这一款的对比度并不是很强烈,所以看个人爱好选择吧。我的选择如下:

这里注意一下选择“内嵌页面样式(defs)”。将预览结果复制到剪贴板之后,新建一个.tex文档,粘贴即可。

当然这里会有几个小问题需要修改一下:

1、为了支持中文,需要用到CJK宏包。在前面加入“\usepackage{CJK}”,并在正文中相应地添加“\begin{CJK*}{GBK}{song}”和“\end{CJK*}”;

2、有一个似乎是为了插入双引号的方式,似乎有点问题(可能是LaTex的版本不兼容),作这样的修改即可:加入“\newcommand{\dq}{"}”,并把正文中的“\shorthandoff{"}”和“\shorthandon{"}”注释掉。

(这个语法我找了很久,才在这里找到解释,感谢:http://tex.stackexchange.com/questions/299721/using-dq-in-subsections-has-an-extra

3、它生成的代码的“\end{document}”之间有空格,把空格删掉。

首先给出代码:

 1 \documentclass{article}
2 \usepackage{color}
3 \usepackage{alltt}
4 \usepackage[T1]{fontenc}
5 \usepackage[latin1]{inputenc}
6 \usepackage{CJK}%中文
7
8 \newcommand{\hlstd}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#1}}
9 \newcommand{\hlnum}[1]{\textcolor[rgb]{0.06,0.58,0.63}{#1}}
10 \newcommand{\hlesc}[1]{\textcolor[rgb]{0.86,0.41,0.09}{#1}}
11 \newcommand{\hlstr}[1]{\textcolor[rgb]{0.06,0.58,0.63}{#1}}
12 \newcommand{\hlpps}[1]{\textcolor[rgb]{0.06,0.58,0.63}{#1}}
13 \newcommand{\hlslc}[1]{\textcolor[rgb]{0.59,0.59,0.59}{#1}}
14 \newcommand{\hlcom}[1]{\textcolor[rgb]{0.59,0.59,0.59}{#1}}
15 \newcommand{\hlppc}[1]{\textcolor[rgb]{0.41,0.78,0.23}{#1}}
16 \newcommand{\hlopt}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#1}}
17 \newcommand{\hlipl}[1]{\textcolor[rgb]{0.45,0.46,0.77}{#1}}
18 \newcommand{\hllin}[1]{\textcolor[rgb]{0.59,0.59,0.59}{#1}}
19 \newcommand{\hlkwa}[1]{\textcolor[rgb]{0.23,0.42,0.78}{#1}}
20 \newcommand{\hlkwb}[1]{\textcolor[rgb]{0.63,0,0.31}{#1}}
21 \newcommand{\hlkwc}[1]{\textcolor[rgb]{0,0.63,0.31}{#1}}
22 \newcommand{\hlkwd}[1]{\textcolor[rgb]{0.78,0.23,0.41}{#1}}
23 \definecolor{bgcolor}{rgb}{1,1,1}
24 \newcommand{\dq}{"}
25
26 \title{Source file}
27 \begin{document}
28 \begin{CJK*}{GBK}{song}
29 \pagecolor{bgcolor}
30 \newsavebox{\hlboxopenbrace}
31 \newsavebox{\hlboxclosebrace}
32 \newsavebox{\hlboxlessthan}
33 \newsavebox{\hlboxgreaterthan}
34 \newsavebox{\hlboxdollar}
35 \newsavebox{\hlboxunderscore}
36 \newsavebox{\hlboxand}
37 \newsavebox{\hlboxhash}
38 \newsavebox{\hlboxat}
39 \newsavebox{\hlboxbackslash}
40 \newsavebox{\hlboxpercent}
41 \newsavebox{\hlboxhat}
42 \setbox\hlboxopenbrace=\hbox{\verb.{.}
43 \setbox\hlboxclosebrace=\hbox{\verb.}.}
44 \setbox\hlboxlessthan=\hbox{\verb.<.}
45 \setbox\hlboxgreaterthan=\hbox{\verb.>.}
46 \setbox\hlboxdollar=\hbox{\verb.$.}
47 \setbox\hlboxunderscore=\hbox{\verb._.}
48 \setbox\hlboxand=\hbox{\verb.&.}
49 \setbox\hlboxhash=\hbox{\verb.#.}
50 \setbox\hlboxat=\hbox{\verb.@.}
51 \setbox\hlboxbackslash=\hbox{\verb.\.}
52 \setbox\hlboxpercent=\hbox{\verb.\%.}
53 \setbox\hlboxhat=\hbox{\verb.^.}
54 \def\urltilda{\kern -.15em\lower .7ex\hbox{\~{}}\kern .04em}
55 \noindent
56 \ttfamily
57 %\shorthandoff{"}
58 \hlstd{}\hllin{\ 1\ }\hlcom{/{*}}\\
59 \hllin{\ 2\ }\hlcom{项目:LeTeX测试}\\
60 \hllin{\ 3\ }\hlcom{作者:zyy}\\
61 \hllin{\ 4\ }\hlcom{{*}/}\hlstd{}\\
62 \hllin{\ 5\ }\hlppc{\#include\ \usebox{\hlboxlessthan}stdio.h\usebox{\hlboxgreaterthan}}\\
63 \hllin{\ 6\ }\hlstd{}\hlkwb{int\ }\hlstd{}\hlkwd{main}\hlstd{}\hlopt{()}\\
64 \hllin{\ 7\ }\hlstd{}\hlopt{\usebox{\hlboxopenbrace}}\\
65 \hllin{\ 8\ }\hlstd{\ }\hlslc{//这里是中文注释}\\
66 \hllin{\ 9\ }\hlstd{\ }\hlkwd{printf}\hlstd{}\hlopt{(}\hlstd{}\hlstr{\dq{}Hello\ world!}\hlesc{$\backslash$n}\hlstr{\dq{}}\hlstd{}\hlopt{);}\\
67 \hllin{10\ }\hlstd{\ }\hlkwa{return\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{;}\\
68 \hllin{11\ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\hlstd{}\\
69 \mbox{}
70 \normalfont
71 \normalsize
72 %\shorthandon{"}
73 \end{CJK*}
74 \end{document}
75 (* LaTeX generated by highlight 3.28, http://www.andre-simon.de/ *)

最后的效果是这样的:

这个比前一种方式好多了是吧?

[总结]

使用listings相当简便,要配置的东西并不多,也能实现高亮的效果;Highlight的高亮效果更加丰富,并且支持中文注释,只是实现略复杂一些。

时隔两年后的更新:

感谢@SYCstudio的交流,学习到了一种新的方法,使用minted宏包来实现代码高亮,个人觉得效果还不错,跟Highlight的效果差不多,就是最开始的配置稍微麻烦一点,不过配置好了之后每次使用会比较方便。这里贴一个介绍如何使用minted宏包的博客地址:https://blog.csdn.net/u012705410/article/details/50605374

希望跟大家一起交流,共同进步。

最新文章

  1. (实例篇)PHP实现HTTP断点续传的方法
  2. java实现文件变化监控
  3. Diode -- Pay Attention to Parallel Connection
  4. 【python】入门学习(九)
  5. [课程设计]Scrum 2.8 多鱼点餐系统开发进度(下单一览页面-菜式一览功能的最终实现)
  6. Python3中的新特性(1)——新的语言特性
  7. 哈希(1) hash的基本知识回顾
  8. Notification在不同版本SDK中的使用
  9. 2014第11周四Eclipse开发问题记
  10. ajax实现下载功能
  11. 《Python自然语言处理》第一章-练习17
  12. js里面的map、filter、forEach、reduce、for in、for of等遍历方法
  13. [BOI2004]Sequence 数字序列(左偏树)
  14. 关于pycharm中缩进、粘贴复制等文本编辑功能部分失效的解决办法
  15. border三角形
  16. 删除iis日志(deliislogs.vbs)
  17. 【F12】谷歌浏览器--前台效果可以在不访问服务器的前提下直接改样式看效果是否是预期值。
  18. Lambda引言
  19. 【UVA514】铁轨
  20. ADT Android Development Tools

热门文章

  1. 《剑指offer》面试题47. 礼物的最大价值
  2. Python 根据id生成唯一码
  3. 2022GDUT寒训专题一I题
  4. Fastjson反序列化漏洞分析 1.2.22-1.2.24
  5. Servlet-整个Servlet类的继承体系
  6. IoC容器-Bean管理注解方式(注入属性@Autowired和Qualifier)
  7. cp 不提示按y
  8. Cobbler批量安装操作系统
  9. linux区分atime,ctime and mtime
  10. Python打印JSON中中文的解决办法