2010-07-17 26 views
10

Khi tôi đọc sách Pragmatic Bookshelf, nó có dạng sau:Mã nguồn liệt kê một cái gì đó giống như các cuốn sách lập trình thực dụng với LaTeX?

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

  • Làm thế nào tôi có thể làm điều đó với LaTeX? Số dòng ở bên trái, tô màu mã nguồn và tên nguồn màu xám.
  • Các công cụ để liệt kê mã nguồn với LaTeX là gì?
+3

@Voter để đóng: Trên meta nó đã được xác định rằng LaTeX câu hỏi thuộc về SO. http://meta.stackexchange.com/questions/12918/can-we-have-a-ruling-on-latex-on-stackoverflow – Thomas

+1

Mặc dù Stack Overflow là một nơi hoàn toàn tốt để hỏi các câu hỏi của LaTeX hiện tại, có một đề xuất để thiết lập một trang web tương tự dành riêng cho TeX/LaTeX. Vui lòng [đi tới đây và "cam kết"] (http://area51.stackexchange.com/proposals/2148/tex-latex-and-friends?referrer=LtGW8MKLMIWU41zfK7oIpw2) nếu bạn tham gia vào một trang web như vậy. – ShreevatsaR

+1

@Thomas: người hỏi ai muốn latex q trên s.o. chấp nhận câu trả lời cho thấy latex q thuộc về s.o. = "nó được xác định trên meta ..." –

Trả lời

18

Gói mã định dạng để định dạng mã nguồn trong LaTeX là listings. Kiểm tra những gì nó có thể làm trong hướng dẫn sử dụng của nó here.

Đây là khoảng cách giữa tôi quản lý để có được:

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

Các tên tập tin từ các chú thích cũng là mục tiêu của liên kết Download. Xin lỗi về việc thiếu các góc tròn. Những người có thể có thể được thực hiện với TikZ.

Dưới đây là lời mở đầu:

\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 

Sau đó, câu chữ một danh sách trong cơ thể với:

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

Tôi yêu LaTeX.

+0

Nó khá tuyệt, nhưng như tôi đã cập nhật với câu hỏi của tôi. Bạn có thể xem xét tại sao phần phụ đề lại khác biệt? – prosseek

+0

Rất tiếc, tôi đã dán mã sai. Bạn nên sử dụng 'title' thay vì' caption'. Tôi chỉ hack '\ lst @ maketitle', chứ không phải' \ lst @ makecaption'. Đã chỉnh sửa. – Thomas

+1

Cảm ơn, nó hoạt động hoàn hảo. – prosseek

Các vấn đề liên quan