13

I intend to use the package listings for including C# code in a LaTeX document. There are a lot of languages supported by this package, but C# lacks at all. Is there an extension for adding support for this language? or where should I add the C# (ver 4.0) keywords to see them highlighted? Note that the support for Java/C++ does not suffice, as there are some keywords not introduced in these languages: ref, out, var etc.

2 Answers 2

17

The listings package supports it (see the manual, page 12). Your document could look something like this:

\documentclass{scrartcl}
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{sharpc}{language=[Sharp]C, frame=lr, rulecolor=\color{blue!80!black}}

\begin{document}

\lstset{style=sharpc}
\begin{lstlisting}
Your c# code here
\end{lstlisting}

\end{document}

As I don't know C#, I also don't know if the language definition of listings is sufficient for you, but it offers morekeywords key, so you can add the ones you are missing

3
  • Is it possible to get this in LyX? Because I don't see SharpC in the list of available languages. Apr 27, 2012 at 14:45
  • 4
    @DmitriNesteruk You find it on page 12 of the manual. And it's [Sharp]C, which means use language C, the dialect Sharp. Without this listings would choose the standard dialect (ANSI in this case, therefore it is underlined in the manual). Although I don't use Lyx, I don't see any reason for it not to work. Apr 27, 2012 at 16:24
  • 11
    Thanks! Though I must admit this is beyond unintuitive... who on earth came up with that idea? C# as a dialect of C, pah! Ridiculous! Apr 28, 2012 at 7:41
4

Use this to change the parameters that you want

\usepackage{listings}
\usepackage{color}
\lstloadlanguages{C,C++,csh,Java}

\definecolor{red}{rgb}{0.6,0,0} 
\definecolor{blue}{rgb}{0,0,0.6}
\definecolor{green}{rgb}{0,0.8,0}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstset{
language=csh,
basicstyle=\footnotesize\ttfamily,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
tabsize=2,
extendedchars=true,
breaklines=true,
frame=b,
stringstyle=\color{blue}\ttfamily,
showspaces=false,
showtabs=false,
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
commentstyle=\color{green},
morecomment=[l]{//}, %use comment-line-style!
morecomment=[s]{/*}{*/}, %for multiline comments
showstringspaces=false,
morekeywords={ abstract, event, new, struct,
as, explicit, null, switch,
base, extern, object, this,
bool, false, operator, throw,
break, finally, out, true,
byte, fixed, override, try,
case, float, params, typeof,
catch, for, private, uint,
char, foreach, protected, ulong,
checked, goto, public, unchecked,
class, if, readonly, unsafe,
const, implicit, ref, ushort,
continue, in, return, using,
decimal, int, sbyte, virtual,
default, interface, sealed, volatile,
delegate, internal, short, void,
do, is, sizeof, while,
double, lock, stackalloc,
else, long, static,
enum, namespace, string},
keywordstyle=\color{cyan},
identifierstyle=\color{red},
backgroundcolor=\color{cloudwhite},
}

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{blue}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

And then:

\begin{lstlisting}[language={[Sharp]C}, caption={C\# exaple}, label={Script}]

%some code here

\end{lstlisting}
1
  • 3
    this is great, but you forgot to define cloudwhite so it wont actually compile. I used Fords definition of cloud white, that looks ok.. \definecolor{cloudwhite}{rgb}{0.9412, 0.9608, 0.8471}
    – Henrik
    Jan 31, 2019 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .