Listings for OCaml and quotes

In this particular case you can use the literate option which doesn't break with double-quoted strings or other highlighting:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
%\usepackage[scaled]{beramono}
\usepackage{listings}
\usepackage{xcolor}

\lstset{
 language=caml,
 columns=[c]fixed,
% basicstyle=\small\ttfamily,
 keywordstyle=\bfseries,
 upquote=true,
 commentstyle=,
 breaklines=true,
 showstringspaces=false,
 stringstyle=\color{blue},
 literate={'"'}{\textquotesingle "\textquotesingle}3
}

\begin{document}
 \begin{lstlisting}
 type 'a t = ..
 let double_quote = "foo"
 let double_quote = '"'
 let double_quote = 'a'
 let double_quote = "'"
 let broken_highlight = ()
 \end{lstlisting}
\end{document}

enter image description here


You could consider switching to minted, which handles this input correctly. There is a black and white style available with the option style=bw.

MWE:

\documentclass{article}
\usepackage{minted}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[scaled]{beramono}

\begin{document}
\begin{minted}{ocaml}
type 'a t = { name : string; mutable info : 'a};;
let p = { name = "John"; info = 23 };;
let double_quote = '"'
let broken_highlight = ()
\end{minted}

\begin{minted}[style=bw]{ocaml}
type 'a t = { name : string; mutable info : 'a};;
let p = { name = "John"; info = 23 };;
let double_quote = '"'
let broken_highlight = ()
\end{minted}

\end{document}

Result:

enter image description here

Tags:

Listings