LaTeX之代码语法高亮

来源:互联网 发布:南京软件企业排名 编辑:程序博客网 时间:2024/05/16 04:58

转自http://www.latexstudio.net/archives/5900

我们可以使用Latex来排版文章和书籍,特别是可以用Latex来排版学习笔记,可以取得很不错的效果。实际上,用Latex中插入代码,可以取得代码语法高亮的效果。
首先,我们展示以下默认的排版效果,创建一个.tex文件,写入以下内容:

\documentclass{ctexart}\usepackage{listings}\begin{document}\begin{lstlisting}#include <iostream>int main(){    std::cout << "Hello, World!" << std::endl;}  \end{lstlisting}\end{document}

那么排版效果没有什么显著特点。

可以为lstlisting环境指定可选参数language=c++,用来设定代码使用的语言:

\documentclass{ctexart}\usepackage{listings}\begin{document}\begin{lstlisting}[language=c++]#include <iostream>int main(){    std::cout << "Hello, World!" << std::endl;}  \end{lstlisting}\end{document}


可以看到,这时排版效果中的关键字是黑体。

语法高亮

我们可以定制lstlisting环境:

\documentclass{ctexart}\usepackage{listings}\usepackage{xcolor}\lstset{    columns=fixed,           numbers=left,                                        % 在左侧显示行号    frame=none,                                          % 不显示背景边框    backgroundcolor=\color[RGB]{245,245,244},            % 设定背景颜色    keywordstyle=\color[RGB]{40,40,255},                 % 设定关键字颜色    numberstyle=\footnotesize\color{darkgray},           % 设定行号格式    commentstyle=\it\color[RGB]{0,96,96},                % 设置代码注释的格式    stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},   % 设置字符串格式    showstringspaces=false,                              % 不显示字符串中的空格    language=c++,                                        % 设置语言}\begin{document}     {\setmainfont{Courier New Bold}                          % 设置代码字体                   \begin{lstlisting}#include <iostream>int main(){    std::cout << "Hello, World!" << std::endl;}  \end{lstlisting}}\end{document}

可以看到,关键字、注释和字符串都改变了颜色。

拓展关键字

尽管我们已经指定了代码的编程语言是C++,但是C++中很多关键字仍不被支持,同样,C++11中新的关键字也无法实现代码高亮。
我们可以为lstlisting环境增加更多的关键字,使其支持C++11的关键字:

\usepackage{listings}\usepackage{xcolor}% 定义可能使用到的颜色 \definecolor{CPPLight}  {HTML} {686868}\definecolor{CPPSteel}  {HTML} {888888}\definecolor{CPPDark}   {HTML} {262626}\definecolor{CPPBlue}   {HTML} {4172A3}\definecolor{CPPGreen}  {HTML} {487818}\definecolor{CPPBrown}  {HTML} {A07040}\definecolor{CPPRed}    {HTML} {AD4D3A}\definecolor{CPPViolet} {HTML} {7040A0}\definecolor{CPPGray}  {HTML} {B8B8B8}\lstset{    columns=fixed,           numbers=left,                                        % 在左侧显示行号    frame=none,                                          % 不显示背景边框    backgroundcolor=\color[RGB]{245,245,244},            % 设定背景颜色    keywordstyle=\color[RGB]{40,40,255},                 % 设定关键字颜色    numberstyle=\footnotesize\color{darkgray},           % 设定行号格式    commentstyle=\it\color[RGB]{0,96,96},                % 设置代码注释的格式    stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},   % 设置字符串格式    showstringspaces=false,                              % 不显示字符串中的空格    language=c++,                                        % 设置语言    morekeywords={alignas,continute,friend,register,true,alignof,decltype,goto,    reinterpret_cast,try,asm,defult,if,return,typedef,auto,delete,inline,short,    typeid,bool,do,int,signed,typename,break,double,long,sizeof,union,case,    dynamic_cast,mutable,static,unsigned,catch,else,namespace,static_assert,using,    char,enum,new,static_cast,virtual,char16_t,char32_t,explict,noexcept,struct,    void,export,nullptr,switch,volatile,class,extern,operator,template,wchar_t,    const,false,private,this,while,constexpr,float,protected,thread_local,    const_cast,for,public,throw,std},}\begin{document}     {\setmainfont{Courier New Bold}                          % 设置代码字体                   \begin{lstlisting}#include <iostream>int main(){    constexpr int MAX = 100;}  \end{lstlisting}}\end{document}
可以看到,C++11中的关键字constexpr显示高亮。

除此之外,我们还可以使标准库的容器也实现代码高亮:

\usepackage{listings}\usepackage{xcolor}% 定义可能使用到的颜色 \definecolor{CPPLight}  {HTML} {686868}\definecolor{CPPSteel}  {HTML} {888888}\definecolor{CPPDark}   {HTML} {262626}\definecolor{CPPBlue}   {HTML} {4172A3}\definecolor{CPPGreen}  {HTML} {487818}\definecolor{CPPBrown}  {HTML} {A07040}\definecolor{CPPRed}    {HTML} {AD4D3A}\definecolor{CPPViolet} {HTML} {7040A0}\definecolor{CPPGray}  {HTML} {B8B8B8}\lstset{    columns=fixed,           numbers=left,                                        % 在左侧显示行号    frame=none,                                          % 不显示背景边框    backgroundcolor=\color[RGB]{245,245,244},            % 设定背景颜色    keywordstyle=\color[RGB]{40,40,255},                 % 设定关键字颜色    numberstyle=\footnotesize\color{darkgray},           % 设定行号格式    commentstyle=\it\color[RGB]{0,96,96},                % 设置代码注释的格式    stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},   % 设置字符串格式    showstringspaces=false,                              % 不显示字符串中的空格    language=c++,                                        % 设置语言    morekeywords={alignas,continute,friend,register,true,alignof,decltype,goto,    reinterpret_cast,try,asm,defult,if,return,typedef,auto,delete,inline,short,    typeid,bool,do,int,signed,typename,break,double,long,sizeof,union,case,    dynamic_cast,mutable,static,unsigned,catch,else,namespace,static_assert,using,    char,enum,new,static_cast,virtual,char16_t,char32_t,explict,noexcept,struct,    void,export,nullptr,switch,volatile,class,extern,operator,template,wchar_t,    const,false,private,this,while,constexpr,float,protected,thread_local,    const_cast,for,public,throw,std},    emph={map,set,multimap,multiset,unordered_map,unordered_set,    unordered_multiset,unordered_multimap,vector,string,list,deque,    array,stack,forwared_list,iostream,memory,shared_ptr,unique_ptr,    random,bitset,ostream,istream,cout,cin,endl,move,default_random_engine,    uniform_int_distribution,iterator,algorithm,functional,bing,numeric,},    emphstyle=\color{CPPViolet}, }\begin{document}     {\setmainfont{Courier New Bold}                          % 设置代码字体                   \begin{lstlisting}#include <iostream>#include <array>int main(){    constexpr int MAX = 100;    std::array<int, MAX> arr;}  \end{lstlisting}}\end{document}

可以看到,array已经可以高亮了

TeX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
documentclass{ctexart}
\usepackage{listings}
\usepackage{xcolor}
%定义可能使用到的颜色
 
\definecolor{CPPLight}  {HTML}{686868}
\definecolor{CPPSteel}  {HTML}{888888}
\definecolor{CPPDark}  {HTML}{262626}
\definecolor{CPPBlue}  {HTML}{4172A3}
\definecolor{CPPGreen}  {HTML}{487818}
\definecolor{CPPBrown}  {HTML}{A07040}
\definecolor{CPPRed}    {HTML}{AD4D3A}
\definecolor{CPPViolet}{HTML}{7040A0}
\definecolor{CPPGray}  {HTML}{B8B8B8}
\lstset{
    columns=fixed,      
    numbers=left,                                        % 在左侧显示行号
    frame=none,                                          % 不显示背景边框
    backgroundcolor=\color[RGB]{245,245,244},            % 设定背景颜色
    keywordstyle=\color[RGB]{40,40,255},                % 设定关键字颜色
    numberstyle=\footnotesize\color{darkgray},          % 设定行号格式
    commentstyle=\it\color[RGB]{0,96,96},                % 设置代码注释的格式
    stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},  % 设置字符串格式
    showstringspaces=false,                              % 不显示字符串中的空格
    language=c++,                                        % 设置语言
    morekeywords={alignas,continute,friend,register,true,alignof,decltype,goto,
    reinterpret_cast,try,asm,defult,if,return,typedef,auto,delete,inline,short,
    typeid,bool,do,int,signed,typename,break,double,long,sizeof,union,case,
    dynamic_cast,mutable,static,unsigned,catch,else,namespace,static_assert,using,
    char,enum,new,static_cast,virtual,char16_t,char32_t,explict,noexcept,struct,
    void,export,nullptr,switch,volatile,class,extern,operator,template,wchar_t,
    const,false,private,this,while,constexpr,float,protected,thread_local,
    const_cast,for,public,throw,std},
    emph={map,set,multimap,multiset,unordered_map,unordered_set,
    unordered_multiset,unordered_multimap,vector,string,list,deque,
    array,stack,forwared_list,iostream,memory,shared_ptr,unique_ptr,
    random,bitset,ostream,istream,cout,cin,endl,move,default_random_engine,
    uniform_int_distribution,iterator,algorithm,functional,bing,numeric,},
    emphstyle=\color{CPPViolet},
}
\begin{document}    
{\setmainfont{CourierNewBold}                          % 设置代码字体                  
\begin{lstlisting}
#include<iostream>
#include<array>
intmain()
{
    constexprintMAX=100;
    std::array<int,MAX>arr;
}  
\end{lstlisting}}
\end{document}
0 0
原创粉丝点击