formatting - 如何使用 LaTeX 以实用程序员系列丛书的风格格式化源代码列表?

当我阅读实用书架书籍时,它具有以下格式:

http://img210.imageshack.us/img210/2965/screenshot20100717at121.png
  • 如何使用 LaTeX 做到这一点?左侧的行号、着色的源代码和灰色的源名称。
  • 使用 LaTeX 列出源代码的工具有哪些?

最佳答案

用于在 LaTeX 中格式化源代码的包是 listings .在其手册 here 中查看它可以做什么.

这是我设法接近的距离:

标题中的文件名也是 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\lst@SkipOrPrintLabel{%
    \ifnum\lst@skipnumbers=\z@
        \global\advance\lst@skipnumbers-\lst@stepnumber\relax
        \lst@PlaceNumber
        \lst@numberfirstlinefalse
    \else
        \lst@ifnumberfirstline
            {\def\thelstnumber{Line \@arabic\c@lstnumber}\lst@PlaceNumber}%
            \lst@numberfirstlinefalse
        \else
            {\def\thelstnumber{-}\lst@PlaceNumber}%
        \fi
    \fi
    \global\advance\lst@skipnumbers\@ne}%
\def\lst@maketitle#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}

我喜欢 LaTeX。

https://stackoverflow.com/questions/3272479/

相关文章:

javascript - 并非所有浏览器都支持 toLocaleString()?

java - 是否可以使用 String.format 作为条件小数点?

c++ - ostream showbase 不显示 "0x"为零值

vim - 在 Vim 中轻松将函数参数重新格式化为多行

java - 如何在 IntelliJ 中停止流式 API 的扁平化

formatting - 抑制 Fortran 95 写入语句中的换行符

android - HTML.fromHtml 换行符消失

io - Fortran 将我的输出添加到星号 - 为什么?

floating-point - 如何格式化具有特定精度和前置零的 f32?

python - 将任意文本格式化为漂亮的 HTML 的 Python 模块的名称是什么?