3

I have a snippet of code I'm putting into a paper written in LaTeX.

I don't want it to highlight any of the keywords that start with a # sign, such as #define, or #pragma. Here is a MWE of what I have right now:

\documentclass[10pt,journal,compsoc]{IEEEtran}

\newcommand{\us}{\char`_}
\usepackage{listings}
\usepackage{color}
\usepackage{float}
\lstdefinestyle{cpp_onecol}{
    language=C++,
    basicstyle=\small\ttfamily,
    breaklines=true,
    morekeywords={CHECK},
    keywordstyle=\color{black}\bfseries\itshape,
    commentstyle=\color{black}\bfseries,
    alsoother={\#},
    deletekeywords ={\#pragma}
}
\begin{document}

\begin{figure}
\begin{lstlisting}[style=cpp_onecol]
#pragma hello
#define I_DONT_LIKE_POUNDSIGN 1

int a;
int b;
volatile int c;   

\end{lstlisting}
\begin{lstlisting}[caption=Stupid hashtag.]
\end{lstlisting}
\end{figure} 

\end{document}

This generates:

Pound sign still highlighting.

I have tried searching for a solution, but I think the # is ruining my search results too. The only one I found suggested adding the alsoother={\#}, but that didn't really help at all.

2

I figured it out. I added a deletedelim with a pound sign.

Here is the MWE:

\documentclass[10pt,journal,compsoc]{IEEEtran}

\newcommand{\us}{\char`_}
\usepackage{listings}
\usepackage{color}
\usepackage{float}
\lstdefinestyle{cpp_onecol}{
    language = C++,
    basicstyle=\small\ttfamily,
    breaklines=true,
    morekeywords={CHECK},
    keywordstyle=\color{black}\bfseries\itshape,
    commentstyle=\color{black}\bfseries,
    alsoother={\#},
    deletekeywords ={\#pragma},
    deletedelim=*[directive]\#
}
\begin{document}

\begin{figure}
\begin{lstlisting}[style=cpp_onecol]
#pragma hello
#define I_DONT_LIKE_POUNDSIGN 1

int a;
int b;
volatile int c;  

// Hello! 

\end{lstlisting}
\begin{lstlisting}[caption=Stupid hashtag.]
\end{lstlisting}
\end{figure} 

\end{document}

And the result:

Fixed it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.