乳膠源代碼列表應如何生成相似於已知書籍的輸出,例如Spring Framework的輸出? 我嘗試過使用乳膠列表包,可是沒法生成看起來像下面那樣漂亮的東西。 所以,我對格式化指令感興趣,以生成相似下面的示例(來自Manning的Spring in Action 示例章節 ): html
編輯在TormodFjeldskår的幫助下,這裏有完整的片斷,能夠產生所需的外觀: java
\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}}
在文檔中使用它: python
\lstinputlisting[label=samplecode, caption=A sample]{sourceCode/HelloWorld.java}
您還能夠執行其餘一些操做,例如選擇新字體: shell
\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}
我想知道爲何沒人提到Minted包。 它具備比LaTeX列表包更好的語法突出顯示。 它使用Pygments 。 babel
$ pip install Pygments
LaTeX中的示例: app
\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}
結果以下: less
您須要使用帶有pdflatex命令的標誌-shell-escape
。 curl
有關更多信息,請訪問: https : //www.sharelatex.com/learn/Code_Highlighting_with_minted ide
對於我使用的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={} }
它看起來就像這樣
看一下algorithms
包,特別是algorithm
環境。
試試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}
您可能想要自定義它。 列表包有幾個參考。 只是谷歌他們。