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.

Consider this small example:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{listings} %for listings of the source code

\begin{document}

\lstset{language=sh}
\begin{lstlisting}
--   <-- Works!
a--b <-- Does not work!
-{}- <-- Won't help!
\end{lstlisting}

\end{document}

How to force lstlisting to typeset -- instead of a long dash always? Also, how to increase distance between two consecutive dashes?

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

You can use the option literate to modify the output:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings} %for listings of the source code

\begin{document}

\lstset{language=sh,literate={--}{{-\,-}}1}
\begin{lstlisting}
--   <-- Works!
a--b <-- Does not work!
-{}- <-- Won't help!
\end{lstlisting}

\end{document}

Furthermore you shouldn't use the option utf8x. An explanation you can read in the question utf8x vs. utf8 (inputenc)

share|improve this answer
add comment

use

\lstset{language=sh,basicstyle=\ttfamily}

and if you want to use bold chracters than use a font like Bera Mono or Luxi Mono!

\documentclass[10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[scaled=0.83]{beramono}
\usepackage{listings} %for listings of the source code
\lstset{basicstyle=\ttfamily}
\begin{document}

\lstset{language=sh}
\begin{lstlisting}
--    # Works!
a--b  # Works
if else fi # Keywords work too! 
\end{lstlisting}

\end{document}
share|improve this answer
    
You are right, but CM typewriter does not have bold or italic variants - see my answer –  Boris Dec 12 '11 at 18:45
    
sure and Courier is a "really not nice looking font ..." –  Herbert Dec 12 '11 at 20:08
    
Agreed, Courier is not nice. Bera Mono is better - especially for presentation slides, where it can be combined with Bera Sans for body text and Arev for math - my favorite sans combinations –  Boris Dec 12 '11 at 21:47
add comment

First, if you use long dashes for options, then this works:

a --b

Second, the choice of propotional fonts for listings is ugly. Unfortunately, CM typewriter does not have bold or italic variants, so just setting \lstset{basicstyle=\ttfamily} is not what you want. I prefer courier, which gives more satisfactory results:

\documentclass[10pt]{standalone}
\usepackage[utf8x]{inputenc}
\usepackage{courier}
\usepackage{listings} %for listings of the source code
\lstset{basicstyle=\ttfamily}
\begin{document}

\lstset{language=sh}
\begin{lstlisting}
--    # Works!
a--b  # Works
if else fi # Keywords work too! 
\end{lstlisting}

\end{document}

enter image description here

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.