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.

When using Perl variable names with sigils with listings, the $ and @ sigils somehow get coloured in identifierstyle, but the % sigil remains unstyled.

Therefore, this code:

\documentclass{article}

\usepackage{listings,xcolor}

\lstdefinestyle{perl}{
   language=Perl,
   basicstyle=\ttfamily,
   keywordstyle=\bfseries\color{green!40!black},
   commentstyle=\color{purple!40!black},
   identifierstyle=\color{blue},
   stringstyle=\color{orange},
}

\lstset{style=perl}

\begin{document}
    \begin{lstlisting}
        my ($a, $b, $c) = ('a'..'z');
        my ($a, $b, @c) = ('a'..'z');
        my ($a, $b, %c) = ('a'..'z');
    \end{lstlisting}
\end{document}

gives this result:

unstyled sigil

Is there a way I could make listings understand that % should have the same style as $ or @?

The manual is pretty clear about keywords and comments, but it didn't help me understand the difference between an identifier and an operator (?) that doesn't get styled. Looking at the Perl language definition in lstlang1.sty didn't help either since there is nothing there about either $ and @ or %.

share|improve this question
    
Please make your code compilable, starting with \documentclass{...} and ending with \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to help you. Help them help you: remove that one hurdle between you and a solution to your problem. –  Jubobs Nov 6 '13 at 14:00
1  
@Jubobs: thank you, I have done as you suggest. –  scozy Nov 6 '13 at 15:12

1 Answer 1

up vote 5 down vote accepted

Short answer

By default, identifiers cannot start by %. You can allow identifiers to start by % by specifying alsoletter={\%} in the definition of your custom perl style.

Detailed answer

Look up the alsoletter key in subsection 4.18 of the listings manual:

All identifiers (keywords, directives, and such) consist of a letter followed by alpha-numeric characters (letters and digits). For example, if you write keywords={one-two,\#include}, the minus sign must become a digit and the sharp a letter since the keywords can’t be detected otherwise.

According to Table 2, % is (by default) treated, not as a "letter", but as "other". The Perl style (which is defined in lstdrvrs.dtx, a file that ships with listings.sty) doesn't change that.

Therefore, in order to allow identifiers to begin by %, you must specify alsoletter={\%}. Note that % must be escaped, here; see item 5 in subsection 4.1 for more details.

enter image description here

\documentclass{article}

\usepackage{listings,xcolor}

\lstdefinestyle{perl}
{
   language=Perl,
   basicstyle=\ttfamily,
   keywordstyle=\bfseries\color{green!40!black},
   commentstyle=\color{purple!40!black},
   identifierstyle=\color{blue},
   stringstyle=\color{orange},
   alsoletter={\%},
}

\lstset{style=perl}

\begin{document}
    \begin{lstlisting}
        my ($a, $b, $c) = ('a'..'z');
        my ($a, $b, @c) = ('a'..'z');
        my ($a, $b, %c) = ('a'..'z');
    \end{lstlisting}
\end{document}
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.