2009-02-25 46 views
130

我目前正在編寫一份項目報告,並解釋了我使用過的稍微瘋狂的非直觀代碼,我需要在代碼中加入很多很多很短的摘錄內容。在LaTeX中查看代碼*很好*

我怎樣才能獲得代碼轉換成LaTeX的是:

  • 看起來不錯
  • 如果線路太長(名單lstlisting或逐字)不熄滅頁面的側
  • 最好與文本的其餘部分一致

編輯(想我會添加設置,以便人們不必嘗試和數字出來自己(從維基採取(鏈接進一步下跌),並編輯了美好的事物))

尼斯設置:

\usepackage{color} 
\usepackage{listings} 
\lstset{ % 
language=C++,    % choose the language of the code 
basicstyle=\footnotesize,  % the size of the fonts that are used for the code 
numbers=left,     % where to put the line-numbers 
numberstyle=\footnotesize,  % the size of the fonts that are used for the line-numbers 
stepnumber=1,     % the step between two line-numbers. If it is 1 each line will be numbered 
numbersep=5pt,     % how far the line-numbers are from the code 
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color} 
showspaces=false,    % show spaces adding particular underscores 
showstringspaces=false,   % underline spaces within strings 
showtabs=false,     % show tabs within strings adding particular underscores 
frame=single,   % adds a frame around the code 
tabsize=2,   % sets default tabsize to 2 spaces 
captionpos=b,   % sets the caption-position to bottom 
breaklines=true,  % sets automatic line breaking 
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace 
escapeinside={\%*}{*)}   % if you want to add a comment within your code 
} 

\begin{lstlisting} 
!!code!! 
\end{lstlisting} 

這些設置的一個例子:

+0

lstlisting * can * do換行,您只需要使用正確的lstset命令。 – 2009-02-25 17:34:12

回答

41

listings包是相當不錯的,很柔ible(例如不同大小的評論和代碼)。

2

對於簡單的文檔,我有時使用逐字,但列表對於大塊代碼來說很不錯。

17

事實證明,lstlisting能夠很好地格式化代碼,但需要大量的調整。

Wikibooks有一個很好的示例,可以調整參數。