列表 - 为自定义语言选择正确的颜色

列表 - 为自定义语言选择正确的颜色

我有很多簿记交易,我想以彩色编码格式排版。我正在使用该listings包来实现我想要的功能。我尝试了以下代码。

\documentclass[12pt,letter]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[scaled=0.85]{beramono}
\usepackage[T1]{fontenc}
\renewcommand\familydefault\sfdefault
\usepackage{color}
\definecolor{eclipseBlue}{RGB}{42,0.0,255}
\definecolor{eclipseGreen}{RGB}{63,127,95}
\definecolor{eclipsePurple}{RGB}{127,0,85}
\definecolor{eclipseRed}{RGB}{200,0,42}
\usepackage{listings}

\lstdefinelanguage{ledger}
{
    morekeywords={EUR,GBP,AUD,CAD},
    otherkeywords={0,1,2,3,4,5,6,7,8,9,\$,\%,[,],(,),.,/,:,=,*,!,-},
    keywordsprefix={\$,\%},
    sensitive=true, % keywords are case-sensitive
    comment=[l]{;}, % l is for line comment
    alsoletter={\$,\%,/,=,:,[,],(,),0,1,2,3,4,5,6,7,8,9,*,!},
    alsodigit={.,-}
    %identifierstyle=\color{identifierColor}
}

\lstset{
    language=ledger,
    basicstyle=\small\ttfamily,
    breaklines=false,
    keywordstyle=\color{eclipseGreen},
    commentstyle=\color{eclipsePurple},
    numbers=left,
    numberstyle=\scriptsize,
    numberblanklines=true,
    numbersep=2mm,
    frame=lines,
    captionpos=b,
}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ipsum purus, sagittis sodales dictum ac, fermentum non leo. Donec consectetur faucibus felis, eget molestie mauris scelerisque interdum. Aenean erat dolor, semper eget augue non, mattis ornare mi. Aliquam laoreet orci nec eros tincidunt, in venenatis sem lacinia. Aenean et \lstinline|2015/06/28| urna sit amet erat dictum tempor. Sed efficitur molestie libero. Vestibulum pretium bibendum risus, eu maximus enim porttitor vitae. Curabitur sed tristique lacus. Nulla quis magna vitae lorem tincidunt mollis. Quisque \lstinline|$45.98| vitae dui in odio venenatis fringilla. Nam quis justo sagittis, tincidunt neque interdum, aliquam purus. Suspendisse suscipit, libero at volutpat elementum, elit dolor fringilla augue, at vulputate velit turpis sit amet lectus. Mauris dictum accumsan erat eget convallis. 

%[name=,title=,caption=,label=,nolol]
\begin{lstlisting}
2015/06/28 ! Just a test
        ; With some comment
    Assets:Cash                                                          $500.00
    Income:Work                                                         $-500.00

2013/10/13  Receive donation from Marisol Haxby
        ; Entity: Marisol Haxby
        ; Program: ArgoUML
    Assets:ArgoUML:Checking GBP                                           213.81 GBP
    Income:ArgoUML:Donations GBP                                         -213.81 GBP
        ; IncomeType: Donations

2013/10/13  Convert donation from Marisol Haxby
        ; Program: ArgoUML
    Assets:ArgoUML:Checking                                                           [=2013/10/13]
    Assets:ArgoUML:Checking GBP                                          -213.81 GBP

2013/10/17  Donation from Rosaline Colker
        ; Entity: Rosaline Colker
        ; Program: ArgoUML
    Assets:ArgoUML:Checking                                               $10.42
    Income:ArgoUML:Donations                                             $-10.42
        ; IncomeType: Donations

2013/10/28  Gandi Web Hosting
        ; Entity: Gandi
        ; Program: ArgoUML
    Expenses:ArgoUML:Gandi Web Hosting                                    $30.00
        ; Receipt: ./2013-10-28_Gandi_Web_Hosting.txt
    Assets:ArgoUML:Checking                                              $-30.00

2013/10/28  Close income to income summary
        ; Program: ArgoUML
    Income:ArgoUML                                                      %8(-1*O)
    Income:ArgoUML:Income Summary        
\end{lstlisting}

\end{document}

其结果如下。

几乎我想要的例子

这几乎就是我想要的。我想要的是:

  • 注释中出现的关键字使用注释样式而不是关键字样式。例如,在第 185 行,我希望整个内容都变成紫色。
  • 我希望$%表示关键字的开头(直到空格)。例如,在第 190 行,O 显示为黑色,但我希望它是绿色。目前我使用keywordsprefix但它没有影响。我只能通过将$%添加到 来使它们变为绿色otherkeywords
  • 我希望一些东西,例如,,,,,,等等!,以eclipseBlue 颜色出现。*:=[]

我尝试使用下面的方法。然而,这只会导致上面看到的绿色变成黑色。

\lstdefinelanguage{ledger}
{
    morekeywords={EUR,GBP,AUD,CAD},
    morekeywords=[2]{[,],(,),/,\$,\%,tag,account,assert,check,commodity,include,define},
    morekeywords=[3]{0,1,2,3,4,5,6,7,8,9,:,-,=,*,!,value,note,format,nomarket},
    keywordsprefix={\$,\%},
    sensitive=true,
    comment=[l]{;},
    alsoletter={\$,\%,/,=,:,[,],(,),*,!,0,1,2,3,4,5,6,7,8,9},
    alsodigit={.,-},
    basicstyle=\small\ttfamily,
    keywordstyle=\color{eclipseRed},
    keywordstyle=[2]\color{eclipseBlue},
    keywordstyle=[3]\color{eclipseGreen},
    commentstyle=\color{eclipsePurple}
}

我感谢任何建议或帮助。

相关内容