# Using keywords including a dot in the name with the listings package

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?

-

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}

\begin{document}

\begin{lstlisting}
test
\end{lstlisting}

\end{document}

-

You can use

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

-
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

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},
rulecolor=\color{black},
xleftmargin=5pt,
xrightmargin=5pt,
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
backgroundcolor=\color{gray!.50},
alsoletter={.}
}

\begin{lstlisting}[language=JavaScript]
var text = document.createTextNode(A text node)
// "offline" node manipulation
\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.

-