如何在列表中定义自定义类别的关键字?

如何在列表中定义自定义类别的关键字?

如何在中定义自定义关键字类别listings

在下面的 C# 列表中,关键字(例如usingnamespace等)会根据需要突出显示。但是,我想定义一个名为“属性”的自定义关键字类,这样我就可以将、等声明TestAssert属性,并以自定义样式(例如青色)突出显示它们。我该怎么做?

在此处输入图片描述

\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}

答案1

listings包有一个内部宏,\lst@InstallKeywords其目的正是定义自定义关键字类。在您的例子中,调用

\lst@InstallKeywords k{attributes}{attributestyle}\slshape{attributestyle}{}ld

创建四个键:attributesmoreattributesdeleteattributesattributestyle(默认设置为\slshape)。所有四个键均具有几乎与其等效物的用法相同keywords;唯一的区别是它们不接受任何可选参数,这意味着它们只允许一类“属性”关键字。

但是,如果需要,你可以定义一个整体家庭“属性”关键字(其中可以有多个“属性”关键字类)与另一个名为 的内部宏\lst@InstallFamily;后者的语法与 完全相同\lst@InstallKeywords。有关更多详细信息,请参阅手册。

在此处输入图片描述

\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}

相关内容