7

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.

8

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.

  • 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
3

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}
  • 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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.