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 need to highlight the syntax of files containing lines with parameters and their values in LaTeX. Let's say I have a following file:

# some comment
parameter1 = value of parameter1
parameter2 = some another value
# and so on ...

Is it possible to colorize it with 3 colors: one for comments, one for parameter names and one for their values?

share|improve this question

migrated from stackoverflow.com Sep 16 '12 at 23:43

This question came from our site for professional and enthusiast programmers.

    
Are you using the listings package? –  Alexis Pigeon Sep 14 '12 at 14:09
    
I know this package, but I don't know if I can use it in such purpose and even whether it's possible. –  tomek.k Sep 14 '12 at 17:08
    
check lstdefinelanguage command in the listings package manual. –  mythealias Sep 17 '12 at 0:23

2 Answers 2

Just to give you a starting point I made the following MWE to show the possibilities of package listings. The first example shows how to use an existing language in package listings, the second example shows how you can define a new language kurt to be used with package listings for highlighting the code.

%http://tex.stackexchange.com/questions/72837/how-to-highlight-colorize-the-syntax-of-configuration-files-like-ini-or-con
\documentclass{article}

\usepackage{listings}
\usepackage{xcolor,showframe}

\lstdefinestyle{ConfigFiles}{% define own style
  language={[LaTeX]TeX},
  basicstyle=\small\ttfamily,
  linewidth=0.9\linewidth,
  breaklines=true,
  keywordstyle=\color{blue}\bfseries,
  identifierstyle=\color{magenta},
  commentstyle=\color{cyan},
  backgroundcolor=\color{yellow!10},
  tabsize=2,
  morekeywords = {parameter},
}

\lstdefinelanguage{kurt}{% new language for listings
  morekeywords={parameter1,parameter2,wert},
  sensitive=false,
  morecomment=[l]{\#},      % comment
  morestring=[b]",          % string def
}

\begin{document}

First example with \texttt{lstlisting} and language \LaTeX:

\begin{lstlisting}[style=ConfigFiles]
% File name.tex
\documentclass{article}
\usepackage{listings}
\lstdefinestyle{ConfigFiles}{
  language={[LaTeX]TeX},       % comment
  keywordstyle=\color{blue},   % comment
  basicstyle=\small\ttfamily,
  tabsize=2,
  parameter=wert,
  morekeywords = {parameter},
}
\begin{document}               % comment
Text "text" text               % comment
\end{document}
\end{lstlisting}

Second example with new language \texttt{Kurt}:

\begin{lstlisting}[language={Kurt},keywordstyle=\color{blue},stringstyle=\color{green}]
# some comment
parameter1 = value of parameter1
parameter2 = some another value
parameter3 = wert
# and so on "and so on" ...
\end{lstlisting}

\end{document} 

You will get (black lines are from package showframe):

enter image description here

share|improve this answer

Here is a ConTeXt solution, in case someone is interested. The vim module uses the vim editor to provide syntax highlighting, which has support for a huge number of languages, so there is no need to create your own parsing style.

\usemodule[vim]

\definevimtyping[INI][syntax=dosini]


\starttext
\startINI
# some comment
parameter1 = value of parameter1
parameter2 = some another value
# and so on ...
\stopINI
\stoptext

which gives

enter image description here

The vim module automatically caches the result, so there is no considerable performance penalty.

share|improve this answer

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.