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 writing some code with the listings package and I need to detect the words starting with the number sign (#) and apply certain style. For example, if I have this code:

 something #foo anotherThing

I would like to put blue colors to '#foo'.

So far I have:

\lstset{
....
identifierstyle=\idstyle, 
}

And then:

 \makeatletter
 \newcommand*\idstyle{%
         \expandafter\id@style\the\lst@token\relax
 }
 \def\id@style#1#2\relax{%
         \ifcat#1\relax\else
                 \ifnum`#1=\uccode`#1%
                    \small\ttfamily\color{red!100!black}
                \fi
                 \ifnum\pdfstrcmp{#1}{\#}=0
         \small\ttfamily\color{blue!100!black}

         \fi
           \ifnum`#1= 35%
                         \small\ttfamily\color{blue!100!black}
                 \fi
         \fi
 }
 \makeatother

So I am trying to put a blue color for words starting with #. The first try (using \pdfstrcmp) works perfect with others type of characters but not with #. I cannot scape it. I tried to scape # in lots of ways such as \#, \#\, |\verb#|, etc et, but no one worked.

In the second try, I am trying to go by comparing the unicode value, but I am not successful either.

Notice also that at the same time I wanted to put colors (different color) to words that start with uppercase. So how can I have both at the same time (set different colors to both, words that start with uppercase and those that start with \#?

Sorry for the question but I am newbie with Latex...

Any ideas how can I get this?

share|improve this question
    
I'm almost sure to read in the past a similar question in which they wanted to set with a certain style words preceded by @. Unfortunately I can't find it now :( –  Claudio Fiandrino Jun 2 '12 at 12:40
    
Hi Marco. Thanks for the welcome :) As you can see in the question, I do use \# but still it does not work. –  Mariano Martinez Peck Jun 2 '12 at 12:47
add comment

2 Answers

up vote 3 down vote accepted

I will try to answer your question. The symbol # is a specific character, also within listings. To detect the symbol inside the identifier style you must declare # as a letter. By default it is declared as "other". Table 2: Standard character table of the great documentation shows the default setting. So you can do the following:

enter image description here

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

\lstset{%
alsoletter=\#,
identifierstyle=\idstyle, 
}

\makeatletter
\newcommand*\idstyle[1]{%
         \expandafter\id@style\the\lst@token{#1}\relax%
 }
 \def\id@style#1#2\relax{%
           \ifnum\pdfstrcmp{#1}{\#}=0%
                \small\ttfamily\color{blue!100!black} \the\lst@token%
            \else%
              \edef\tempa{\uccode`#1}%
              \edef\tempb{`#1}%
              \ifnum\tempa=\tempb%
                  \small\ttfamily\color{red!100!black} \the\lst@token%
              \else%
                  \the\lst@token%
             \fi%
            \fi%
 }
 \makeatother
\begin{document}
\begin{lstlisting}
something #foo another Thing
\end{lstlisting}
\end{document}
share|improve this answer
    
@MarianoMartinezPeck: Edit and please remove you last answer. –  Marco Daniel Jun 2 '12 at 17:10
    
Wow. Thanks Marco. That works great. Thanks A LOT. –  Mariano Martinez Peck Jun 2 '12 at 17:29
add comment

You can use keywordsprefix=\# to specify that anything beginning with # is to be treated as a keyword:

enter image description here

Notes:

Code:

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

\lstset{%
    backgroundcolor=\color{yellow!20},%
    basicstyle=\small\ttfamily,%
    keywordstyle=\color{blue}\bfseries,%
    language=Java,%
    keywordsprefix=\#,%
    alsoletter=\#,%  if you also want the "#" to be highlighted
    }%

\begin{document}
\begin{lstlisting}
  something #foo anotherThing
\end{lstlisting}
\end{document}
share|improve this answer
    
Hi. Thanks for the answer. However, that means that I would have to apply the same styles to those words starting with # and keywords. In my case I need both and both with different styles. –  Mariano Martinez Peck Jun 2 '12 at 16:38
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.