Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Using listings package, I am trying to write some LISP code. The issue is that LISP uses a lot of '-' in function name, and it makes the line width too large to break the boundary in two column mode.

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

Any good solution?

enter image description here

share|improve this question
    
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Apr 30 '13 at 2:57
3  
Try the option breaklines=true. –  Peter Grill Apr 30 '13 at 2:57
add comment

2 Answers

up vote 6 down vote accepted

use as basic style a monofont or translate the hyphen to a shorter one (my second example):

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

enter image description here

share|improve this answer
add comment

Adding the option breaklines=true allows for line breaking:

enter image description here

Notes:

Code:

\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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.