添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
knightzju  ·  10分钟搞定LangChain+Milvus ...·  7 月前    · 
成熟的荔枝  ·  ASP.NET Core ...·  1 年前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account basicstyle= \setmainfont {Courier New}

我曾经还采用了另一种形式,在插入代码的前面设置: \setmainfont{Courier New}
但是两种形式都会有BUG,之后描述。
2. 正文的字体为Palatino,设置页眉的方式为

\usepackage{fancyhdr}
\fancyhead[R]{test}
\fancyhead[L]{test}
\fancyfoot[C]{test}
  • 问题描述:在论文最后几页中插入代码,上述所说的两种设置代码字体的方法都会影响页眉的字体,但是不影响正文字体,即页眉的字体发生了改变。我尝试过清空此页的页眉页脚重新设置,但是没有效果。
    \setmainfont{TeX Gyre Pagella}
    \fancyhead[L]{Team \# 86765}
    \fancyfoot[C]{\bfseries\thepage}
    \fancyhead[R]{Page \thepage\ of \pageref{LastPage}}
    \lstinputlisting[language=python]{data_process.py}

    这样没有效果,插入代码的字体会影响页眉,但是不影响正文。
    4. 请问如何单独设置代码的字体,而且代码块的字体不影响论文其他位置的字体。我在Stackoverflow,tex.stackchange和overleaf上查询了许久,没有得到较好的解决方案。他们提供的解决方案是 basicstyle=\ttfamily ,但是这样代码的字体很丑,巨丑。

    这个不叫 MWE……

    basicstyle 应该设置为「字体切换命令」,包括 \ttfamily \bfseries \itshape 等,也可以是颜色,比如 \color{red} 。而 \setmainfont 你可以理解为把 \rmfamily 设置为某个字体,是一种「字体声明命令」。所以正确的写法就是 basicstyle=\ttfamily

    \setmainfont 顾名思义,设置的是「主字体」,所以页眉之类的都会因此而改变。代码习惯上会用等宽字体(也叫打字机字体),应该用 \setmonofont 声明,用 \ttfamily 切换。

    你觉得丑,至少给个图是吧。默认 \ttfamily 是 cmtt,我觉得丑不到哪里去。如果要用 Courier New,应该这么写:

    \documentclass{article}
    \usepackage{fontspec,listings}
    \setmonofont{Courier New}
    \lstset{basicstyle=\ttfamily}
    \begin{document}
    \begin{lstlisting}  % 原为 lstlistings,已修改
    Hello, \LaTeX!
    \end{lstlisting}
    \end{document}

    我们这里暂且只考虑仅有英文的情况(basic latin)。如果有中文,在 ctex 宏集的框架下,中英文字体是分别设置的。具体去看 xeCJK ctex 的文档。

    \it \bf 是过时命令,不要再用了。

    fontspec 跟传统 type1 字体(就是拿 courier 包调用的这种)不要混用。

    相当建议读一点书,您这段代码实在是……

    建议您先阅读一下入门的教材,我推荐包太雷的《雷太赫排版系统简介》: http://dralpha.altervista.org/zh/tech/lnotes2.pdf

    提交「最小工作示例」,您要保证提交的代码能够反应您的问题。您的问题属于字体设置,而提交的代码却有太多不相关的内容。

    就字体设置而言,我总结一下 @stone-zeng 的回答:

  • 用了 fontspec 宏包,就尽量用「系统安装的字体」,不要用别的字体宏包。
  • 设置正文字体(宋体类似)、无衬线字体(黑体类似)、等宽字体(代码字体)请用 \setmainfont \setsansfont \setmonofont 。分别用一次就好,不要把这些命令嵌套进 \newcommand 里。
  • 除非您想秀您系统上有什么字体,否则请尽量不要使用 \fontspec 来指定字体。
  • 代码字体通过 \setmonofont{Courier New} 设置好了以后,Courier New 就可以由「字体切换命令」之 \ttfamily 调用。
  • 请试试 @stone-zeng 给的短小精悍的例子,它展示了上述几个要点的实现方法。
  • 最后我想说「审美因人而异」,您觉得 LaTeX 默认的等宽字体丑,但其实 Latin Modern Mono 是有真斜体的,作为代码注释的字体很不错,反而是 Courier 系列比较局限。还可以用别的等宽字体啊,Fira Code、Monoid、Iosevka 都是不错的选择。

    抱歉,可能第一次用github有点兴奋,我写的实在不是MWE,对两位带来的不便还请谅解。

    我重新简述下,eemmmmm,背景我比较菜,入门全靠Lshort。

    那个,Stone—zeng说的方法在overleaf上也有提及,但是确定不丑?效果如下:

    我尽力提供简短的MWE。问题简述,对应的代码页中,页眉的字体变成了代码字体,而且我改不回来。
    正常的页眉:
    代码页的代码也正常:
    代码页的页眉不正常:

    % 文档类
    \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}

    我对字体实在不太了解,设置字体只会 \setmainfont ,我会按照你们说的继续修改,至此十分感谢。

    那个,Stone—zeng说的方法在overleaf上也有提及,但是确定不丑?效果如下:

    啊!我猜到了是什么情况!真的很丑!不过您提供的截图确认了我的猜想。您看的这个情况,在 TeX 社区早就被人吐槽过了,参见 Is there a good reason for lstlistings code to look so ugly by default? 这个帖子。

    原因在那个帖子楼下的回答里,我这里简述一下: listings 宏包的原作者觉得,不应该把字体的审美强加于用户,所以「裸用」 lstlisting 环境列出代码,结果就是源代码会使用「正文字体」被显示出来。

    这正是您截图中的效果:很明显地,源代码使用了「正文字体」(看起来很像 Times New Roman Computer Modern Roman),虽然说是按字母对齐,但是极丑无比。这个「正文字体」本身是「比例字体」,也就是说每个字母宽度本来就是不一样的,例如 m 就会比较宽、 i 就会比较窄。「比例字体」不适合用于源代码的抄录,所以我们强调了您要设置「等宽字体」。

    实话说,什么字体都不设置,直接在导言区写一句: \lstset{basicstyle=\ttfamily} 就是可以的。在这里面的 \ttfamily 就是在告诉 lstlisting 环境「给我用等宽字体来显示源代码」。

    我对字体实在不太了解,设置字体只会 \setmainfont ,我会按照你们说的继续修改,至此十分感谢。

    再次强调, \setmainfont 设置的是「正文字体」, main 对应「正文」,您都指定正文字体为 Courier 了,页眉页脚作为正文的一部分当然会变成 Courier 啊。您想用 Courier,就要通过 \setmonofont 来声明, mono 对应「等宽」。 注意两个命令的拼写是不一样的!

    我在这里给出一个同时设置正文字体与等宽字体的例子,您在修改的时候,可以直接将您的代码在我的例子里扩充。重要的事情重复三遍:用了 fontspec 之后, 不要再用传统字体包!不要再用传统字体包!不要再用传统字体包!

    % 用 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\menlonumberstyle=\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}