web-dev-qa-db-ja.com

プロの本のようにLaTeXのソースコードリスト

ラテックスソースコードのリストは、例えばSpring Frameworkのものなど、既知の本のようにどのように出力されるのでしょうか。私はラテックスリストのパッケージを試してみましたが、以下のように素敵に見えるものを作り出すことができませんでした。だから私は主に以下のサンプルのようなものを作り出すためのフォーマット指示に興味があります(Manningの サンプルの章 forSpring in Actionから):

From Manning's Spring in Action

EDIT特に TormodFjeldskå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}
320
Mork0075

本当に欲しいのは、キャプションの外観をカスタマイズすることです。これはcaptionパッケージを使って最も簡単にできます。このパッケージの使い方については マニュアル(PDF) をご覧ください。マニュアルの第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}

結果:

Preview

184

listingsパッケージに満足しています。

Listing example

私がそれをどのように構成するかです:

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

そして、あなたがしたことが何であれ、固定幅フォントを使うようにlistingsパッケージを設定してください(あなたの例のように、あなたはドキュメントにオプションを見つけるでしょう)。デフォルト設定では、グリッド上のプロポーショナルフォントのタイプセットが使用されます。これは、他の写真の回答からわかるように、私見のように、見苦しく見苦しく読めないものです。私は個人的には非常にプロポーショナルフォントでコードタイプセットを読まなければならないときにいらいらします。

これで固定幅フォントを設定してみてください。

\lstset{basicstyle=\ttfamily}
31
zvrba

どうして誰も Minted パッケージに言及していないのだろうか。 LaTeXのリスティングパッケージよりはるかに優れた構文の強調表示があります。 Pygments を使います。

$ pip install Pygments

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}

結果は次のとおりです。

enter image description here

pdflatexコマンドではフラグ-Shell-escapeを使用する必要があります。

詳細については: https://www.sharelatex.com/learn/Code_Highlighting_with_minted

22
Hans Ott

listingsパッケージを試してみてください。これは、色付きの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}

それをカスタマイズしたいと思うかもしれません。 listingsパッケージにはいくつかの参照があります。ただそれらをグーグル。

21
Markus Lux

algorithms パッケージ、特にalgorithm環境を見てください。

9
avakar

他にも、新しいフォントの選択など、できることがいくつかあります。

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

私が使用している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={}
}

そしてそれはまさにこのように見えます

enter image description here

2
pachamaltese