# Patching tikzscale

I'm trying to patch the package tikzscale. Specifically, I want to remove the following lines from the package, as it does not constitute a useful error in my usecase[1].

\tikzscale@ifSizeDifference{\measuredFirst - \measuredSecond}{%
}{%
\PackageError{tikzscale}{Requested to scale unscalable graphic}{Do not set width or height for graphic in\MessageBreak #3}%
}%

I am additionally attempting to do this without editing the package itself, as I expect to share this document with other people (and on Overleaf) in such a situation that I cannot request others to edit their installs on the package, so I'm trying to do this with regexpatch, or xpatch.

xpatch fails with:

File: etoolbox.def 2018/02/11 v2.5e etoolbox debug messages (JAW)
)
[debug] tracing \patchcmd on input line 19
[debug] analyzing '\tikzscale@scaleTikzpictureTo'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] -- macro cannot be retokenized cleanly
[debug] -> the macro may have been defined under a category
[debug]    code regime different from the current one
[debug] -> the replacement text may contain special control
[debug]    sequence tokens formed with \csname...\endcsname;
[debug] -> the replacement text may contain carriage return,
[debug]    newline, or similar characters

regexpatch fails with:

File: etoolbox.def 2018/02/11 v2.5e etoolbox debug messages (JAW)
)
*************************************************
* xpatch message
* \tikzscale@scaleTikzpictureTo' is not especially defined
*************************************************
*************************************************
* xpatch message
* Macro\tikzscale@scaleTikzpictureTo' is NOT patchable
* (Check if it contains `@' commands)
*************************************************

Is there a way in which this can work? Here is an MWE (while this will compile with no issues, inspecting the log will reveal the failure to patch).

\documentclass[twocolumn,twoside]{article}

\usepackage{regexpatch}
\usepackage{tikzscale}
\usepackage{lipsum}
\usepackage{tikz}

\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\tracingpatches
\tracingxpatches
\makeatletter
\regexpatchcmd{\tikzscale@scaleTikzpictureTo} % Command to patch
{\PackageError{tikzscale}{Requested to scale unscalable graphic}.*} % Content to be replaced
{\relax} % Replacement content
{\message{Succesfully patched tikzscale!}} % Success message
{\message{Failed to patch tikzscale!}} % Failure message
\makeatother

\begin{document}
\lipsum
\end{document}

[1] My usecase is this, if it matters: I am writing .tikz files which themselves have \includegraphics, annotated in tikz. When I attempt to include them in my main document, they throw me the error Requested to scale unscalable graphic, however, the document compiles correctly, however, it fails on Overleaf.

An example of the .tikz I'm trying to include:

\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0){
\includegraphics[width=0.95\linewidth]{figure2.pdf}
};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
% (a) (b) (c) (d) labels
\node at (0.02,0.97){\textbf{(a)}};
\node at (0.40,0.97){\textbf{(b)}};
\node at (0.67,0.97){\textbf{(c)}};
\node at (0.21,0.66){\textbf{(d)}};
\node at (0.02,0.39){\textbf{(e)}};

% Inset label
\fill [white] (0.23,0.53) rectangle (0.30,0.62);
\fill [white] (0.65,0.53) rectangle (0.75,0.62);
\node at (0.67,0.41) {Diffraction losses};
\node[anchor=south west] at (0.23,0.53) {$\bm{|\psi\rangle}$};
\node[anchor=south west] at (0.65,0.53) {$\bm{T|\psi\rangle}$};

% Optical component labels
\node at (0.14,0.35){\small Laser};
% \node at (0.07,0.32){\small Fiber};
\node at (0.14,0.01){\small Pol$_1$};
\node at (0.23,0.01){\small HWP};
\node at (0.31,0.01){\small BS};
\node at (0.30,0.32){\small Det$_B$};
% \node at (0.40,0.01){\small Lens};
% \node at (0.46,0.35){\small Metasurface};
% \node at (0.53,0.01){\small Lens};
\node at (0.46,-0.02){\small Metasurface};
\node at (0.70,0.01){\small Fourier lens};
\node at (0.74,0.20){\small QWP};
\node at (0.83,0.20){\small Pol$_2$};
\node at (0.95,0.03){\small Det$_A$};

%\draw[red,ultra thick,rounded corners] (0.62,0.65) rectangle (0.78,0.75);
\end{scope}
\end{tikzpicture}
• Welcome to TeX.SE! I'd recommend to also present a real world example that shows how you want to use the patch, perhaps there is a simpler possibility. – marmot Jun 8 '18 at 5:48
• First of all, \regexpatchcmd requires a very different syntax; but that's not the problem: the macro \tikzscale@scaleTikzpictureTo does not contain the tokens you'd like to replace, because it's defined with \NewDocumentCommand (and so patching it is not covered by regexpatch). – egreg Jun 8 '18 at 6:26
• I would try to avoid to nest tikzpictures. It can work, but they can be also side effects. See e.g. tex.stackexchange.com/a/66037/2388. Beside this: before getting rid of an error, you should try to understand why the author added the test and the error message. – Ulrike Fischer Jun 8 '18 at 8:36
• It cannot 'compile correctly' if you get an error. Really, nesting tikzpictures ought not be done and can always (?) be avoided. – cfr Jun 9 '18 at 3:05
• It compiles correctly insofar as the document is actually rendered as expected, despite the error coming up! I have no idea why, either. Also, I'm not trying to nest tikz pictures - my main document uses \includegraphics to include a .tikz file, which has an \includegraphics of a .pdf file. I've edited my post with an example. – Firnagzen Jun 12 '18 at 2:48

## 1 Answer

The command \tikzscale@scaleTikzpictureTo is defined with \NewDocumentCommand and neither xpatch nor regexpatch support patching such commands directly.

If you do \xshowcmd\tikzscale@scaleTikzpictureTo before trying the patch, you'll see

> \tikzscale@scaleTikzpictureTo=\protected
macro:->\__xparse_start_expandable:nNNNNn {mmm}\tikzscale@scaleTikzpictureTo
\tikzscale@scaleTikzpictureTo  \tikzscale@scaleTikzpictureTo code
?{\__xparse_expandable_grab_m:w \__xparse_expandable_grab_m:w
\__xparse_expandable_grab_m:w }.

on the console and this clearly explains why the patch doesn't work: the text you'd like to replace isn't there to begin with.

A little knowledge of xparse will tell you what is the real macro to patch. Hint: it has a space in its name; a standard \expandafter trick can be used for passing it as argument to \xpatchcmd.

Beware also that \regexpatchcmd requires l3regex syntax (part XXVI in interface3.pdf); you used a syntax proper to \xpatchcmd.