I am creating a custom style for the lstlisting
package, with some custom colors for certain words. I am trying to define different "classes" of words, where each class has a certain property, like color, etc. Like this:
name: class1
Keywords: for, int, cout
style: red, underlined
name: class2
keywords: double, std
style: blue, bold
From what I was able to understand from other discussions, the \lstset
command has to be used, with the emph
keyword. I have two problems:
- How do I define multiple classes, where each has different keywords and properties?
- How do I highlight things like
#include
or<iostream>
, without clashing with other parts of the code (for example in loops where there is usually a<
) ?
What I have so far is the following:
\documentclass[a4paper]{article}\usepackage{listings}\usepackage{color}%Custom Colors\definecolor{plainText}{RGB}{74,131,31}\definecolor{comments}{RGB}{74,131,31}\definecolor{strings}{RGB}{180,54,34}\definecolor{numbers}{RGB}{44,45,211}\definecolor{keywords}{RGB}{160,49,158}\definecolor{preprocessorStatements}{RGB}{109,75,48}\definecolor{classNames}{RGB}{101,63,165}\lstset{frame=tb, language=C++, aboveskip=3mm, belowskip=3mm, showstringspaces=false, columns=flexible, basicstyle={\small\ttfamily}, numbers=left, numberstyle=\tiny\color{plainText}, keywordstyle=\color{keywords}, commentstyle=\color{comments}, stringstyle=\color{strings}, breaklines=true, breakatwhitespace=true tabsize=4}\begin{document}\lstinputlisting[tabsize=4]{main.cpp}\end{document}
As an example you can use this
#include <iostream>using namespace std;int main() { double var1 = 3.2; int var2 = 3; for (int i=0; i<var2; i++) { cout << i << endl; } return 0;}
Thanks in advance