提示:minted包有一個bug,見文末。。
---------
簡介(轉自: http://blog.houqp.me/minted-better-alternative-package-for-lstlisting/ )
------
曾幾何時,我一直在使用lstlisting宏包高亮代碼,鑒於我從來就沒在Word裡面享受過這樣的功能,我一直感到很滿意。直到今天我遇到了minted宏包。minted的[項目首頁]託管於Google Code。
使用非常的簡單,例如:
\begin{minted}{c}
printf("Hello World.");
\end{minted}
minted在後台調用的是pygmentize,所以輸出的結果非常漂亮,自然也就支援多種主題。minted提供很多可配置的參數,更多選項請參考[官方文檔]。
有一點要注意的是編譯時間需要加上-shell-escape參數。
------
安裝
minted的project home: http://code.google.com/p/minted/
主要按照上面文檔的介紹安裝就可以了。。 安裝時會要求安裝 minted.sty, 這裡有一個簡單的方法:
http://www.cnblogs.com/longdouhzt/archive/2012/10/11/2720045.html
安裝完minted.sty 就大功告成了~~
--------
使用
查看支援的語言: $ pygmentize -L lexers
如 C, NASM
注意用小寫 如\begin{minted}{nasm}
幾個可能有用的配置:
1. 背景:
\definecolor{bg}{rgb}{0.95,0.95,0.95}\begin{minted}[bgcolor=bg]{php}<?php echo "Hello, $x";?>\end{minted}
2. 行號
linenos (boolean)
(default: false)
Enables line numbers. In order to customize the display style of line numbers, you
need to redefine the \theFancyVerbLine macro:
\renewcommand{\theFancyVerbLine}{\sffamily\textcolor[rgb]{0.5,0.5,1.0}{\scriptsize\oldstylenums{\arabic{FancyVerbLine}}}}\begin{minted}[linenos,firstnumber=11]{python}def all(iterable): for i in iterable: if not i: return False return True\end{minted}
...以後再補充吧
---------------------------
最近用minted包發現一個bug:
Long listings are not splitted across pages.
即代碼太長,一頁放不下時,minted不會自動分頁,而是直接把超過一頁的部分截了。。囧。。
兩個可能的解決方案:
(1) 手動分頁:
http://tex.stackexchange.com/questions/51165/code-over-two-pages-and-continue-counting-lines
1 \begin{program}[H] 2 \begin{minted}[bgcolor=lightgray,linenos=true,fontfamily=courier,fontsize=\footnotesize,frame=single]{javascript} 3 \\Code Part 1 : numbers going from 1..28 4 \end{minted} 5 \caption{Same Caption} 6 \label{prog:samelabel} 7 \end{program} 8 9 \newpage10 11 \begin{program}[H]12 \ContinuedFloat13 \begin{minted}[bgcolor=lightgray,linenos=true,fontfamily=courier,fontsize=\footnotesize,frame=single,firstnumber=29]{javascript} 14 \\Code Part 2 change firstnumber value15 \end{minted}16 \caption{Same Caption}17 \label{prog:samelabel}18 \end{program}
(2) mdframed (沒試過)
http://tex.stackexchange.com/questions/23663/minted-truncates-the-code-if-it-doesnt-fit-into-one-page