6

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?

  • LyX is a LaTeX code editor, but I think it's not designed for program listings in other languages. Or do you want to use a listing inside a (La)TeX document? – user31729 Mar 6 '15 at 12:23
  • Its the latter. I have some code which I need to insert – Sab Mar 6 '15 at 12:25
  • I think, the LyX experts will know some solution to this, but you will need the listings package for highlighting in the document itself. – user31729 Mar 6 '15 at 12:26
  • 1
    @SoundsOfSilence LyX is not really a LaTeX code editor. Insert --> Program listings uses the listings package, so it's just a matter of setting it up correctly in Document --> Settings --> Listings. – Torbjørn T. Mar 6 '15 at 12:29
  • @TorbjørnT. You know more of it, definitely. I never used LyX – user31729 Mar 6 '15 at 12:29
7

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

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.