2010-07-17 16 views
10

當我讀到務實書架的書,它具有以下格式:源代碼列表像LaTeX的實用程序員書籍一樣?

http://img210.imageshack.us/img210/2965/screenshot20100717at121.png

  • 我如何能做到這一點乳膠?左側的行號,着色源代碼和灰色源名稱。
  • LaTeX的源代碼清單工具是什麼?
+3

@Voter關閉:在meta上確定LaTeX問題屬於SO。 http://meta.stackexchange.com/questions/12918/can-we-have-a-ruling-on-latex-on-stackoverflow – Thomas 2010-07-17 17:17:39

+1

雖然堆棧溢出目前是一個很好的問LaTeX問題的地方,但有一個建議爲TeX/LaTeX建立一個類似的網站。如果你願意參加這樣的網站,請[去這裏和「提交」](http://area51.stackexchange.com/proposals/2148/tex-latex-and-friends?referrer=LtGW8MKLMIWU41zfK7oIpw2)。 – ShreevatsaR 2010-07-17 20:42:33

+1

@Thomas:提問者希望在s.o上使用乳膠q。接受一個認爲乳膠q屬於s.o的答案。 =「它是在meta上確定的......」 – 2010-07-17 22:57:25

回答

18

包在乳膠格式化的源代碼是listings。看看它在手冊here中能做些什麼。

這是我多麼接近設法:

The listing, as typeset in LaTeX http://i29.tinypic.com/wb8bdi.png

從字幕文件名也是Download鏈接的目標。對不起,缺乏圓角。這些可以用TikZ完成。

這裏的序言:

\usepackage{listings} 
\usepackage[T1]{fontenc} 
\usepackage[scaled]{beramono} 
\usepackage{tgadventor} 
\usepackage[usenames,dvipsnames]{color} 
\usepackage[colorlinks=true]{hyperref} 

\definecolor{lineno}{rgb}{0.5,0.5,0.5} 
\definecolor{code}{rgb}{0,0.1,0.6} 
\definecolor{keyword}{rgb}{0.5,0.1,0.1} 
\definecolor{titlebox}{rgb}{0.85,0.85,0.85} 
\definecolor{download}{rgb}{0.8,0.1,0.5} 
\definecolor{title}{rgb}{0.4,0.4,0.4} 

\lstset{ 
    language=Lisp, 
    basicstyle=\ttfamily\small\color{code}, 
    showspaces=false, 
    showstringspaces=false, 
    numbers=left, 
    firstnumber=1, 
    stepnumber=5, 
    numberfirstline=true, 
    numberstyle=\color{lineno}\sffamily\scriptsize, 
    keywordstyle=\color{keyword}\bfseries, 
    stringstyle=\itshape, 
    morekeywords={dosync,if}, 
    deletekeywords={alter} 
} 

\makeatletter 
\gdef\[email protected]{% 
    \ifnum\[email protected]=\[email protected] 
     \global\advance\[email protected]\[email protected]\relax 
     \[email protected] 
     \[email protected] 
    \else 
     \[email protected] 
      {\def\thelstnumber{Line \@arabic\[email protected]}\[email protected]}% 
      \[email protected] 
     \else 
      {\def\thelstnumber{-}\[email protected]}% 
     \fi 
    \fi 
    \global\advance\[email protected]\@ne}% 
\def\[email protected]#1{ 
    \vskip\abovecaptionskip 
    \colorbox{titlebox}{ 
     \scriptsize 
     \color{download}\ttfamily\href{http://example.com/#1}{Download} 
     \color{title}\sffamily\bfseries#1} 
    \vskip\belowcaptionskip} 
\makeatother 

然後,排版上的列表中體:

\begin{lstlisting}[title=examples/introduction.clj] 
(defn hello 
    "Writes hello message to *out*. Calls you by username. 
    Knows if you have been here before." 
    [username] 
    (dosync 
    (let [past-visitor (@visitors username)] 
     (if past-visitor 
     (str "Welcome back, " username) 
     (do 
      (alter visitors conj username) 
      (str "Hello, " username)))))) 
\end{lstlisting} 

我愛乳膠。

+0

這很酷,但我更新了我的問題。你能看看爲什麼標題部分有點不同嗎? – prosseek 2010-07-17 20:27:33

+0

對不起,我粘貼了錯誤的代碼。你應該使用'title'而不是'caption'。我只攻擊了'\ lst @ maketitle',而不是'\ lst @ makecaption'。編輯。 – Thomas 2010-07-17 20:32:23

+1

謝謝,它完美的作品。 – prosseek 2010-07-17 20:37:02

相關問題