LaTeX源代码与专业书籍中的类似

LaTeX源代码与专业书籍中的类似,latex,Latex,latex源代码清单应该如何生成类似于已知书籍的输出,例如Spring框架的输出?我试过使用latex listings软件包,但无法生成与下面一样好的东西。因此,我对生成以下示例(来自Manning's for Spring in Action)的格式说明非常感兴趣: 编辑 以下是生成所需外观的完整片段: \usepackage{listings} \usepackage{courier} \lstset{ basicstyle=\footnotesize\ttfamily, % D

latex源代码清单应该如何生成类似于已知书籍的输出,例如Spring框架的输出?我试过使用latex listings软件包,但无法生成与下面一样好的东西。因此,我对生成以下示例(来自Manning's for Spring in Action)的格式说明非常感兴趣:

编辑 以下是生成所需外观的完整片段:

\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}

请查看软件包,尤其是
算法
环境。

尝试一下
清单
软件包。下面是我不久前用来制作彩色Java列表的一个示例:

\usepackage{listings}

[...]

\lstset{language=Java,captionpos=b,tabsize=3,frame=lines,keywordstyle=\color{blue},commentstyle=\color{darkgreen},stringstyle=\color{red},numbers=left,numberstyle=\tiny,numbersep=5pt,breaklines=true,showstringspaces=false,basicstyle=\footnotesize,emph={label}}

[...]

\begin{lstlisting}
public void here() {
    goes().the().code()
}

[...]

\end{lstlisting}

您可能需要自定义它。清单包中有几个参考资料。只要用谷歌搜索一下就可以了。

我对
清单
软件包很满意:

以下是我如何配置它:

\lstset{
language=C,
basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
frame=tb,
columns=fullflexible,
showstringspaces=false
}
我是这样使用它的:

\begin{lstlisting}[caption=Caption example.,
  label=a_label,
  float=t]
// Insert the code here
\end{lstlisting}

不管您做什么,请将listings包配置为使用固定宽度字体(如您的示例所示;您将在文档中找到该选项)。默认设置使用网格上的比例字体排版,这是非常难看和不可读的,这可以从其他带有图片的答案中看出。当我必须阅读一些按比例字体排版的代码时,我个人非常恼火

尝试使用以下选项设置固定宽度字体:

\lstset{basicstyle=\ttfamily}

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

编辑:使用MikTex测试:

\documentclass{report}

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

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

% This concludes the preamble

\begin{document}

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

\end{document}
结果:


您还可以做其他几件事,例如选择新字体:

\documentclass[10pt,a4paper]{article}
% ... lots of packages e.g. babel, microtype, fontenc, inputenc &c.
\usepackage{color}    % Leave this out if you care about B/W printing, obviously.
\usepackage{upquote}  % Turns curly quotes in verbatim text into straight quotes. 
                      % People who have to copy/paste code from the PDF output 
                      % will love you for this. Or perhaps more accurately: 
                      % They will not hate you/hate you less.
\usepackage{beramono} % Or some other package that provides a fixed width font. q.v.
                      % http://www.tug.dk/FontCatalogue/typewriterfonts.html
\usepackage{listings} 
\lstset {                 % A rudimentary config that shows off some features.
    language=Java,
    basicstyle=\ttfamily, % Without beramono, we'd get cmtt, the teletype font.
    commentstyle=\textit, % cmtt doesn't do italics. It might do slanted text though.
    \keywordstyle=        % Nor does cmtt do bold text.
        \color{blue}\bfseries,
    \tabsize=4            % Or whatever you use in your editor, I suppose.
}
\begin{document} 
\begin{lstlisting}
public final int ourAnswer() { return 42; /* Our final answer */ }
\end{lstlisting} 
\end{document}

我想知道为什么没有人提到这个包裹。它具有比LaTeX清单包更好的语法突出显示。它使用

乳胶中的示例:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{minted}

\begin{document}
\begin{minted}{python}
import numpy as np

def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable

    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1) 

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;

                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)

                VT = np.zeros((n*m,1), int)

    return M
\end{minted}
\end{document}
其结果是:

您需要在pdflatex命令中使用标志
-shell escape

有关更多信息:

对于我使用的R代码

\usepackage{listings}
\lstset{
language=R,
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\color{gray},
numbers=left,
numberstyle=\ttfamily\color{gray}\footnotesize,
stepnumber=1,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=b,
breaklines=true,
breakatwhitespace=false,
title=\lstname,
escapeinside={},
keywordstyle={},
morekeywords={}
}
看起来就像这样



谢谢。在我的讨论中,这个软件包似乎在理论上非常强大,我从很多数学书中都知道。但是我不会太强调这一点(prerequsites,if,else),我希望有一个像上面那样的格式。我只是在谈论
算法
环境,而不是
算法
<代码>算法是一个浮动容器,看起来很不错。你可以把你想要的任何东西放进去,即使是elsethread提到的
清单也可以。谢谢。我仍然知道清单软件包,但无法像我的示例那样格式化。这才是真正的问题,请更准确一点。对我来说,我发布的列表“看起来像专业书籍中的”和“看起来像你发布的一样好”。请使用以屏幕截图形式发布的示例作为我要存档的结果。为了完整性起见,您可能需要将\usepackage{color}添加到您发布的文本中。我花了一点时间才发现它不见了。干得好!我不得不添加\usepackage{caption}和\usepackage{graphics},但它似乎转换了单引号。您好,我必须将源文件放在哪里?在您的示例Hello.javaI中,我只想为\lsInputListing部分中的内容重新定义标题格式(类似于myCaption)。你知道怎么做吗?试试/captionsetup[lstlisting]{your options}这很好用,谢谢。你知道如何实现标题背后的灰色背景吗(就像我最初的帖子示例一样)?在文档中找不到任何内容。这看起来不错,但我的标题框缩进了(不是文本,而是框本身)。我不知道为什么,因为列表也没有缩进。如果有人想把列表的背景也涂上颜色,你会注意到标题比列表宽。在这里寻找解决方案。我个人使用columns=fullflexible和basicstyle=\small\sffamily,就像我上面发布的示例一样。这些字符没有垂直对齐,但我认为它们看起来比使用\ttfamily更好。你觉得我上面贴的样品难看吗?你的例子看起来不错。但是,我不喜欢嵌套复合语句,因为适当的缩进(列对齐)对查看复合语句({}块)的范围有很大帮助。我也在想同样的事情,但到目前为止,我所有的列表看起来都不错。嵌套复合语句是一种转移视线的方法。因为缩进都是由空格组成的,所以无论其他字符的宽度如何,缩进都会排成一行。@lamba:如果我没记错的话,它会告诉Latex把它放在页面的顶部。呃,比例字体的列表太难看了。(另外,由于文化原因,有些人(至少很多日本人,也许还有其他亚洲人)很难读懂。)@mirabilos:是的,我想我后来改了。在这个清单上看起来还可以,但在其他缩进/嵌套较多的清单上就完全不行了。我认为在\keywordstyle和\tabsize中,应该删除反斜杠,因为这样做行不通。不过还是很有帮助的+1.Minted是LaTeX中排版源代码的软件包。它不仅易于使用、功能丰富且文档丰富,而且在源代码中使用Unicode字符时也没有问题(不像
清单
)。
\usepackage{listings}
\lstset{
language=R,
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\color{gray},
numbers=left,
numberstyle=\ttfamily\color{gray}\footnotesize,
stepnumber=1,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=b,
breaklines=true,
breakatwhitespace=false,
title=\lstname,
escapeinside={},
keywordstyle={},
morekeywords={}
}