[Tex/LaTex] Lyx code tag with highlighting for C

codehighlightinglistingslyx

I just installed Lyx and when I try to insert the program I observe that there is no syntax highlighting. What is the best way to allow for syntax highlighting in LYX?

Best Answer

I assume you need highlighted code in the PDF. When you do Insert --> Program listing, or if you insert a child document as a Program listing, it is the features of the listings package that is used. The look of all listings in a document can be modified under Document --> Settings --> Listings, where you can add any of the <key>=<value> pairs listed in the manual.

For example, to specify that the language is C, add language=C, to say that comments should be italic and yellow, add commentstyle=\color{yellow}\itshape, and so on. Again, the manual is the reference. (I've never really used it much myself.)

By adding something like

language=C,keywordstyle=\color{blue},
commentstyle=\color{magenta}\itshape,
emphstyle=\color{red},
breaklines,
basicstyle=\ttfamily,
stringstyle=\color{green},
identifierstyle=\color{cyan}

to the settings, this hello world from Wikipedia (with an added comment) turns out as shown below:

#include <stdio.h>

// comment!

int main(void)
{
    printf("hello, world\n");
}

enter image description here