Here's one possibility for the colon, comma, braces, square brackets, and numbers, using the literate
key:
\documentclass{article}
\usepackage{bera}% optional: just to have a nice mono-spaced font
\usepackage{listings}
\usepackage{xcolor}
\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}
\lstdefinelanguage{json}{
basicstyle=\normalfont\ttfamily,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frame=lines,
backgroundcolor=\color{background},
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
{:}{{{\color{punct}{:}}}}{1}
{,}{{{\color{punct}{,}}}}{1}
{\{}{{{\color{delim}{\{}}}}{1}
{\}}{{{\color{delim}{\}}}}}{1}
{[}{{{\color{delim}{[}}}}{1}
{]}{{{\color{delim}{]}}}}{1},
}
\begin{document}
\begin{lstlisting}[language=json,firstnumber=1]
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
0123456789
\end{lstlisting}
\end{document}
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
There is a trick way: Highlight all strings except those following a colon.
PS: Internally, the following aspects
strings
,comments
,escape
,style
,language
,keywords
,labels
,lineshape
,frames
,emph
,index
are treat equally. In case you needstring
/comment
for the actual string/comment, you can add an aspect's dealing with attributes and values by yourself.