Problematic interaction between ltxdoc and tikz matrices

I have been spending some time trying to solve a problem in the interaction between TikZ matrices and the ltxdoc document class. While using ltxdoc, I cannot use the |[options]| syntax at the beginning of a cell to change the properties of a specific node in a matrix of nodes. This functionality is documented on page 375 of TikZ manual.

The following snippet provides a minimal example reproducing the problem.

\documentclass[a4paper]{ltxdoc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes, nodes in empty cells]{
% if |[red]| is removed, or the documentclass is
% changed to article, it works all right
first & |[red]| second & third \\
};
\end{tikzpicture}
\end{document}


Compilation fails with the following message:

! Argument of \tikz@lib@matrix@with@options has an extra }.


Everything works fine if I change the documentclass (e.g., to article) or if I remove the |[red]| part. I am using extensively this functionality, and I had no problem whatsoever until I switched to ltxdoc.

I inspected things in tikzlibrarymatrix.code.tex, but everything seems all right there. I am using ltxdoc version 2007/11/11 v2.0u and TikZ 2.10.

-
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – Werner Nov 6 '11 at 15:57
ltxdoc has the following in its code: \AtBeginDocument{\MakeShortVerb{\|}}. I assume that is what is causing the problem. If I understand correctly it makes |x| a verbatim x. – Roelof Spijker Nov 6 '11 at 15:58
Looking into the pfgmanual sources, I found that the snippets of code using the |[options]| syntax are wrapped within {\catcode\|=12 <snippet> }. If I adopt the same strategy, the problem is fixed. Still, I don't understand exactly why is it happening. Is it related to @wh1t3's comment? – masterpiga Nov 6 '11 at 16:01
That changes the catcode to other. Presumably the MakeShortVerb macro makes the pipe active. The grouping by the braces causes it to become other only locally and thus doesn't interfere with ltxdoc Normally the catcode of | is other as well, so in that case, nothing special happens. – Roelof Spijker Nov 6 '11 at 16:10

As mentioned by @wh1t3, ltxdoc issues \AtBeginDocument{\MakeShortVerb{\|}} within its preamble. The class documentation states the following:

Make | be a "short verb" character, but not in the document preamble, where an active character may interfere with packages that are loaded.

immediately after loading the doc package - which defines \MakeShortVerb. The easiest way to fix this is to issue

\AtBeginDocument{\DeleteShortVerb{\|}}% | -> catcode "other"


which effectively does the same as

\AtBeginDocument{\catcode\|=12}% | -> catcode "other"


in your preamble. This will delay execution until \begin{document} (outside the document preamble) and reverse/override the definition of \MakeShortVerb{\|} set by the ltxdoc document class. Of course, you could also execute \DeleteShortVerb{\|} after \begin{document}, if you wish.

This should not cause any side effects, since you could define your own (other) abbreviated verbatim command(s) as required.

\documentclass[a4paper]{ltxdoc}% http://ctan.org/pkg/ltxdoc
\AtBeginDocument{\DeleteShortVerb{\|}}% | -> catcode "other"
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes, nodes in empty cells]{
first & |[red]| second & third \\
};
\end{tikzpicture}
\end{document}

-
Also \DeleteShortVerb{\|} after \begin{document}, which is simpler. – egreg Nov 6 '11 at 17:10
@egreg: Correct. – Werner Nov 6 '11 at 17:12