Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to use lstlisting to present a file like this:

#comment
[keyword]
key1=value1
key2=value with RegEx

[keyword:NewKeyword]
key1 = value1
key2=value with RegEx

and this is my language definition:

\lstdefinelanguage{myLang}{
    sensitive=false,
    morestring=[b]",
    keywords={=},
    morecomment=[l]{\#},
}

How can I extract [keyword] and [keyword:NewKeyword]?

share|improve this question
1  
Note that "highlighting" and "extracting" can mean two completely different things. I'm assuming you mean the former - highlighting. –  Werner Nov 17 '11 at 15:20
    
Welcome to TeX.SE. –  Peter Grill Nov 17 '11 at 15:39

1 Answer 1

I you want to highlight the words with the [...], you can use moredelim (as with myLangStyleA) which discards the delimiters and applies the given style. If you want to keep the brackets you can use morecomment (as with myLangStyleB)

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{filecontents}% only need to provide the file foo.mylang

\begin{filecontents*}{foo.mylang}% provide file foo.mylang
    #comment
    [keyword]
    key1=value1
    key2=value with RegEx

    [keyword:NewKeyword]
    key1 = value1
    key2=value with RegEx
\end{filecontents*}

\lstdefinelanguage{myLang}{
    sensitive=false,
    morestring=[b]",
    keywords={=},
    morecomment=[l]{\#},
}

\lstdefinestyle{myLangStyleA}{
    language=myLang,
    moredelim=[is][\color{blue}\ttfamily]{[}{]}
}

\lstdefinestyle{myLangStyleB}{
    language=myLang,
    morecomment=[s][\color{red}\ttfamily]{[}{]}, 
}

\begin{document}
    Using \textbf{style=myLangStyleA}:
    \lstinputlisting[style=myLangStyleA]{foo.mylang}

    Using \textbf{style=myLangStyleB}:
    \lstinputlisting[style=myLangStyleB]{foo.mylang}
\end{document}
share|improve this answer
    
I think the OP wants to keep the brackets: the settings seems that of configuration files. –  egreg Nov 17 '11 at 15:41
    
@egreg: Ok. Have updated the solution that provides both options. –  Peter Grill Nov 17 '11 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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