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:

\makeatletter \input{pgfmanual.prettyprinter.code} \input{pgfmanual.pdflinks.code} \makeatotherI 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. Runningmakeindexdoesn't help either. – Martin Scharrer Nov 07 '11 at 10:28\makeatletter? I didn't understand, sorry. – masterpiga Nov 07 '11 at 10:38@as letter, so you need to use\makeatletterbeforehand if you load them from a document preamble directly. – Martin Scharrer Nov 07 '11 at 10:41pgfmanual.styis 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:56pgfmanual-en-macrosshould load them and because I couldn't find them there I loaded them manually. About the\makeatletter: As I wrote, you only need\makeatletterif 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\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:44pgfmanual.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#\expandafter1which I have never seen before... – Roelof Spijker Nov 07 '11 at 15:26the only necessary thing is that \pgfmanualpdflabel has been called for every (fully qualified) key, control sequence, environment or whathever.In my example, thecommandenvironment (defined inpgfmanual-en-macros.tex) among the other things also wraps a call to\pgfmanualpdflabel. I have verified (with\show) that\pgfmanualpdflabelis invoked with the right value, hence I would expect\sillymacroto show up highlighted in the code example. By the way, I have no compilation problem whatsoever. – masterpiga Nov 07 '11 at 15:44inputit 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:57pgfmanual.pdflinks.code.texis input bypgfmanual.code.tex, which in turn is input bypgfmanual.sty. Actually, the only thing thatpgfmanual.styis 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