# \usetikzlibrary{positioning} in an expl3 package but PGF Math Error: Unknown function of'

EDIT: The problem I was encountering was due to a regex replace, which I did not include in the original question. The problem is that I was replacing spaces in a token list with a control space \. (I shouldn't have done this, and have since resolved the problem via the answer provided here). The first issue is that the control space in a tikz option/property seems to allow that control space \. That's why everything was working for me until I flipped things around with the right=of nodename. Apparently, tikz does not like that control space after the =. The second issue is that the error message give no indication that the space is a problem. Instead, the error message seems to indicate that of is unrecognized, which suggests that the the control space is recognized as a normal space.

Unfortunately, I did not include these details originally, because the error message gave no indication of the space (modified by the regex) being the issue. Fixing the regex, fixed the problem because the normal space from the token list that underwent a regex replace maintained the correct space.

Inside an expl3 package, I am using tikz and loading \usetikzlibrary{positioning}. The reason for this is to transition from the deprecated right of=nodename to the current right=of nodename which requires the positioning library.

I used the solution here (Using of \usetikzlibrary in an expl3 package fails) to fix the usage of various tikz libraries within the expl3 package. Everything worked fine until I loaded the positioning library and then try to use the right=of nodename notation. It gives me there error: ! Package PGF Math Error: Unknown function 'of' (in 'of nodename').

I know that the library is installed correctly on my system because I can use the notation in a standard article document and use it without error. The issue seems to be connected to expl3, tikz, and positioning in a package, but I'm not really sure how to diagnose the problem further or fix it.

Obligatory MWE:

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{some-package.sty}

\RequirePackage{expl3}
\ProvidesExplPackage
% Package name
{some-package}
% Release date
{2017/03/15}
% Release version
{1.0}
% Description
{some package}

\RequirePackage{l3regex}%
\RequirePackage{xstring}%
\RequirePackage{xparse}%

\RequirePackage{tikz}%
%%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\RequirePackage{etoolbox}
\ExplSyntaxOff
\patchcmd
{\pgfutil@InputIfFileExists}
{\input #1}
{%
\@pushfilename
\input #1 %
\@popfilename
}
{}{}
\ExplSyntaxOn
\usetikzlibrary{backgrounds}%
\usetikzlibrary{calc}%
\usetikzlibrary{arrows.meta}%
\usetikzlibrary{positioning}%

\DeclareDocumentCommand{\makeapicture}{ }
{%
\begin{tikzpicture}

\node (test) [fill,inner~sep=2pt,label={[left~of~=~test]test}] at ( 4 , 3 ) {};% This works fine

\node (test2) [fill,inner~sep=2pt,label={[left=of~test2]test2}] at ( 4 , 3 ) {};% This will give the error

\end{tikzpicture}
}

\end{filecontents}
\usepackage{some-package}

\begin{document}

\makeapicture

\end{document}


Importantly, loading the positioning library does not cause any errors. It is only once I define a node using the non-deprecated notation that the error gets triggered. It seems as though the positioning library is simply not loaded or something.

EDIT: I thought that perhaps the order of the libraries caused the problem and tried \usetikzlibrary{positioning} first, last, and in between each other library, but this did nothing.

EDIT2: expl3 code is not included in this example because it has no issues. Several document commands and internal command sequences, however, set up tikzpictures and create \nodes. All was working well until I decided to convert from the deprecated right of=nodename to the positioning version right=of nodename, as I mentioned at the top. Literally, the only change that I made from a completely working package to one with this error is a) include the positioning library and b) change the location syntax.

Also, changing this section:

\ExplSyntaxOff
\patchcmd
{\pgfutil@InputIfFileExists}
{\input #1}
{%
\@pushfilename
\input #1 %
\@popfilename
}
{}{}
\ExplSyntaxOn
\usetikzlibrary{backgrounds}%
\usetikzlibrary{calc}%
\usetikzlibrary{arrows.meta}%
\usetikzlibrary{positioning}%


to

\ExplSyntaxOff
\patchcmd
{\pgfutil@InputIfFileExists}
{\input #1}
{%
\@pushfilename
\input #1 %
\@popfilename
}
{}{}

\usetikzlibrary{backgrounds}%
\usetikzlibrary{calc}%
\usetikzlibrary{arrows.meta}%
\usetikzlibrary{positioning}%

\ExplSyntaxOn


does not work either.

• That's not an MWE. We can't produce the error you report if we try to compile it. But you need to switch expl3 syntax off before processing the TikZ stuff. left of is going to be read as leftof because spaces are not spaces. It isn't obvious why you want the expl3 syntax here at all, but you certainly don't want it for TikZ. – cfr Mar 15 '17 at 4:34
• What is the fix to do? The link you gave has nothing to do with expl3. – cfr Mar 15 '17 at 4:35
• Switch the syntax off before loading the libraries and defining the macro which needs TikZ. TikZ's syntax is fundamentally allergic to L3's. It relies on spaces being correct. – cfr Mar 15 '17 at 4:44
• I guess you mean tex.stackexchange.com/questions/57424/…. – cfr Mar 15 '17 at 4:50
• What the patch does affects the loading of the libraries. It doesn't 'fix TikZ to work in the L3 environment'. – cfr Mar 15 '17 at 4:54

\documentclass{article}
\usepackage{expl3,etoolbox,tikz}
\makeatletter
\ExplSyntaxOff
\patchcmd
{\pgfutil@InputIfFileExists}
{\input #1}
{%
\@pushfilename
\input #1%
\@popfilename
}
{}{}
\ExplSyntaxOn
\makeatother
\usetikzlibrary{positioning}
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\node (a) {a};
\node [left=of a] {b};
\end{tikzpicture}
\end{document}


The fix fixes the input of the libraries. It is not a general fix for the use of TikZ code when expl3 syntax is active. That would be extremely difficult, if not impossible, since the two syntaxes are fundamentally allergic to each other. TikZ essentially uses its own parser and relies on things being as it expects.

The solution is to turn the syntax off before defining your macro, if required.

\ExplSyntaxOff
\NewDocumentCommand\makeapicture{}
{%
\tizk{...}%
}
\ExplSyntaxOn
`