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 have this pgfplots code that I want to put in a lstlisting environment:

\pgfplotsset{
   every axis x label/.append style = {
      font = \small
   },
   every axis y label/.append style = {
      font = \small,
      rotate = -90,
      xshift = 0.5em
   }
}

How can I convince the listings package that those strings like every axis x label have to be treated as identifiers? I don't like the idea of assigning the role of keyword to the single substrings every, axis, x, label.

share|improve this question
2  
Alternatively, you can use special markers for these kind of keywords, such as enclosing them within colons. For instance :every axis x label: and so on... Listings can highlight everything between the colons (without printing them of course). See this question. –  guillem Nov 20 '12 at 7:21
    
Potential duplicate: tex.stackexchange.com/questions/72999/… –  Christian Feuersänger Jan 15 at 18:59

2 Answers 2

up vote 7 down vote accepted

According to the listings package documentation this is an explicit impossibility. Quoted, from page 21 (with some emphasis):

Another problem: by default some characters are not allowed inside keywords, for example -, :, ., and so on. The reference guide covers this problem by introducing some more keys, which let you adjust the standard character table appropriately. But note that white space characters are prohibited inside keywords.

For small instances, there exists no viable alternative at the moment within listings. However, for larger or more general instances, the code examples included in the pgf package documentation (using the codeexample environment) may be of some help.

share|improve this answer
    
So, I wonder how did the authors of tikz and pgfplots achieve the pretty printing of their code with listings. –  agodemar Sep 6 '11 at 16:36
1  
Furthermore, it seems unlikely that a small amount of TeX hacking will make it convenient to use space characters valid in keywords, because they are most often used to determine the boundaries of keywords: any keyword which was bracketed by space characters would not be recognised. –  Niel de Beaudrap Sep 6 '11 at 16:39
4  
The automated highlighting in pgfplots and tikz is proprietary, it has been written from scratch in order to support the pgfkeys environment. These manuals do not use listings. In this particular context, spaces are part of keywords and the delimiters are well-defined. Unfortunately, the library which does the highlighting is not yet ready as separate package (although it is used for at least pgfmanual.pdf, pgfplots.pdf, and pgfplotstable.pdf and is general enough for more pgfkeys based approaches). –  Christian Feuersänger Sep 6 '11 at 20:29
1  
@Christian: Only the first page of the current pfg manual uses listings to typeset the code. As such, you'll notice highlights occurring only for single-words. Does "proprietary" imply that the pgf documentation will not compile in the hands of the general public? –  Werner Sep 6 '11 at 20:48
    
@Werner the pgf manual can be compiled by anyone who wants to do so (the same holds for pgfplots). I wanted to express that the underlying highlighting routines are subroutines which have been written by me with pgfplots and pgf in mind. But so far, I only planned to provide the necessary code-cleanup and documentation to make them usable for other packages. You can use them for your package if you are willing to read the code comments and the code as such. –  Christian Feuersänger Sep 7 '11 at 17:43

You can hijack the moredelim key to bend listings to your will... sort of. See below.

Note that this approach is far from perfect. For instance, with the code below, any character sequence starting by every axis x and ending with label will be highlighted like a bona fide keyword does. However, you might be able to put it to good use.

related: How to add a keyword with a blank space in Listings package?

enter image description here

\documentclass{article}

\setlength\parindent{0pt}

\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{mytikzPGFstyle}{
    language = TeX,
    basicstyle = \ttfamily,
    keywordstyle = \color{blue},
    frame = single,
    moredelim = [s][keywordstyle]{every\ axis\ x\ }{label},
    moredelim = [s][keywordstyle]{every\ axis\ y\ }{label},
}

\begin{document}

Success:
\begin{lstlisting}[style=mytikzPGFstyle]
\pgfplotsset{
   every axis x label/.append style = {
      font = \small
   },
   every axis y label/.append style = {
      font = \small,
      rotate = -90,
      xshift = 0.5em
   }

   \draw node {every, axis, label, x}; 
}
\end{lstlisting}

Failure:
\begin{lstlisting}[style=mytikzPGFstyle]
every axis x foo label/.append style = {
\end{lstlisting}
\end{document}
share|improve this answer
    
I don't understand how, but the documentation of microtype uses a “ ” (non-breaking space) between some keywords, e.g., no ligatures, and it's correctly highligted. I might post question in the future. EDIT: Although it's not viewable, the character is a non-breaking space (in Mac: opt + space bar). –  Manuel Jun 14 at 22:51

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.