3

I'm trying to create a simple style for Windows INI files using the listings package

I'd like this results:

  • purple [sections] tags
  • red assignment operators (= and :)
  • gray comment lines starting with (; or #)

Here is how Notepad++ highlights it.

Windows INI highlight

I tried to adapt code from these answers.

So far I was able to achieve purple [tags] and ; comments. But I can't get the assignment operators to hightlight, nor the # comments to work.

\documentclass{article}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}

\lstdefinelanguage{Ini}
{
    basicstyle=\ttfamily\small,
    columns=fullflexible,
    tag=[s]{[]},
    tagstyle=\color{Orchid}\bfseries,
    usekeywordsintag=true,
    morecomment=[l]{;},
    commentstyle=\color{gray}\ttfamily,
    alsoletter={=},
    ndkeywords={=},
    ndkeywordstyle=\color{green}\bfseries
}[html]

\title{INI test}
\author{Me}
\begin{document}
   \maketitle
   INI file highlighting test.

    \begin{lstlisting}[language={Ini}]
    ; comment1
    [section1]
    var1 = val1
    var2 = val2

    #comment2
    [section2]
    var1: val1
    var2: val2
    \end{lstlisting}
\end{document}
7
  • 1
    What kind of .ini files do you mean? Windows or KDE like settings? Can you provide a working tex code with an example .ini?
    – user31729
    Sep 9 '15 at 14:27
  • I added an example and highlight from Notepad++ (similar in function, but different colors).
    – Agostino
    Sep 9 '15 at 14:33
  • You should use otherkeywords for symbols instead of (the deprecated) ndkeywords (I'll show you how in an answer)
    – Bordaigorl
    Sep 9 '15 at 14:46
  • 1
    I think you're abusing the tag and tagstyle keys, here. You should use moredelim, instead. You also need to add morecomment=[l]{\#}.
    – jub0bs
    Sep 9 '15 at 14:50
  • @Jubobs I tried, but it doesn't work. I suspect building on the html style is creating problems for the assignment operators, but I can't get the tags to work like that.
    – Agostino
    Sep 9 '15 at 14:56
4

I think the best way to implement this is avoiding using the html style as a base:

\lstdefinelanguage{Ini}
{
    basicstyle=\ttfamily\small,
    columns=fullflexible,
    morecomment=[s][\color{Orchid}\bfseries]{[}{]},
    morecomment=[l]{\#},
    morecomment=[l]{;},
    commentstyle=\color{gray}\ttfamily,
    morekeywords={},
    otherkeywords={=,:},
    keywordstyle={\color{green}\bfseries}
}

This satisfies the requirements but will not highlight html inside the ini's values.

Your problem was:

  • # cannot be specified as a comment delimiter without escaping it (it is a special character)
  • to highlight symbols instead of changing their class it is better to use otherkeywords (which can only be used if morekeywords is used first, hence the morekeywords={} entry)
  • you were abusing tags where morecomment is more than sufficient (you could also have used moredelim)
2
  • Good approach. +1
    – jub0bs
    Sep 9 '15 at 15:23
  • Thank you. Now there's a simple and effective configuration for INI files around here. +1 if you think the question was worthy as well ;)
    – Agostino
    Sep 9 '15 at 15:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.