The listings
package has an internal macro called \lst@InstallKeywords
for exactly that purpose: defining a custom class of keywords. In your case, invoking
\lst@InstallKeywords k{attributes}{attributestyle}\slshape{attributestyle}{}ld
creates four keys: attributes
, moreattributes
, deleteattributes
, and attributestyle
(which is set to \slshape
by default). All four keys have almost the same usage as their keywords
equivalents; the only difference is that they do not accept any optional argument, which means that they only allow for one class of "attribute" keywords.
However, if needed, you can define a whole family of "attribute" keywords (within which you can have several "attribute" keyword classes) with another internal macro called \lst@InstallFamily
; the latter has exactly the same syntax as \lst@InstallKeywords
. See the manual for more details.
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{pdflscape}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\usepackage[driver=pdftex, margin=2.54cm]{geometry}
\makeatletter
\lst@InstallKeywords k{attributes}{attributestyle}\slshape{attributestyle}{}ld
\makeatother
\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{redstrings}{rgb}{0.64,0.08,0.08}
\definecolor{xmlcomments}{rgb}{0.5,0.5,0.5}
\definecolor{types}{rgb}{0.17,0.57,0.68}
\lstset{language=[Sharp]C,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
commentstyle=\color{greencomments},
keywordstyle=\color{bluekeywords}\bfseries,
stringstyle=\color{redstrings},
basicstyle=\ttfamily,
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, GeneticAlgorithmHarmonizer, var},
moreattributes={Assert, Test}, % etc...
attributestyle = \bfseries\color{RubineRed}, % (for instance)
%deleteattributes={Assert}, % just a test
}
\begin{document}
\begin{lstlisting}[breaklines=true]
using System;
using Harmonizer;
using NUnit.Framework;
using ScoreManagement;
namespace TestingLibrary
{
[TestFixture]
public class ScoreManagementUnitTests
{
private const KeySignature KeySignature = ScoreManagement.KeySignature.CMajor;
private const TimeSignature TimeSignature = ScoreManagement.TimeSignature.FourFour;
[Test]
public void AddConstructorParameters_AddingKeySignature_ReturnsTrue()
{
var score = new Score(KeySignature);
Assert.IsTrue(score.KeySignature == KeySignature);
}
}
}
\end{lstlisting}
\end{document}
Best Answer
The easiest thing to do here is to declare all digits as "letters" and define all your keywords with the
keywords
key alone, eschewing theotherkeywords
key completely: