2

I have lots of bookkeeping transactions that I would like to typeset in a color coded format. I am using the listings package to accomplish what I want. I tried the following code.

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

Which resulted in the following.

Example of almost what I want

This is almost what I want. What I want instead is:

  • The keywords that happen to appear in the comments to use the comment style and not the keyword style. For example, on line 185, I would like the whole thing to the in the purple color.
  • I would like $ and % to indicate the beginning of a keyword (until a space). For example, on line 190, the O appears as black, but I wanted it to be green. Currently I use keywordsprefix but it has no impact. I could only get $ and % to be green by adding them to the otherkeywords.
  • I would like somethings, eg !, *, :, =, [, ], etc., to appear as the eclipseBlue color instead.

I tried to use the following instead. However, that just resulted in what you see in green above to be black.

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

I appreciate any advise or aid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.