修补 tikzscale

修补 tikzscale

我正在尝试修补该软件包tikzscale。具体来说,我想从软件包中删除以下几行,因为它在我的用例[1]中不构成有用的错误。

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

我还尝试在不编辑包本身的情况下执行此操作,因为我希望与其他人(以及在 Overleaf 上)共享此文档,在这种情况下我无法请求其他人编辑他们在包上的安装,所以我尝试使用regexpatch、 或 来执行此操作xpatch

xpatch失败:

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失败:

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)
*************************************************

有没有什么办法可以解决这个问题?这是一个 MWE(虽然编译时没有问题,但检查日志会发现修补失败)。

\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] 我的用例是这样的,如果它很重要:我正在编写.tikz文件,这些文件本身有\includegraphics,在 tikz 中注释。当我尝试将它们包含到我的主文档中时,它们会抛出错误Requested to scale unscalable graphic,但是,文档可以正确编译,但在 Overleaf 上会失败。

.tikz我试图包含的一个例子:

\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}

答案1

该命令\tikzscale@scaleTikzpictureTo是用 定义的\NewDocumentCommand,既不支持xpatch也不regexpatch支持直接修补此类命令。

如果你\xshowcmd\tikzscale@scaleTikzpictureTo在尝试补丁之前这样做,你会看到

> \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 }.

在控制台上,这清楚地解释了为什么补丁不起作用:您想要替换的文本从一开始就不存在。

了解一点xparse就能知道要修补的真正宏是什么。提示:它的名称中有一个空格;\expandafter可以使用标准技巧将其作为参数传递给\xpatchcmd

还要注意\regexpatchcmd需要l3regex语法(中的第 XXVI 部分interface3.pdf);您使用了适合的语法\xpatchcmd

相关内容