# Why are my custom settings for listings not taken into account?

I would like some help to get my source code to look correct. The code is supposed to look like this:

As you can see, some words should be purple and bold, some should just be bold, some should be blue and the comments should be green. I'm using the listings package for this. See my minimal working example below.

However, for some reason, declaring new keywords using keywordstyle=[2]{...} and keywordstyle=[3]{...} seems to have no effect. The new keywords are printed like normal text. What am I doing wrong?

\documentclass[12pt,a4paper]{report}

\usepackage{listings}
\usepackage{color}

\definecolor{mygreen}{rgb}{0,0.5,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{byzantium}{rgb}{0.44, 0.16, 0.39}

\lstset{
basicstyle=\footnotesize\ttfamily,
numbers=left,
numberstyle=\color{mygray},
keywordstyle=\bfseries\color{byzantium},
keywordstyle=[2]\bfseries,
keywordstyle=[3]\color{blue},
morekeywords = {},
keywords=[2]{ePWM_config()},
keywords=[3]{"DSP28x_Project.h",PCLKCR0,bit,TBCLKSYNC},
title=ePWM.c
}

\begin{document}

\begin{lstlisting}
static void ePWM_config()
{
// Setup Time Base Clock ePWM1
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
\end{lstlisting}

\end{document}

• You can additional 'keywords' and assign a style (color) to them. – user31729 Apr 22 '14 at 7:09
• How do I assign a different style to only particular keywords? – Christian Apr 22 '14 at 7:22
• I know that you can add more keywords by writing "morekeywords={newkeyword}" but not how to set a different style to those keywords. – Christian Apr 22 '14 at 7:25
• Edited my original post. – Christian Apr 22 '14 at 8:32
• You should probably replace "look correct" by "look like in eclipse". – Martin Thoma Apr 22 '14 at 9:37

There are at least four issues with your listings settings.

1. You load the C language too late. Loading a language discards most of the listings tweaks that were performed (using \lstset) beforehand. You must load the language first, and only then customise the appearance. I recommend you define your own style, for convenience.

2. You're trying to define things like "DSP28x_Project.h" as keywords. This is awkward because listings doesn't allow (by default) " in identifiers, and because the C language uses " as string delimiter. If what you want is to highlight C string literals in blue, you should simply tell listings what of your prefered string style is:

stringstyle = \color{blue},

3. You're trying to define things like ePWM_config() as keywords. By default, listings does not allow parentheses to be part of identifiers, for good reason. There are cases where you might want to override that behaviour (see this answer for an example), but that's unwarranted here. Moreover, parentheses don't seem to be in boldface in your screenshot. Simply define ePWM_config (not ePWM_config()) as a keyword.

4. You use a font family that does not come with a boldface typewriter font. TeX font families, such as Computer Modern (which is used by default in LaTeX), do not provide a boldface typewriter font. When listings attempts to typeset stuff in bold typewriter, TeX complains that no such font is available and uses the "normal" (i.e. medium-series) typewriter font instead. To remedy that problem, you must use a font family that comes with boldface typewriter font. A popular choice is Bera Mono. Inserting the following lines in your preamble tells LaTeX to use Bera Mono as typewriter font throughout your LaTeX document.

\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}


Edit: if you want the typewriter font to be Bera Mono only in your listings, not in the rest of the document, use something like this:

basicstyle = \footnotesize\def\fvm@Scale{.85}\fontfamily{fvm}\selectfont


## Complete code

\documentclass[12pt,a4paper]{report}

\usepackage{listings}
\usepackage{xcolor}
\usepackage[T1]{fontenc}

\definecolor{mygreen}{rgb}{0,0.5,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{byzantium}{rgb}{0.44, 0.16, 0.39}

\makeatletter
\lstdefinestyle{myCstyle}{
language       = C,
basicstyle     = \footnotesize\def\fvm@Scale{.85}\fontfamily{fvm}\selectfont,
numbers        = left,
numberstyle    = \color{mygray},
keywordstyle   = \bfseries\color{byzantium},
keywordstyle   =[2]\bfseries,
keywordstyle   =[3]\color{blue},
stringstyle    = \color{blue},
morekeywords   = {},
directivestyle = \color{violet}\bfseries,
morekeywords   =[2]{ePWM_config,ePWM},
morekeywords   =[3]{PCLKCR0,bit,TBCLKSYNC},
title          = ePWM.c,
}
\makeatother

\begin{document}
{
\ttfamily
This should be typeset in Computer Modern typewriter font,
contrary to C listings, which should be typeset in scaled-down Bera Mono font.
}

\begin{lstlisting}[style=myCstyle]
#include  "DSP28x_Project.h"

static void ePWM_config();

void ePWM()
{
// Stop the Time Base Clock
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;

// configure the ePWM
ePWM_config();

// Enable the Time Base Clock
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
}
\end{lstlisting}

\end{document}

• This worked. However, some of the keywords I have do not work. Such as "DSP28x_Project.h" and adc_isr(). Any idea on how to fix that? – Christian Apr 22 '14 at 12:38
• @Christian See my edit. – jub0bs Apr 22 '14 at 13:53
• Thanks a lot! Is there any way to switch to bera mono just in the code, though? I'm using times new roman in the rest of my thesis and I would prefer to continue doing so. – Christian Apr 22 '14 at 14:20
• Yes, there is. Wait for my next edit. – jub0bs Apr 22 '14 at 14:25
• Looks absolutely fantastic. Thanks for all your help. I really appreciate it! My thesis will look really good thanks to you. – Christian Apr 23 '14 at 8:24