[Tex/LaTex] Forbidding keywords in listings

listings

I would like to force the listings package to be more selective when highlighting keywords. I'm currently including C source files in my document, and while I'm happy that occurrences of the float keyword are properly highlighted, I'd like the following line to appear as

#include <float.h>

instead of the current

#include <float.h>

In a similar spirit, else is fine, but #else should be typeset instead of #else.

I tried solutions like using deletekeywords={float.}, but to no avail (unsurprisingly, according to the documentation: "(…) by default some characters are not allowed inside
keywords, for example ‘-’, ‘:’, ‘.’, and so on. (…)"). Playing with deletedirectives led nowhere either.

Best Answer

The keyword is float without dot, not float.:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=C, deletekeywords={float}]
#include <float.h>
\end{lstlisting}
\end{document}

Result

It is more difficult to have both. The literate feature seems to do the trick:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[
  language=C,
  literate={float.}{float.}6,
]
#include <float.h>
float a = 1.0;
\end{lstlisting}
\end{document}

Result

If you do not want a special formatting of directives, it can be disabled by an empty directivestyle:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[
  language=C,
  directivestyle={},
  literate={float.}{float.}6,
  columns=flexible,
]
#include <float.h>
float a = 1.0;
#ifdef foo
#else
#endif
\end{lstlisting}
\end{document}

Result

Related Question