# beautiful listing for CSharp

Does anyone have a nice typesetting style for the C# language using listings? I configure it in this way:

\lstset{language=[Sharp]C,,basicstyle=\footnotesize,
showspaces=false,showtabs=false,,breaklines=true,
showstringspaces=false,breakatwhitespace=true,
escapeinside={(*@}{@*)}}


But still it doesn't look fine, T_T. In my configuration I have characters that are too rounded and the code looks like if it was written using a typewriter. I don't want that. I want a modern, clear font, that looks more similar to the what I see in Visual Studio.

Final Election.

Thanks to all you guys, for give me ways to improve the appearance of the code. My final election is from the answer of @PauloCereda, little tuned with one of the comments and a bfseries touch for keywords. Here is the code and how it looks:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}

\usepackage{color}
\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{redstrings}{rgb}{0.9,0,0}

\usepackage{listings}
\lstset{language=[Sharp]C,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
keywordstyle=\color{bluekeywords}\bfseries,
stringstyle=\color{redstrings},
basicstyle=\ttfamily
}

\begin{document}

\begin{lstlisting}
/**
* Prints Hello World.
**/
class Program
{
public static void Main()
{
System.Console.WriteLine("Hello World!");
}
}
\end{lstlisting}

\end{document}


-
The default Visual Studio font also is a typewriter style/monospace font :-) –  Andrey Vihrov May 16 '11 at 19:54
@AndreyVihrov xD, perhaps that they look fine, not bloated or overly rounded. I also think that the space between characters is bigger than normal in listings. –  mjsr May 16 '11 at 19:59
If I'm not mistaken, the Visual Studio font name is Consolas. There's an alternative font called Inconsolata, which is pretty good. –  Paulo Cereda May 16 '11 at 20:32
Additionally, you can find the free Microsoft fonts listed here –  percusse May 17 '11 at 12:45
See my answer here (click). –  kiss my armpit Jul 27 '14 at 20:03

Based on Thorsten's code, I added colors to keywords, strings and comments and replaced the current typewriter font by Inconsolata.

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{inconsolata}

\usepackage{color}
\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{redstrings}{rgb}{0.9,0,0}

\usepackage{listings}
\lstset{language=[Sharp]C,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
keywordstyle=\color{bluekeywords},
stringstyle=\color{redstrings},
basicstyle=\ttfamily
}

\begin{document}

\begin{lstlisting}
/**
* Prints Hello World.
**/
class Program
{
public static void Main()
{
System.Console.WriteLine("Hello World!");
}
}
\end{lstlisting}

\end{document}


Resulting in:

According to the Inconsolata documentation, loading it redefines the \tt font.

-
perhaps the colors looks cool, when i zoom in or zoom out the clarity of the font is lost. –  mjsr May 16 '11 at 21:38
forget my previous comment, i don't know how but now the fonts looks perfect. One more help please, i try to make the keywords bold but perhaps i put "keywordstyle=\color{bluekeywords}\bfseries" it seems that it ignore the instruction. –  mjsr May 16 '11 at 22:27
@voodoomsr: Unfortunately, it seems that the Inconsolata font has no bold face. Maybe we can choose another typewriter font. –  Paulo Cereda May 16 '11 at 22:46
@voodoomsr: Take a look at beramono, which is a modern-looking (not too Courier-like) pretty good monotype font that includes bold face and slanted versions. –  Daniel May 17 '11 at 7:03
@Daniel ouuuuu yea with that font and the color configuration that @PauloCereda provide plus the bfseries for the keywords produce the clear and beautiful look that i want :D –  mjsr May 17 '11 at 12:11

You may use basicstyle to customize the font:

\documentclass{minimal}
\usepackage{bold-extra}
\usepackage{listings}
\lstset{language=[Sharp]C,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
basicstyle=\ttfamily,
columns=fullflexible
}
\begin{document}
\begin{lstlisting}
class Program
{
public static void Main()
{
System.Console.WriteLine("Hello World!");
}
}
\end{lstlisting}
\end{document}


Alternatively you could use lmodern instead of bold-extra as Andrey Vihrov suggested.

-
In an answer I was writing, I also used \usepackage{lmodern}. This loads a bold typewriter font which is used for identifiers. –  Andrey Vihrov May 16 '11 at 20:09
they look a little better but they lost all the customization for special keywords, operators, etc, i.e. they are presented as plain text. –  mjsr May 16 '11 at 20:13
Err, I meant keywords. –  Andrey Vihrov May 16 '11 at 20:18
@voodoomsr: Try the above command and see if it's better. Note that the bold typewriter font is only slightly heavier than the regular one. There is a darker font, but IMO it looks worse. –  Andrey Vihrov May 16 '11 at 20:20
@voodoomsr See my updated code –  Thorsten May 16 '11 at 20:20

There is a list of Visual Studio styles that you can choose as a reference. The following figure shows some of them.

Afterwards, you need to look up the name of colors, fonts, etc used there to create your own listings style. Creating listings style can be found in What configuration do you propose for listings.sty to make the output look comfortable?.

-

If you don't mind solutions that involve other tools, I like to use Pygments to generate code listings for inclusion in my documents. It can process dozens of programming languages and other markup. Just pipe your code listing through their command-line program and \input the resulting markup in your document.

-
Or, use the minted package. –  You May 16 '11 at 21:28

If you compile the document with xelatex or lualatex, you can use any TrueType or OpenType font you want. So you could use for example the font that Visual Studio uses. Just load the fontspec package and use something like \setmonofont{Inconsolata} together with basicstyle=\texttt. Or define a font family explicitly for code, as in the following example.

\documentclass{minimal}
\usepackage{fontspec,xcolor}

% Define \codefont to switch to the font 'Ubuntu'.
% (I find the font size more pleasing when the lower case
% characters are scaled to the size of the lower case of the
% surrounding text.)
\newfontfamily\codefont[Scale=MatchLowercase]{Ubuntu}

\usepackage{listings}
\lstset{language=[Sharp]C,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
basicstyle=\codefont,             % use the font defined as \codefont
stringstyle=\color{blue!70!black},
columns=fullflexible
}
\begin{document}

\begin{lstlisting}
// A program that prints a greeting message.
class Program
{
public static void Main()
{
System.Console.WriteLine("Hello World!");
}
}
\end{lstlisting}
\end{document}


Also see the listings manual for lots of ways to customize the formatting of listings.

-
Btw, the Ubuntu font is available from font.ubuntu.com –  Caramdir May 16 '11 at 21:40
i really like how this look, is it possible to put a working code with inconsolata?...i try what you say but i receive an error. Maybe i'm using bad the xelatex command, is the first time i use it, :D. i always use pdflatex.exe. –  mjsr May 17 '11 at 1:42
@voo: Did you install the font? It is hard to help you without the error message. –  Caramdir May 17 '11 at 3:10
@Caramdir yes it is installed, here is the log of the error dl.dropbox.com/u/15811510/lol.log –  mjsr May 17 '11 at 3:41
@voo: It seems you are compiling the example with the Ubuntu font. If you do that you also need to install that font. If you want Inconsolata, try compiling the code I posted in the answer, but with Ubuntu replaced by Inconsolata. Also MikTeX 2.8 might have old versions of XeTeX and fontspec, which might cause problems. –  Caramdir May 17 '11 at 3:53