由于 \patchcmd 而产生的额外空间

由于 \patchcmd 而产生的额外空间

看起来\patchcmdfrometoolbox添加了三个不需要的空格,如下所示:

\documentclass{minimal}
\usepackage{etoolbox}
\begin{document}
\tracingall%                            3 spaces in log file
\def\test{1}]\patchcmd\test{1}{2}{}{}[% 3 spaces between brackets in output
\end{document}

我怎样才能解决这个问题?

答案1

两个空格来自 的调用\scantokens,第三个空格来自代码中未受保护的行尾。

\documentclass{minimal}
\usepackage{etoolbox}

\makeatletter
% Add \ifhmode\unskip\fi
\protected\def\etb@ifscanable#1{%
  \begingroup
  \edef\etb@resrvda{%
    \def\noexpand\etb@resrvda####1\detokenize{macro}:####2->####3&{%
      ####1\def\string\etb@resrvda####2{####3}}%
    \edef\noexpand\etb@resrvda{\noexpand\etb@resrvda\meaning#1&}}%
  \etb@resrvda
  \makeatletter
  \scantokens\expandafter{\etb@resrvda}%
  %%% ADDITION
  \ifhmode\unskip\fi
  %%% END ADDITION
  \expandafter\endgroup\ifx#1\etb@resrvda
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\def\etb@patchcmd@scantoks#1{%
  \edef\etb@resrvda{\endgroup
    \unexpanded{\makeatletter\scantokens}{#1}%
    \catcode\number`\@=\the\catcode`\@\relax}%
  \etb@resrvda
  %%% ADDITION
  \ifhmode\unskip\fi
  %%% END ADDITION
}
% Fix a missing `%'
\protected\long\def\etb@ifpattern#1#2{%
  \begingroup
  \edef\etb@resrvda{%
    \def\noexpand\etb@resrvda####1\detokenize{#2}####2&{%
      \endgroup\noexpand\noexpand\noexpand\ifblank{####2}}%
    \edef\noexpand\etb@resrvda{\noexpand\etb@resrvda
      \expandafter\strip@prefix\meaning#1\detokenize{#2}&}%
    \noexpand\etb@resrvda}% <---------- MISSING IN etoolbox.sty
  \etb@resrvda\@secondoftwo\@firstoftwo}

\makeatother


\begin{document}
\def\test{1}]\patchcmd\test{1}{2}{}{}[% brackets for checking

][% for check
\end{document}

在此处输入图片描述

但是,最好的策略是永远不要使用\patchcmd水平模式。

相应的命令\xpatchcmdregexpatch存在这个问题。


\@empty不同的补丁可以在两个“\scantokens”的末尾添加:

\protected\def\etb@ifscanable#1{%
  \begingroup
  \edef\etb@resrvda{%
    \def\noexpand\etb@resrvda####1\detokenize{macro}:####2->####3&{%
      ####1\def\string\etb@resrvda####2{####3}}%
    \edef\noexpand\etb@resrvda{\noexpand\etb@resrvda\meaning#1&}}%
  \etb@resrvda
  \makeatletter
  \scantokens\expandafter{\etb@resrvda\@empty}% <---- ADDED
  \expandafter\endgroup\ifx#1\etb@resrvda
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\def\etb@patchcmd@scantoks#1{%
  \edef\etb@resrvda{\endgroup
    \unexpanded{\makeatletter\scantokens}{#1\noexpand\@empty}% <---- ADDED
    \catcode\number`\@=\the\catcode`\@\relax}%
  \etb@resrvda}

在第二种情况下,\noexpand\@empty是需要的,因为我们在里面\edef。然而,这需要比我愿意做的更多的测试。

我本来想用它\patchcmd自己来修补这两个命令,但它们却拒绝了。

相关内容