![使用 listing 包以双列模式记录 LISP 代码](https://linux22.com/image/267498/%E4%BD%BF%E7%94%A8%20listing%20%E5%8C%85%E4%BB%A5%E5%8F%8C%E5%88%97%E6%A8%A1%E5%BC%8F%E8%AE%B0%E5%BD%95%20LISP%20%E4%BB%A3%E7%A0%81.png)
使用listings
包,我尝试编写一些 LISP 代码。问题是 LISP 在函数名称中使用了大量“-”,这导致行宽过大而无法在双列模式下打破边界。
\lstset{numbers=left, numberstyle=\tiny, stepnumber=1,firstnumber=1,
numbersep=5pt,language=Lisp,
stringstyle=\ttfamily,
basicstyle=\footnotesize,
showstringspaces=false
}
\begin{lstlisting}[firstnumber=1, caption=Getting labels, label=glabels]
(defun find-symbol-between-sigma-sets (sigma-a sigma-b scan-node symbols g)
(let* ((scan-node-set-in-sigma-a (find-scan-node sigma-a scan-node))
(next-scan-node-set-in-sigma-b
(get-next-node-set-from-scan-node-set
scan-node-set-in-sigma-a sigma-b g))
(union-node-set-in-sigma-a
(get-previous-nodes next-scan-node-set-in-sigma-b g)))
(if
(and
(memberp union-node-set-in-sigma-a scan-node-set-in-sigma-a)
(path-exists-between-set-a-and-set-b
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b g))
(car
(unique-get-symbols
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b symbols))
nil)))
\end{lstlisting}
有什么好的解决办法吗?
答案1
使用单字体作为基本样式或将连字符转换为更短的连字符(我的第二个示例):
\documentclass[twocolumn]{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{listings}
\lstset{numbers=left,numberstyle=\tiny,numbersep=5pt,language=Lisp,
stringstyle=\ttfamily\small,basicstyle=\ttfamily\footnotesize,
showstringspaces=false,breaklines}
\begin{document}
\begin{lstlisting}[caption=Getting labels, label=glabels]
(defun find-symbol-between-sigma-sets (sigma-a sigma-b scan-node symbols g)
(let* ((scan-node-set-in-sigma-a (find-scan-node sigma-a scan-node))
(next-scan-node-set-in-sigma-b
(get-next-node-set-from-scan-node-set
scan-node-set-in-sigma-a sigma-b g))
(union-node-set-in-sigma-a
(get-previous-nodes next-scan-node-set-in-sigma-b g)))
(if
(and
(memberp union-node-set-in-sigma-a scan-node-set-in-sigma-a)
(path-exists-between-set-a-and-set-b
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b g))
(car
(unique-get-symbols
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b symbols))
nil)))
\end{lstlisting}
\newpage
\begin{lstlisting}[caption=Getting labels,
basicstyle=\footnotesize,literate={-}{{-}}1]
(defun find-symbol-between-sigma-sets (sigma-a sigma-b scan-node symbols g)
(let* ((scan-node-set-in-sigma-a (find-scan-node sigma-a scan-node))
(next-scan-node-set-in-sigma-b
(get-next-node-set-from-scan-node-set
scan-node-set-in-sigma-a sigma-b g))
(union-node-set-in-sigma-a
(get-previous-nodes next-scan-node-set-in-sigma-b g)))
(if
(and
(memberp union-node-set-in-sigma-a scan-node-set-in-sigma-a)
(path-exists-between-set-a-and-set-b
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b g))
(car
(unique-get-symbols
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b symbols))
nil)))
\end{lstlisting}
\end{document}
答案2
添加选项breaklines=true
允许换行:
笔记:
- 包裹
showframe
仅用于显示页边距。
代码:
\documentclass{article}
\usepackage{showframe}
\usepackage{listings}
\lstset{numbers=left, numberstyle=\tiny, stepnumber=1,firstnumber=1,
numbersep=5pt,language=Lisp,
stringstyle=\ttfamily,
basicstyle=\footnotesize,
showstringspaces=false,
breaklines=true,
}
\begin{document}
\begin{lstlisting}[firstnumber=1, caption=Getting labels, label=glabels]
(defun find-symbol-between-sigma-sets (sigma-a sigma-b scan-node symbols g)
(let* ((scan-node-set-in-sigma-a (find-scan-node sigma-a scan-node))
(next-scan-node-set-in-sigma-b
(get-next-node-set-from-scan-node-set
scan-node-set-in-sigma-a sigma-b g))
(union-node-set-in-sigma-a
(get-previous-nodes next-scan-node-set-in-sigma-b g)))
(if
(and
(memberp union-node-set-in-sigma-a scan-node-set-in-sigma-a)
(path-exists-between-set-a-and-set-b
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b g))
(car
(unique-get-symbols
union-node-set-in-sigma-a next-scan-node-set-in-sigma-b symbols))
nil)))
\end{lstlisting}
\end{document}