1

I am reproducing C code style with listings. I have defined "" to be a string, so when I add libraries as "ff.h" it is treated as a string and is printed blue. To avoid that, I use literate.

An example of this:

\documentclass[11pt,fleqn]{article} 

\usepackage[dvipsnames]{xcolor} 

\usepackage{listings}

\lstdefinestyle{styleC}{
  language = C,
  commentstyle = {\color{ForestGreen}},
  stringstyle = {\color{NavyBlue}},
  comment = [l]{//},
  morecomment = [s]{/*}{*/},
  morestring = [b]',
  morestring = [b]",   
}

\lstnewenvironment{C}{
  \lstset{
    style=styleC,
    frame=single,
    literate = {"ff.h"}{"ff.h"}6 {"math.h"}{"math.h"}8 
    }
  }
  {}
  
  
\begin{document}
  
\begin{C}
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <complex.h>
#include "ff.h"
#include "ffconf.h"
#include "math.h"
#include "arm_math.h"
#include "audioMoth.h"
\end{C}

\end{document}

with a correct result: enter image description here

But when I applied this to all libraries:

literate = {"ff.h"}{"ff.h"}6 {"ffconf.h"}{"ffconf.h"}10 {"math.h"}{"math.h"}8   {"arm_math.h"}{"arm_math.h"}12 {"audioMoth.h"}{"audioMoth.h"}13 

the result is the following:

enter image description here

What could be happening?

2

You must use braces around the numbers if they get larger than 9:

\documentclass[11pt,fleqn]{article}

\usepackage[dvipsnames]{xcolor}

\usepackage{listings}

\lstdefinestyle{styleC}{
  language = C,
  commentstyle = {\color{ForestGreen}},
  stringstyle = {\color{NavyBlue}},
  comment = [l]{//},
  morecomment = [s]{/*}{*/},
  morestring = [b]',
  morestring = [b]",
}

\lstnewenvironment{C}{
  \lstset{
    style=styleC,
    frame=single,
    %literate = {"ff.h"}{"ff.h"}6 {"math.h"}{"math.h"}8
    literate = {"ff.h"}{"ff.h"}6 {"ffconf.h"}{"ffconf.h"}{10} {"math.h"}{"math.h"}8   {"arm_math.h"}{"arm\_math.h"}{12} {"audioMoth.h"}{"audioMoth.h"}{13}
    }
  }
  {}


\begin{document}

\begin{C}
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <complex.h>
#include "ff.h"
#include "ffconf.h"
#include "math.h"
#include "arm_math.h"
#include "audioMoth.h"
\end{C}

\end{document}
3
  • Thanks! That was the problem. To anyone that reads this question, it is neccesary to change to {"arm_math.h"}{"arm{\_}math.h"}{12} too in order to add the underscore symbol. – car00 Aug 23 '20 at 9:34
  • I did changed the underscore in my code ;-) – Ulrike Fischer Aug 23 '20 at 9:39
  • Upss, true!! I didn't realize that. Thanks again :) – car00 Aug 23 '20 at 9:43

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.