Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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 :-) –  Promather Dec 14 '10 at 22:54
add comment

1 Answer 1

up vote 8 down vote accepted

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!

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.