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 am using the listings package which works like charm in most cases.

I tried to define new languages for my needs with the help of morekeywords options in \lstset, but entering special keywords having a dot in the name just do not work. E.g.:

\lstset{morekeywords={read.table}}

I know I could set this keyword in two and set read and table to be also keywords, but I do not want every read to be highlighted in the source code.

Do you have an idea how to set this keyword or how to escape the dot?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

After trying to do this by changing the catcode of . to letter and a short look through the listings code I decided to RTFM and found the alsoletter option quite quickly. You also may want to use the alsodigit option instead.

\documentclass{article}

\usepackage{listings}

\lstset{alsoletter={.},morekeywords={read.table}}
\begin{document}

\begin{lstlisting}
   read read.table table read-table
   test
\end{lstlisting}

\end{document}
share|improve this answer
add comment

You can use

\lstset{alsoletter={.},morekeywords={read.table}}
share|improve this answer
    
Now you were 20sec faster :-) –  Martin Scharrer Feb 12 '11 at 15:46
    
I knew this before, but RTFM is still needed to remember the exact keyword. This may be wrong, if we still need int in int.a to be a keyword. –  Leo Liu Feb 12 '11 at 15:49
    
The manual gives an example for - and # (written as \#) and states that the first must become a digit (using alsodigit) and the latter a letter. It doesn't state why, but your example might be the reason. –  Martin Scharrer Feb 12 '11 at 15:58
    
Actually, listings has little ability to do lexical analysis. It just know charactor sets and keywords. In this sense, highlight and GNU Source-highlight are more powerful. –  Leo Liu Feb 12 '11 at 16:07
add comment

My answer is a bit later than the other posts, but I decided to put it here to show, how to define a new language (Javascript in the example) and how to emphasize only part of the code as the OP wants. I have also used alsoletter (as the other examples).

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

\gdef\emphasis#1{\lstset{emph={write,void,writeln,Hello,#1},
   emphstyle={\ttfamily\textcolor{red}}}}


\begin{document}
\lstdefinelanguage{JavaScript} {
    morekeywords={
        break,const,continue,delete,do,while,export,for,in,function,
        if,else,import,in,instanceOf,label,let,new,return,switch,this,
        throw,try,catch,typeof,var,void,with,yield,
        },
    sensitive=false,
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]",
    morestring=[d]',
    alsoletter={.}
}


\lstset{
    %frame=tb,
    framesep=5pt,
    basicstyle=\normalsize, %\ttfamily,
    showstringspaces=false,
    keywordstyle=\ttfamily\color{blue},
    identifierstyle=\ttfamily,
    stringstyle=\ttfamily\color{orange},
    commentstyle=\color{orange},
    rulecolor=\color{black},
    xleftmargin=5pt,
    xrightmargin=5pt,
    aboveskip=\bigskipamount,
    belowskip=\bigskipamount,
            backgroundcolor=\color{gray!.50},
     alsoletter={.}
}

\emphasis{document.createElement, headline.appendChild}
\begin{lstlisting}[language=JavaScript]
// some comments
// about your code
var headline = document.createElement(h1);
var text = document.createTextNode(A text node)
// "offline" node manipulation
headline.appendChild(text);
// adding node to DOM
document.getElementsByTagName("body")[0].appendChild(headline);
\end{lstlisting}
\end{document}

It is always preferable to highlight the part of the code under discussion, rather than color code the whole of the script.

share|improve this answer
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.