可以同时使用 fancyvrb、eso-pic 和 TikZ 吗?

可以同时使用 fancyvrb、eso-pic 和 TikZ 吗?

考虑 MWE

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{tikz}
\usepackage{eso-pic}
\AddToShipoutPictureFG{%
    \begin{tikzpicture}[remember picture, overlay]
        \node[anchor=north east] at (current page.north east) {Hello};
    \end{tikzpicture}}
\begin{document}
\tikz{\fill[blue](0,0)rectangle(1,1);}
\VerbatimInput{tikzducks.sty}
\end{document}

它会产生错误

! Package tikz Error: Sorry, some package has redefined the meaning of the math
-mode dollar sign. This is incompatible with tikz and its calc library and migh
t cause unrecoverable errors.

(输出正常,但是有此错误消息。)

当我注释掉这些行时,错误消息就会消失

\usepackage{eso-pic}
\AddToShipoutPictureFG{%
    \begin{tikzpicture}[remember picture, overlay]
        \node[anchor=north east] at (current page.north east) {Hello};
    \end{tikzpicture}}

或者如果我注释掉

\VerbatimInput{tikzducks.sty}

我的结论(也许是错误的)是,fancyvrb、eso-pic 和 TikZ 这三个包之间存在微妙的干扰,这也许就是为什么我在这里找不到任何关于这方面的讨论。

问题: 有没有什么办法可以治愈这个病?

答案1

此处的错误消息是误报:即使图片使用$,由于它们已被标记,因此它们可以正常工作。tikz 的检查不会阻止以下错误,因为它不会重置 catcode。我会重新定义检查 catcode 的 tikz 命令$以放松或发出警告。在我看来,这已经足够了。

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{tikz}

\makeatletter
% or \let\tikz@ensure@dollar@catcode\relax
\def\tikz@ensure@dollar@catcode{%
  \ifnum\catcode`\$=3 %
  \else
    \pgfutil@packagewarning{tikz}{Sorry, some package has redefined the meaning of the
      math-mode dollar sign. This is incompatible with tikz and its calc
      library and might cause unrecoverable errors}%
    % only show warning once:
    \global\let\tikz@ensure@dollar@catcode=\relax
  \fi
}%

\makeatother
\usepackage{eso-pic}
\AddToShipoutPictureFG{%
    \begin{tikzpicture}[remember picture, overlay]
        \node[anchor=north east] at (current page.north east) {Hello};
    \end{tikzpicture}}
\begin{document}
\tikz{\fill[blue](0,0)rectangle(1,1);}
\VerbatimInput{tikzducks.sty}
\end{document}

答案2

页面头部覆盖中的逐字设置不会被重置,这是eso-pic您可以采取的解决方法

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{tikz}
\usepackage{eso-pic}

\AddToShipoutPictureFG{%
    \catcode`$=3 
    \begin{tikzpicture}[remember picture, overlay]
        \node[anchor=north east] at (current page.north east) {Hello};
    \end{tikzpicture}}
\begin{document}

\tikz{\fill[blue](0,0)rectangle(1,1);}
\VerbatimInput{tikzducks.sty}
\end{document}

相关内容