最后我想说「审美因人而异」,您觉得 LaTeX 默认的等宽字体丑,但其实 Latin Modern Mono 是有真斜体的,作为代码注释的字体很不错,反而是 Courier 系列比较局限。还可以用别的等宽字体啊,Fira Code、Monoid、Iosevka 都是不错的选择。
% 文档类
\documentclass[13pt]{ctexart}
% 设置页面
\usepackage{geometry}
\geometry{left = 3.5cm,right = 3.5cm,top=1cm}
% 设置页眉页脚
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage{tgbonum}
% 清空页眉页脚
\pagestyle{fancy}
% 代码高亮方案宏包
\usepackage{listings}
\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{
breaklines=true,
framextopmargin=50pt,
frame=bottomline,
columns=fixed,
%numbers=left, % 在左侧显示行号
frame=none, % 不显示背景边框
backgroundcolor=\color[RGB]{255,255,255}, % 设定背景颜色
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=python, % 设置语言
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,numpy,graph,path,append,extend,
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}
% 全文设置页眉页脚的地方
\fancyhf{}
\fancyhead[C]{ }
\fancyhead[R]{Page \thepage\ of \pageref{LastPage}}
\fancyhead[L]{Team \# 86765}
\fancyfoot[C]{\bfseries\thepage}
\lipsum[1]
% 开始插代码
\fontsize{12pt}{15pt}\selectfont
Here is Code we used in our model, which python is the main development language, relying on the third-party library numpy.
\vspace{7pt}
% 我想设置Courier New 字体的代码,但是此页的页眉页脚的字体也变成了Courier New
{\setmainfont{Courier New}
\lstinputlisting[language=python]{data_process.py}
\end{document}
% 用 XeLaTeX 或 LuaLaTeX 编译
\documentclass{article}
\usepackage{fontspec}
% 以下设置正文字体为 Palatino 的克隆体
\setmainfont{TeX Gyre Pagella}
% 以下设置等宽字体为 Courier 的克隆体
\setmonofont{TeX Gyre Cursor}
\usepackage{listings}
% 以下告诉 lstlisting 环境:我要用等宽字体列代码!
\lstset{basicstyle=\ttfamily}
\begin{document}
\section{Hello, world!}
Hello, world! Would you like to see some code?
\begin{lstlisting}
Happy \LaTeX{}ing!
\end{lstlisting}
See? Mono-spaced font for code and proportional font for body text.
\end{document}
效果很好啊,代码用的 Courier 等宽字体,正文用的 Palatino 比例字体,页眉页脚不乱改也是 Palatino,很好看。
在我给的例子里,您直接 Here is Code we used in our model […] \lstinputlisting[language=python]{data_process.py}
试试;
commentstyle=\itshape\color[RGB]{0,96,96},
这里用 \itshape
而不是 \it
;
stringstyle=\slshape\color[RGB]{128,0,0},
这里不要用 \rmfamily
,干嘛要切换回正文字体,您不是觉得丑吗……
另:注释掉我例子里的 \setmonofont{TeX Gyre Cursor}
,用默认的等宽字体,照样很好看。重点是您要加个 basicstyle=\ttfamily
进 \lstset
,一切搞定。
回答您的附加:
\lstinputlisting[language=python]{data_process.py}
一切正常。
您提供的注解十分详细,basicstyle=\ttfamily
已经加入。
另外两点建议也已经修改好。
再次感谢。
我觉着吧……listings
设定可以参考一下我的 easy_slides
当中的相关设定:
https://github.com/Liam0205/easy_slides/blob/master/global_setup/settings.tex#L8-L65
至少以我的审美,还挺好看的。
关于字体设定的话,我多年前在知乎上的一个回答也给出了具体例子。核心是 \newfontfamily\menlo{Menlo}
设定字体切换命令和 basicstyle=\small\menlo
及 numberstyle=\tiny\menlo
两句。
%!TEX program = xelatex
\documentclass{article}
\usepackage{listings}
\usepackage{fontspec}
\newfontfamily\menlo{Menlo}
\begin{document}
\begin{lstlisting}[language={[ANSI]C},
numbers=left,
numberstyle=\tiny\menlo,
basicstyle=\small\menlo]
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#define SIZE 26
main (int argc, char *argv[])
int array[SIZE];
int i;
char c;
for (i = 0; i < SIZE; i++)
array[i] = 0;
while ((c = getchar ()) != EOF)
if (isupper (c))
array[c - 'A']++;
for (i = 0; i < 26; i++)
printf ("%c:%5d\n", (char) ('A' + i), array[i]);
return 0;
\end{lstlisting}
\end{document}