I would like to color my C# code listings as Visual Studio does. However, all language definitions that I found so far only color the keywords and not the user types. I understand that it requires parsing the code and may not be supported by the listings package, therefore I am ready to specify the color of each word myself. E.g. something like this:
\definecolor{usertype}{rgb}{0.17,0.57,0.68}
\begin{lstlisting}[language=C#]
public interface {\color{usertype}IEntity}
{
{\color{usertype}Guid} Guid { get; }
{\color{usertype}ICollection}<{\color{usertype}IComponent}> Components { get; }
{\color{usertype}IComponent} this[string componentName] { get; }
event {\color{usertype}EventHandler}<{\color{usertype}ComponentEventArgs}> CreatedComponent;
event {\color{usertype}EventHandler}<{\color{usertype}ChangedAttributeEventArgs}> ChangedAttribute;
}
\end{lstlisting}
Note that some entries such as Guid
are used both as a type and as an identifier. Unfortunately, this doesn't work as entries like {\color{usertype}...}
are literally written into the output. How can I specify the color of individual words or code pieces in a listing? Or is there a better package than lstlisting
for this?
UPDATE: I have tried using \texttt
, but it ignores the spaces in the beginning of the line, requires \\
at the end of each line to force a line break and characters such as {
and }
must be escaped as \{
and \}
.
Even better would be if I could mark certain words as belonging to a specific class via some special symbol, e.g. like this:
\begin{lstlisting}[language=C#]
public interface @IEntity
{
@Guid Guid { get; }
@ICollection<@IComponent> Components { get; }
@IComponent this[string componentName] { get; }
event @EventHandler<@ComponentEventArgs> CreatedComponent;
event @EventHandler<@ChangedAttributeEventArgs> ChangedAttribute;
}
\end{lstlisting}
UPDATE 2: Tried using minted
package. It parses the code and thus is able to highlight it better, but still doesn't highlight the user types.