# Treat a dotted suffix as keyword in listings

I'm writing C++ code in latex with listings.

I'm trying to get listings to recognise the suffix .t() as a keyword (it means matrix transpose in the Armadillo C++ library).

I tried to get the ., ( and ) recognised as letters (as suggested in the listings manual and here) with alsoletter={.,(,)} while adding .t() as a keyword, but it didn't work.

I also saw a solution which involved defining the keyword as a comment, which I would rather not do, because I need comments for different purposes.

Below is a minimal reproducible example:

\documentclass[10pt,a4paper,]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}

\usepackage{listings}

\lstnewenvironment{Cppinput}[1][]{
\lstset{%
language={C++},
alsoletter={.,(,)}, % I also tried {.t()}
morekeywords={mat, .t() , ones},
morecomment=[l]//,
basicstyle= \ttfamily,
keywordstyle= \color{BrickRed},
frame=single,
#1
}
}{}

\begin{document}

\begin{Cppinput}
using namespace arma;

mat A;
mat B;
mat C,

A = B + ones<mat>(n,1) + C.t();
// the ".t()" should be highlited as a keyword.
\end{Cppinput}

\end{document}


I have a hunch that the solution may involve alsoother=(), but I can't undertand its syntax from the manual and it didn't word in my example as alsoother=(.t())

-

You can use otherkeywords for your special string; from the listings documentation about otherkeywords:

Defines keywords that contain other characters, or start with digits. Each given "keyword" is printed in keyword style, but without changing the "letter", "digit" and "other" status of the characters. This key is designed to define keywords like =>, ->, -->, --, ::, and so on.

\documentclass[10pt,a4paper,]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}

\lstnewenvironment{Cppinput}[1][]{
\lstset{%
language={C++},
otherkeywords={.t()},
morekeywords={mat,ones},
morecomment=[l]//,
basicstyle= \ttfamily,
keywordstyle= \color{BrickRed},
frame=single,
#1
}
}{}

\begin{document}

\begin{Cppinput}
using namespace arma;

mat A;
mat B;
mat C,

A = B + ones<mat>(n,1) + C.t();
// the ".t( )" is highlited as a keyword.
\end{Cppinput}

\end{document}


-
Thanks, I had missed that part on otherkeywords. – MatteoS Aug 11 '12 at 18:40