# How to typeset - - with lstlisting package?

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?

-

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)

-

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}

-
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

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}

-