13

I want to insert some C++ code into my thesis and I tried tons of options to make the code look like in my editor. Here is what I have so far:

\usepackage{listings}
\usepackage{color}

\definecolor{mygray}{rgb}{0.4,0.4,0.4}
\definecolor{mygreen}{rgb}{0,0.8,0.6}
\definecolor{myorange}{rgb}{1.0,0.4,0}

\lstset{
basicstyle=\footnotesize\sffamily\color{black},
commentstyle=\color{mygray},
frame=single,
numbers=left,
numbersep=5pt,
numberstyle=\tiny\color{mygray},
keywordstyle=\color{mygreen},
showspaces=false,
showstringspaces=false,
stringstyle=\color{myorange},
tabsize=2
}

Here is a code snippet:

\begin{lstlisting}[language=C++]{Name=test2}
/*
 * Project: ATFOC -> Autonomous Trainer For Object Classification
 * Name:    Main.cc 
 * Autor:   Bla Blub
 * Date:    03.09.2013
 * Description: main class for 
 *          1) executing a web search for images and store them
 *          2) and image comparison to filter good from bad samples
 */

#include "stdafx.h"

#include "WebSearch.h"
#include "ImageComparison.h"

using namespace std;

#define NUMBER_OF_IMAGES 50 // default
#define NUMBER_OF_QUERIES 3 // default

int main(int argc, char** argv)
{
    vector<string> input_vector;
    string input;

    uint i;

 \end{lstlisting}

Here you see what it looks like:

OK, so now my problem is that the preprocessor directives are colored like the other keywords, my question: How can I change that?

Another problem is that the basic datatypes (int, char...) have the same color as standard keywords (for, else, return...), how can I change that?

Thanks in advance for any solution if there is one.

EDIT: If it's possible, I would also want the colored words to either be shadowed or somehow bordered (I mean that the letters have a thin black border and are filled with the respective color) to make them better visible.

1
  • I did not know that this is even possible with listing!
    – wsaleem
    Commented Oct 23, 2019 at 5:43

1 Answer 1

11

For the directive style, use directivestyle.

For the C++ types, you can hack the color by emphasizing with emph and setting the style with emphstyle.

\begin{lstlisting}[language=C++,
                   directivestyle={\color{black}}
                   emph={int,char,double,float,unsigned},
                   emphstyle={\color{blue}}
                  ]
% ...
\end{lstlisting}
2
  • Works perfectly, thanks a lot! (Do you know something for my "EDIT"? :) ) I would've voted for your question, but i don't have enough rep.
    – Auberotte
    Commented Oct 9, 2013 at 0:45
  • You're welcome. I don't think it's possible to shadow nor border keywords in listings, but anyone can correct me.
    – juliohm
    Commented Oct 9, 2013 at 1:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .