# Highlight code with squared brackets

I want to show some .ini file in my work. The ini file works with squared brackets. I try to use \lstdefinelanguage but I can't handle it. That is what I try before and in my understanding it should work but it doesn't:

\documentclass{article}

\usepackage{color}
\usepackage{listings}

\lstdefinelanguage{Ini}
{
tag=[s][],
tagstyle=\color{blue}\bfseries,
usekeywordsintag=true
}

\lstset{
language=Ini
}

\begin{document}

\begin{lstlisting}
[test]
a=15
b=4
[test1]
c=25
d=5
[test2]
e=50
f=5
\end{lstlisting}

\end{document}


That is how it should look:

Highlight the background behind the keywords wouldn't be that necessary but a nice gimmick.

An problem what I don't understand is how should Latex handle this case:

\begin{lstlisting}[language=Ini]
[test]
a=1
\end{lstlisting}


How can latex difference between the property and the content?

The listings documentation (page 44) mentions that the key tag is language specific (to HTML), so you need to make it available to your language

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstdefinelanguage{Ini}
{
basicstyle=\ttfamily\small,
columns=fullflexible,
tag=[s]{[]},
tagstyle=\color{blue}\bfseries,
usekeywordsintag=true
}[html]

\lstset{
language=Ini
}

\begin{document}

\begin{lstlisting}
[test]
a=15
b=4
[test1]
c=25
d=5
[test2]
e=50
f=5
\end{lstlisting}

\end{document}


Regarding your last question,"how should LaTeX handle this case"

\begin{lstlisting}[language=Ini]
[test]
a=1
\end{lstlisting}


lstlisting postpones the processing for verbatim material until having read the optional argument for lstlisting.

• Is there a way to have the equal sign highlighted in red? – Agostino Sep 8 '15 at 23:34