5

How can I define a custom class of keywords in listings?

In the C# listing below, keywords, such as using, namespace, etc., get highlighted as desired. However, I would like to define a custom class of keywords called "attributes", that would allow me to declare Test, Assert, etc. as attributes and highlight them in a custom style (e.g. cyan color). How can I do that?

enter image description here

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{pdflscape}
\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\usepackage[driver=pdftex, margin=2.54cm]{geometry}


\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},
                %list your attributes here
}

\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}
5
  • 3
    If you want this in the main document text (not in listings) then it will be much more robust to not do it in tex and simply use your editor to add \keyword{} around a, b, and c and then define \keyword to be a suitable \textcolor{}{..} Commented Apr 17, 2014 at 15:41
  • Or use \lstinline or \lstMakeShortInline from the listings package. In any case, if you want to highlight something in the main text, you will need to delimit it, somehow.
    – jub0bs
    Commented Apr 17, 2014 at 16:03
  • Sorry for the obscurity - i would like the highlighting to occur in the listings. I updated the question Commented Apr 17, 2014 at 16:08
  • Please edit your question and add some sample C# code to illustrate and list (as precisely as possible) what should be highlighted in what style, what should not get highlighted at all, etc.
    – jub0bs
    Commented Apr 17, 2014 at 16:15
  • How about adding this line to your lstset? emph={Test,Assert},emphstyle={\color{cyan}}
    – Pouya
    Commented Apr 17, 2014 at 17:51

1 Answer 1

7

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.

enter image description here

\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}
2
  • dude - you're a rockstar Commented Apr 17, 2014 at 19:08
  • 2
    @AidenStrydom \m/(**)\m/
    – jub0bs
    Commented Apr 17, 2014 at 19:13

You must log in to answer this question.

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