乳胶源代码列表应如何生成类似于已知书籍的输出,例如Spring Framework的输出?我能够 生产环境 出与下面相似的东西 . 所以我在Spring的行动中为'm primarely interested in the formatting instructions to produce something like the sample below (from Manning' s sample chapter:
EDIT 在Tormod Fjeldskår的帮助下,这里有完整的片段来产生所需的外观:
\usepackage{listings}
\usepackage{courier}
\lstset{
basicstyle=\footnotesize\ttfamily, % Default font
% numbers=left, % Location of line numbers
numberstyle=\tiny, % Style of line numbers
% stepnumber=2, % Margin between line numbers
numbersep=5pt, % Margin between line numbers and text
tabsize=2, % Size of tabs
extendedchars=true,
breaklines=true, % Lines will be wrapped
keywordstyle=\color{red},
frame=b,
% keywordstyle=[1]\textbf,
% keywordstyle=[2]\textbf,
% keywordstyle=[3]\textbf,
% keywordstyle=[4]\textbf, \sqrt{\sqrt{}}
stringstyle=\color{white}\ttfamily, % Color of strings
showspaces=false,
showtabs=false,
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
% backgroundcolor=\color{lightgray},
showstringspaces=false
}
\lstloadlanguages{ % Check documentation for further languages ...
% [Visual]Basic,
% Pascal,
% C,
% C++,
% XML,
% HTML,
Java
}
% \DeclareCaptionFont{blue}{\color{blue}}
% \captionsetup[lstlisting]{singlelinecheck=false, labelfont={blue}, textfont={blue}}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
在文档中使用它:
\lstinputlisting[label=samplecode, caption=A sample]{sourceCode/HelloWorld.java}
8 回答
在我看来,你真正想要的是定制字幕的外观 . 使用
caption
包最容易完成 . 有关如何使用此程序包的说明,请参见the manual (PDF) . 您可能需要创建自己的自定义 Headers 格式,如手册第4章所述 .Edit: 使用MikTex测试:
Result:
我很满意
listings
包:这是我配置它的方式:
我这样使用它:
并且,无论你做什么,配置列表包使用固定宽度字体(如在您的示例中;您将在文档中找到该选项) . 默认设置在网格上使用比例字体排版,即恕我直言,令人难以置信的难看和难以理解,从其他答案与图片可以看出 . 当我必须阅读比例字体中的一些代码排版时,我个人非常恼火 .
尝试使用以下方法设置固定宽度字体:
试试
listings
包 . 这是我前一段时间使用彩色Java列表的例子:您可能想要自定义它 . 列表包有几个参考 . 只是谷歌他们 .
我想知道为什么没有人提到Minted包 . 它具有比LaTeX列表包更好的语法突出显示 . 它使用Pygments .
LaTeX中的示例:
结果如下:
You need to use the flag -shell-escape with the pdflatex command.
有关更多信息:https://www.sharelatex.com/learn/Code_Highlighting_with_minted
看看algorithms包,尤其是
algorithm
环境 .您还可以执行其他一些操作,例如选择新字体:
对于我使用的R代码
它看起来就像这样