latex的listings宏包常用設置


\documentclass[UTF-8]{article}
\usepackage{listings}
\usepackage{color,xcolor}
\usepackage{fontspec}
\usepackage{xeCJK}
\setmonofont[Mapping={}]{Monaco}    %英文引號之類的正常顯示,相當於設置英文字體
\setsansfont{Monaco} %設置英文字體 Monaco, Consolas,  Fantasque Sans Mono
\setmainfont{Monaco} %設置英文字體
\setCJKmainfont{微軟雅黑}  %中文字體設置
%\setCJKsansfont{楷體} %設置中文字體
%\setCJKmonofont{} %設置中文字體

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\lstset{
backgroundcolor=\color{white},   % choose the background color
basicstyle=\footnotesize\monaco, % size of fonts used for the code或改成\small\monaco稍大
numbers=left,                        % 設置行號
numberstyle=\tiny\monaco,            % 設置行號字體大小
columns=fullflexible,
breaklines=true,                 % automatic line breaking only at whitespace
captionpos=b,                    % sets the caption-position to bottom
tabsize=4,                       % 把tab擴展為4個空格,默認是8個太長
commentstyle=\color{mygreen},    % 設置注釋顏色
escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
keywordstyle=\color{blue},       % 設置keyword顏色
stringstyle=\color{mymauve}\monaco,     % string literal style
frame=single,                        % 設置有邊框
rulesepcolor=\color{red!20!green!20!blue!20},
% identifierstyle=\color{red},
language=c++,
}

\begin{document}

\begin{lstlisting}
inline int gcd(int a, int b) { 
    // 如果a<b,則遞歸得gcd(b,a%b)即gcd(b, a),即交換了位置,時間復雜度O(log max(a, b))
    return b==0?a:gcd(b,a%b)
}
inline int lcm(int a, int b) {
    return a/gcd(a,b)*b;
}
\end{lstlisting}

\end{document}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM