我们可以使用 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}

可以看到,这时排版效果中的关键字是黑体:testlisting-3

语法高亮

可能你已经想到要怎样让关键字显示其它颜色了,我们可以定制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}

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

拓展关键字

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

\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},
}
\begin{document}     
{\setmainfont{Courier New Bold}                          % 设置代码字体                   
\begin{lstlisting}
#include <iostream>
int main()
{
    constexpr int MAX = 100;
}  
\end{lstlisting}}
\end{document}

可以看到,C++11中的关键字constexpr显示高亮:testlisting-5除此之外,我们还可以使标准库的容器也实现代码高亮:

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{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已经可以高亮了:testlisting-6选自:http://senlinzhan.github.io/2015/01/14/latex语法高亮/

点赞(18)

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部