17

I am writing docs for a tikz-based package, and I am trying to reuse Till Tantau's codeexample environment to output source code along with its output. However, I cannot manage to get syntax highlighting (that is, automatic linking of keywords) to work.

[EDIT: as it turns out, the pdflinks / syntax highlighting layer is Christian Feuersänger's work]

This is a minimal example reproducing the problem:

\documentclass[a4paper]{ltxdoc}
\usepackage[a4paper,left=2.25cm,right=2.25cm,top=2.5cm,bottom=2.5cm,nohead]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{makeidx}
\makeindex
\usepackage{hyperref}
\hypersetup{%
        colorlinks=true,
        linkcolor=blue,
        filecolor=blue,
        urlcolor=blue,
        citecolor=blue,
        pdfborder=0 0 0,
}
\usepackage{tikz}

% pgfmanual.sty inputs pgfmanual.code.tex, which in turn inputs
% pgfmanual.prettyprinter.code.tex and pgfmanual.pdflinks.code.tex.
% All the files are in the same dir as this file.
\usepackage{pgfmanual}

\usepackage{calc} % NEEDED by pgfmanual-en-macros
\input{pgfmanual-en-macros}
\newcommand{\options}[1][options]{\opt{\oarg{#1}}}
\newcommand{\sillymacro}[2][]{
   \node [shade, top color = black, bottom color = green, %
          text = white, thick, rounded corners, font=\bfseries, #1] {#2};
}
\begin{document}
   \begin{command}{\sillymacro\options\marg{contents}}
      Defining a command via the command environment is supposed to automatically index it, isn't it?
      In fact, an entry is correctly generated in the index.
      Howeverm I would expect |\sillymacro| to be treated as a link, but it doesn't really work.
      \begin{codeexample}[]
         \begin{tikzpicture}
            \sillymacro{Uh-uhhhh! I am fancy}
         \end{tikzpicture}
      \end{codeexample} 
   \end{command}
   No linking inside the example, no |\sillymacro| linking outside.
   \printindex
\end{document}

I would expect automatic linking to happen, but it doesn't. All the files appear to be in the right place, and I get no warning whatsoever. In pgfmanual.code.tex, the key /pdflinks/codeexample link is set to true. I have also activated /pdflinks/show labels, but I cannot see any difference.

EDIT

Maybe I should make clearer that the example compiles all right, and gives no error, but it does not produce the expected result. The only problem is that the automatic linking of the keywords with their definitions and the consequent highlighting of the keywords in the code examples do not work.

As I wrote in the code example, the command environment takes care of extracting the names of the command and indexing it. Things are working up to this point, in fact the entry is correctly indexed and shown in the index at the end of the document. What I am trying to understand is if there is something that I do that is preventing automatic linking from working, or if there is something that I am supposed to do and didn't.

EDIT (post-answer)

After Christian brilliantly answered my question below, I actually tried to make it work. Not without surprise, with respect to the code above I discovered that only adding \def\pgfautoxrefs{1} at the beginning of the document would only partially solve the problem. In-text auto-linking using |\sillymacro| would work as expected, while syntax highlighting in the code example would still be broken.

To make it totally work, one must replace the statement

\usepackage{pgfmanual}

with

\makeatletter
\input{pgfmanual.code}
\makeatother

This is quite puzzling, since all my pgfmanual.sty does is:

\input pgfmanual.code.tex

So, in the end, the complete working example is as follows:

\documentclass[a4paper]{ltxdoc}
\def\pgfautoxrefs{1} % !!!!!!
\usepackage[left=2.25cm,right=2.25cm,
            top=2.5cm,bottom=2.5cm,nohead]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{makeidx}
\makeindex
\usepackage{hyperref}
\hypersetup{%
        colorlinks=true,
        linkcolor=blue,
        filecolor=blue,
        urlcolor=blue,
        citecolor=blue,
        pdfborder=0 0 0,
}
\usepackage{tikz}

% pgfmanual.code.tex in turn inputs
% pgfmanual.prettyprinter.code.tex and pgfmanual.pdflinks.code.tex.
% (I am using pgf 2.10 sources)
\makeatletter          % !!!!
\input{pgfmanual.code} % !!!!
\makeatother           % !!!!

\usepackage{calc} % NEEDED by pgfmanual-en-macros
\input{pgfmanual-en-macros}
\newcommand{\options}[1][options]{\opt{\oarg{#1}}}
\newcommand{\sillymacro}[2][]{
   \node [shade, top color = black, bottom color = green,%
          text = white, thick, rounded corners,%
          font=\bfseries, #1] {#2};
}
\begin{document}
   \begin{command}{\sillymacro\options\marg{contents}}
      Defining a command via the command environment is supposed
      to automatically index it, isn't it?

      In fact, now it does!
      |\sillymacro| is treated as a link, and I can happily 
      click on it.
      \begin{codeexample}[]
         \begin{tikzpicture}
            \sillymacro{Uh-uhhhh! I am fancy}
         \end{tikzpicture}
      \end{codeexample} 
   \end{command}
   It also works outside, and |\sillymacro| is also highlighted in 
   the code listing. Great, thanks!
   \printindex
\end{document}

And here is the result:

Working example of auto-linked docs generated with tikz documentation macros

  • After adding \makeatletter \input{pgfmanual.prettyprinter.code} \input{pgfmanual.pdflinks.code} \makeatother I get ! Package pgfmanual Warning: pgfmanualpdfref{\sillymacro}: target label does no t exist.. Seems that there is no label for it, so the link can't be created. Running makeindex doesn't help either. – Martin Scharrer Nov 07 '11 at 10:28
  • I get no Warning whatsoever from pgfmanual. Where did you add \makeatletter? I didn't understand, sorry. – masterpiga Nov 07 '11 at 10:38
  • I had a look at the pgfmanual source files to find you how the links are added. I figured that these two files need to be loaded as well. The need to have the @ as letter, so you need to use \makeatletter beforehand if you load them from a document preamble directly. – Martin Scharrer Nov 07 '11 at 10:41
  • As I wrote in the example code, pgfmanual.sty is already loading those two files, and it is doing no trick with @. The files that I have included in my project are from pfg 2.10 sources, and I haven't hacked them in any way. – masterpiga Nov 07 '11 at 10:56
  • Ah, ok. I thought pgfmanual-en-macros should load them and because I couldn't find them there I loaded them manually. About the \makeatletter: As I wrote, you only need \makeatletter if you load them in a document preamble. It isn't required inside a package because then @ is already a letter. – Martin Scharrer Nov 07 '11 at 11:04
  • @masterpiga FYI, there is a feature request on the pgf sourceforge page to make the whole pgfmanual macros a standalone class. – cjorssen Nov 07 '11 at 12:54
  • @cjorssen - Yep, I just checked it out. By the way, things are already very well decoupled, and if you just \usepackage{pgfmanual} you are basically ready to go. The only problem that I am having is that I cannot get automatic indexing and highlighting to work. – masterpiga Nov 07 '11 at 13:44
  • Looks like you should take a look at: pgfmanual.pdflinks.code.tex. It seems to be responsible for the behaviour you want. I haven't been able to get it to work though, get an error somewhere where there is \expandafter#\expandafter1 which I have never seen before... – Roelof Spijker Nov 07 '11 at 15:26
  • @wh1t3 I did, and it says the only necessary thing is that \pgfmanualpdflabel has been called for every (fully qualified) key, control sequence, environment or whathever. In my example, the command environment (defined in pgfmanual-en-macros.tex) among the other things also wraps a call to \pgfmanualpdflabel. I have verified (with \show) that \pgfmanualpdflabel is invoked with the right value, hence I would expect \sillymacro to show up highlighted in the code example. By the way, I have no compilation problem whatsoever. – masterpiga Nov 07 '11 at 15:44
  • I get the error when I input it manually. I couldn't find TikZ loading it anywhere and I thought perhaps the command had been provided to something else as long as it was not loaded. – Roelof Spijker Nov 07 '11 at 15:57
  • @wh1t3 pgfmanual.pdflinks.code.tex is input by pgfmanual.code.tex, which in turn is input by pgfmanual.sty. Actually, the only thing that pgfmanual.sty is to input \input{pgfmanual.code}. The code snippet above behaves exactly in the same way if I replace \usepackage{pgfmanual} with \makeatletter \input{pgfmanual.code} \makeatother. – masterpiga Nov 07 '11 at 16:16

1 Answers1

11

The reasons for your problem is that pgfmanual-en-macros.tex explicitly disables the syntax highlighting unless it finds a command-line option which enables it.

In your case, the command-line option is not present and the file disables the syntax highlighting.

The syntax highlighting consumes quite some CPU power and is only activated if one uses make dist for the pgfmanual.

The solution for you is to type

\def\pgfautoxrefs{1}

as first statement in your document. If this macro is absent, the code pgfmanual-en-macros.tex:1286 disables it:

\makeatletter
% \pgfautoxrefs will be defined by 'make dist'
\pgfutil@ifundefined{pgfautoxrefs}{%
    \def\pgfmanualpdflabel#1#2{#2}% NO-OP
    \def\pgfmanualpdfref#1#2{#2}%
    \pgfkeys{
        /pdflinks/codeexample links=false,% DISABLED.
    }%
}{}
\makeatother

Perhaps it really is time to pack all that stuff into a self-contained package... (if there is some willing to start it, I will assist with advice. I wrote that syntax highlighting module).

  • 2
    As you can see, the requests will be coming in now! :) – Werner Nov 07 '11 at 18:21
  • @Christian Uh-uuuu, thanks! That was easy, if you knew where to put the hands. The package is already pretty much self-contained, and it is also decently documented. The only obstacle to release it as a package seems to remove some redundancy and devote some effort to documentation. In this case, I was sure that there was some flag to set, but I was confused by lines 39-43, from which you would understand (well, I did at least) that to activate/deactivate highlighting you only had to toggle /pfdlinks/codeexample links. – masterpiga Nov 07 '11 at 18:27
  • 1
    @Werner did you just volunteer? ;-) – Christian Feuersänger Nov 07 '11 at 18:29
  • @ChristianFeuersänger: Wasn't there a recent request for this? – Werner Nov 07 '11 at 18:32
  • @masterpiga the /pdflinks/codeexample links was also what I had in mind. I had to search a while until I understood it. perhaps the pgfmanual should move its command line parsing to the main .tex file. – Christian Feuersänger Nov 07 '11 at 18:40
  • @Werner now I am confused ... :( What precisely do you mean? – Christian Feuersänger Nov 07 '11 at 18:40
  • @ChristianFeuersänger: I remember someone asking a similar question regarding the documentation of PGF, and wanting the formatting of code words with linkages. Maybe I was dreaming. Either way, with this request I'm sure there will be more supporting the standalone-ness of the PGF syntax highlighting module. – Werner Nov 07 '11 at 18:43
  • 2
    @Werner ah, ok, thanks for the explanation. You are right about more and more package authors asking for this feature. In fact, I received a couple of feature request per mail in the past. Nevertheless, I seem to have a never-ending todo list for my active spare-time projects... and the pgfmanual package would certainly need a couple of weeks (especially its documentation). And much of that stuff needs an extensible UI as well, I guess. That's why no-one has started it until now. Again: if someone wants to do it, I would appreciate it (and I would explain that thing). – Christian Feuersänger Nov 07 '11 at 18:52
  • 2
    @ChristianFeuersänger I just saw this today; if it's not too late, I'd volunteer (the idea is to pack the features providing the syntax highlight into a standalone package?). – Gonzalo Medina Jun 24 '13 at 15:36
  • 1
    @GonzaloMedina Go ahead! I am unaware of any progress in that direction. You might want to take http://tex.stackexchange.com/questions/72999/is-there-a-listings-configuration-for-tikz-as-used-in-pgf-tikz-manual/73103#73103 as a starting point. – Christian Feuersänger Jun 24 '13 at 16:08
  • 1
    @GonzaloMedina, just because I am curious: Did you find some time to make a standalone package? – Stefan Pinnow Jun 04 '17 at 11:47