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.

Given this image:

enter image description here

How would you create a lstdefinelanguage that would make Func<double, double, double> highlighted without having to add spaces after each comma?

\documentclass{book}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\lstdefinelanguage{CSharp}
{
      basicstyle=\ttfamily,
      keywordstyle=\color{blue!60!black}\bfseries,
      morekeywords={double,Func},
      alsoletter={[, ]},
}

\lstset{
    basicstyle=\footnotesize\ttfamily,
    language=[Sharp]C,
    alsolanguage=CSharp,      
    keywordstyle=\color[rgb]{0.0,0,1.0}\bfseries  
}
\begin{document}

\begin{lstlisting}
double x = 10;
Func<double> x = 10;
Func<double, double, double> x = 10;
Func<double , double , double> x = 20;
\end{lstlisting}

\end{document}
share|improve this question
    
@FilipEkberg: If I remove the addition bracket after morekeywords your example works well. –  Marco Daniel Apr 22 '12 at 19:45
add comment

1 Answer

up vote 4 down vote accepted

Here a simple example that shows an approach.

Based on the given information in the comments you want to highlight combinations like int[]. In this case you shouldn't use alsoletter. Instead use otherkeywords

\documentclass[varwidth,border=10,convert={png,size=800,density=1000}]{standalone}
\usepackage{xcolor}
\usepackage{bera}
\usepackage{listings}
\lstdefinelanguage{mylang}{%
  basicstyle=\ttfamily,
  keywordstyle=\color{blue!60!black}\bfseries,
 morekeywords={double,Func,int},
  sensitive=false,
 otherkeywords={[, ]}
}
\begin{document}
\begin{lstlisting}[language=mylang]
double x = 10;
Func<double> x = 10;
Func<double, double, double> x = 10;
Func<double , double , double> x = 10;
int[];
\end{lstlisting}
\end{document}

enter image description here

share|improve this answer
    
Thanks, that works. But it breaks if you add alsoletter={[, ]} If you for instance want to allow int[] to be highlighted too. Is there a reason to that? –  Filip Ekberg Apr 22 '12 at 19:46
    
@FilipEkberg: That has no effect to int. If you set alsoletter={[, ]} you have to define int[] as a keyword. –  Marco Daniel Apr 22 '12 at 19:49
1  
@FilipEkberg: I edited my answer. –  Marco Daniel Apr 22 '12 at 19:55
    
Thanks Marco, it broke if I only had the alsoletter-part. But this seem to work very nice. Thanks! –  Filip Ekberg Apr 22 '12 at 20:18
add comment

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.