繼上次學會在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
希望跟大家一起交流,共同進步。
