9

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.

jub0bs
  • 58,916
agodemar
  • 2,023
  • 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 07:21
  • Potential duplicate: http://tex.stackexchange.com/questions/72999/is-there-a-listings-configuration-for-tikz-as-used-in-pgf-tikz-manual/73103#73103 – Christian Feuersänger Jan 15 '14 at 18:59

3 Answers3

10

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.

Werner
  • 603,163
  • So, I wonder how did the authors of tikz and pgfplots achieve the pretty printing of their code with listings. – agodemar Sep 06 '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 06 '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 06 '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 06 '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 07 '11 at 17:43
  • @Christian Could you please provide a pointer to start using these routines, for a minimal example like the one reported above? – agodemar Sep 07 '11 at 18:56
  • @agodema In principle, yes... unfortunately, that code is rather involved. Reducing it to a minimal example is equivalent to writing a new package 'pgfkeysdoc' or something like that. I will look into it to find at least some starting point for your, yes. The key is the codeexample environment of the pgfmanual styles; it should run more-or-less out of the box - once you include the pgfmanual styles (which are shipped with pgf). But that will enable all of the pgfmanual styles, perhaps some that you do not want. – Christian Feuersänger Sep 08 '11 at 16:30
  • @Christian Ok, thanks. I started looking at the code and learnt about codexample. Well, the code is actually rather involved. I think that if one has to report only few lines of code with listings, it might be better escaping to LaTeX and coloring those fragments 'by hand'. – agodemar Sep 09 '11 at 10:16
  • @agodemar I fear that you are right. These styles are certainly quite cool - but only if they have been prepared to work in a more general environment. But for now, they are quite involved as you say. – Christian Feuersänger Sep 09 '11 at 20:26
5

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}
jub0bs
  • 58,916
  • 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 '14 at 22:51
1

One can also use the "literate programming" experimental feature.

For example, I prefer to typeset pseudo-code with listings as I find it more convenient to type plain text instead of the special syntax of specialized packages like algorithmicx et al.

For assignments I want to spell out "takes the value of". I achieve this by adding the following to my pseudo-language definition (\lstdefinelanguage) :

literate=*{:=}{{\hbox{\textbf{takes the value of}}}}{18}

This also works for keywords with (French) accented letters, which are not recognised as ordinary keywords either.

ysalmon
  • 1,160