# Using of \usetikzlibrary in an expl3 package fails

In the example below I provided a small test package mytestpackage.sty which uses the syntax of expl3.

If I use the command \usetikzlibrary inside the package I will get the error: Runaway argument?

v\pgfversion ]\$Header:/cvsroot/pgf/pgf/generic/pgf/frontendlayer/tikz\ETC.
! File ended while scanning use of \next.
<inserted text>
\par


The MWE:

\documentclass{report}
\usepackage{filecontents}

\begin{filecontents}{mytestpackage.sty}
\RequirePackage{expl3}
\ProvidesExplPackage{mytestpackage}{2012/05/26} {0.1}{mytestpackage}
\RequirePackage{tikz}
\usetikzlibrary{calc,backgrounds,arrows,matrix}
\end{filecontents}
\usepackage{mytestpackage}
\begin{document}
foo bar
\end{document}


The first quick fix is:

\ExplSyntaxOff
\usetikzlibrary{calc,backgrounds,arrows,matrix}
\ExplSyntaxOn


How can I patch TikZ (\usetikzlibrary) to use it inside my package?

Based on this solution we can send a bug report.

-
Using TikZ in an expl3 context is quite hard, because it relies on the space being a space and probably also on : and _ having their normal category code. –  egreg May 26 '12 at 13:04
You could load tikz and the tikzlibraries before \ProvidesExplPackage –  cgnieder May 26 '12 at 13:05
@egreg: The reason is very simple -- to provide a package or class based on expl3 –  Marco Daniel May 26 '12 at 13:06
@cgnieder: This is the other solution. In the chat Joseph found the problem. –  Marco Daniel May 26 '12 at 13:06
@cgnieder: Joseph provided the answer. –  Marco Daniel May 26 '12 at 13:30

The issue arises not because of the catcode of : or _ but because of that of spaces: the code pgf uses to find versions from RCS-style data relies on the presence of a space. Perhaps the cleanest fix is to alter the behaviour of the internals of \usetikzlibary so that the LaTeX2e push/pop filename system is used. This is already updated by expl3 to deal correctly with \ExplSyntax...:

\documentclass{report}
\usepackage{filecontents}
\begin{filecontents}{mytestpackage.sty}
\RequirePackage{expl3}
\ProvidesExplPackage{mytestpackage}{2012/05/26} {0.1}{mytestpackage}
\RequirePackage{tikz}
\RequirePackage{etoolbox}
\ExplSyntaxOff
\patchcmd
{\use@@tikzlibrary}
{\input tikzlibrary\pgf@temp .code.tex}
{%
\@pushfilename
\input tikzlibrary\pgf@temp .code.tex %
\@popfilename
}
{}{}
\ExplSyntaxOn
\usetikzlibrary{calc,backgrounds,arrows,matrix}
\end{filecontents}
\usepackage{mytestpackage}
\begin{document}
foo bar
\end{document}


You could of course do the same 'directly' by copying the entire definition of \use@@tikzlibrary if you want to avoid using etoolbox, but that does not make the solution clearer so I've skipped it.

-