专业书籍中的 LaTeX 源代码列表 [英] LaTeX source code listing like in professional books

查看:27
本文介绍了专业书籍中的 LaTeX 源代码列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

latex 源代码列表应该如何产生类似于已知书籍中的输出,例如 Spring 框架的输出?我已经尝试使用乳胶列表包,但无法产生看起来像下面这样好的东西.所以我主要对格式说明感兴趣,以生成类似于下面的示例(来自 Manning 的 的帮助下,以下是生成所需外观的完整片段:

usepackage{listings}使用包裹{信使}lstset{basicstyle=footnotesize	tfamily, % 默认字体% numbers=left, % 行号位置numberstyle=	iny, % 行号样式% stepnumber=2, % 行号之间的边距numbersep=5pt, % 行号和文本之间的边距tabsize=2, % 选项卡大小扩展字符=真,breaklines=true, % 行将被换行关键字样式=颜色{红色},帧=b,% 关键字样式=[1]	extbf,% 关键字样式=[2]	extbf,% 关键字样式=[3]	extbf,% 关键字样式=[4]	extbf, sqrt{sqrt{}}stringstyle=color{white}	tfamily, % 字符串颜色展示空间=假,显示选项卡=假,xleftmargin=17pt,framexleftmargin=17pt,framexrightmargin=5pt,framexbottommargin=4pt,%背景颜色=颜色{浅灰色},显示字符串空间=假}lstloadlanguages{ % 检查文档以了解更多语言...% [视觉]基本,% 帕斯卡,% C,% C++,% XML,% HTML,爪哇}% DeclareCaptionFont{蓝色}{颜色{蓝色}}% captionsetup[lstlisting]{singlelinecheck=false, labelfont={blue}, textfont={blue}}使用包{标题}DeclareCaptionFont{白色}{颜色{白色}}DeclareCaptionFormat{listing}{colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{parbox{	extwidth}{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}

解决方案

在我看来,您真正想要的是自定义字幕的外观.使用 caption 包最容易做到这一点.有关如何使用此包的说明,请参阅 手册(PDF).您可能需要创建自己的自定义字幕格式,如手册第 4 章所述.

用 MikTex 测试:

documentclass{report}使用包装{颜色}使用包{xcolor}使用包{列表}使用包{标题}DeclareCaptionFont{白色}{颜色{白色}}DeclareCaptionFormat{listing}{colorbox{gray}{parbox{	extwidth}{#1#2#3}}}captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}% 序言到此结束开始{文档}egin{lstlisting}[label=some-code,caption=Some Code]这里的公共无效(){去().the().code()}end{lstlisting}结束{文档}

结果:

How should a latex source code listing look like to produce an output like in known books, for example one for the Spring Framework? I've tried with the latex listings package but wasn't able to produce something that looked as nice as the one below. So I'm primarely interested in the formatting instructions to produce something like the sample below (from Manning's sample chapter for Spring in Action):

EDIT With the help especially of Tormod Fjeldskår here's the complete snippet to produce the desired look:

usepackage{listings}
usepackage{courier}
lstset{
    basicstyle=footnotesize	tfamily, % Default font
    % numbers=left,              % Location of line numbers
    numberstyle=	iny,          % 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]	extbf,
    % keywordstyle=[2]	extbf,
    % keywordstyle=[3]	extbf,
    % keywordstyle=[4]	extbf,   sqrt{sqrt{}}
    stringstyle=color{white}	tfamily, % 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{	extwidth}{hspace{15pt}#1#2#3}}}
captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

Use it with this in your document:

lstinputlisting[label=samplecode, caption=A sample]{sourceCode/HelloWorld.java}

解决方案

It seems to me that what you really want, is to customize the look of the captions. This is most easily done using the caption package. For instructions how to use this package, see the manual (PDF). You would probably need to create your own custom caption format, as described in chapter 4 in the manual.

Edit: Tested with MikTex:

documentclass{report}

usepackage{color}
usepackage{xcolor}
usepackage{listings}

usepackage{caption}
DeclareCaptionFont{white}{color{white}}
DeclareCaptionFormat{listing}{colorbox{gray}{parbox{	extwidth}{#1#2#3}}}
captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

% This concludes the preamble

egin{document}

egin{lstlisting}[label=some-code,caption=Some Code]
public void here() {
    goes().the().code()
}
end{lstlisting}

end{document}

Result:

这篇关于专业书籍中的 LaTeX 源代码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆