4

Some keywords defined with the otherkeywords key are not formatted as expected when breaklines is set to true. Here is my MWE:

\documentclass[12pt, a4paper]{article}
\usepackage{listings}
\lstset{
  basicstyle=\itshape\ttfamily, 
  keywordstyle=\upshape,
}
\lstdefinestyle{syntax}{
  language=[ANSI]C,
  otherkeywords={(,)},
  breaklines=true,
}

\begin{document}
\begin{lstlisting}[style=syntax]
  const ( int | float ) text
\end{lstlisting}
\end{document}

The result looks something like this (but nicely monospaced...):

const ( int | float ) text

If I set breaklines=false, it looks as expected:

const ( int | float ) text

Am I missing something obvious? As for versions, "This is pdfTeX, Version 3.1415926-2.4-1.40.13 (MiKTeX 2.9)" on Windows (at work, they force me...).

jub0bs
  • 58,916
Mattias
  • 43
  • 2
  • Literate replacements and "other keywords" that involve the closing-parenthesis character ) are known to be affected by breaklines. See http://tex.stackexchange.com/questions/73795/problem-with-literate-and-breaklines-true-in-listings-package and its accepted answer for a workaround. – jub0bs Jun 23 '14 at 16:09
  • also related: http://tex.stackexchange.com/questions/172945/coloring-in-listing – jub0bs Jun 23 '14 at 16:15

1 Answers1

3

Based on https://tex.stackexchange.com/a/149203/36296

\documentclass{article}
\usepackage{listings}
\usepackage{etoolbox}

\lstset{
    basicstyle=\itshape\ttfamily, 
    keywordstyle=\upshape,
}

\lstdefinestyle{syntax}{
    language=[ANSI]C,
    breaklines=true,
    otherkeywords={(,)},
}

\makeatletter
\patchcmd{\lsthk@SelectCharTable}{)}{`}{}{} 
\makeatother 

\begin{document} 
    \begin{lstlisting}[style=syntax]        
const ( int | float ) text 
    \end{lstlisting}               
\end{document}

enter image description here