4

I am writing some document which contains Cg (C for Graphics) code in the document. I want to show the code in a neat highlighted mode. Any easy method to suggest? I tried the listings package but it doesn't support Cg language.

  • 1
    I'm not very familiar with Cg, adding additional keywords to listings starting from, e.g., C, is not very difficult. I think to a zeroth approximation, a key like morekeywords={foo,bar,baz} might work, but better check the manual. – Ulrich Schwarz Dec 14 '10 at 22:03
  • OK, I found the solution. It is similar to your suggestion. Check my answer :-) – Rafid Dec 14 '10 at 22:54
8

OK, I found the solution to my own question. I defined the Cg language using the following code:

\lstdefinelanguage{cg}{
    morekeywords={void, float, float2, float4, float4,
        float4x4, POSITION, TEXCOORD0, out, uniform},
    morecomment=[l]{//}
}

Some notes:
1. The 'morekeywords' specify the keywords of the language. Obviously, there are many more keywords in the Cg language, but I used the ones I need.
2. The 'morecomment' is used to specify commenting styles in the language. Cg language use the same style as C language.

Then I used the following code to make some customization:

\lstset{
    language=cg,
    tabsize=2,
    basicstyle=\ttfamily\scriptsize,
    keywordstyle=\color{blue},
    commentstyle=\color{green}\scriptsize,
    stringstyle=\color{green},
    showstringspaces=false,
    breaklines=true
}

Finally, including my code in the document:

\lstinputlisting[language=cg]{VertexProgram.cg}

Don't forget to include the listings package:

\usepackage{listings}

Hope that helps!

  • 2
    Nice find, and +1 for being considerate enough to answer your own question for others :) – Adrian Petrescu Dec 15 '10 at 4:30

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.