fancyvrb ShortVerb 与 tikz 不兼容

fancyvrb ShortVerb 与 tikz 不兼容

我一直在处理一个包含大量逐字逐句的文档,因此我正在使用fancyvrb \DeflneShortVerb{\|}。现在我想添加 tikz 图片,但整个过程失败了。有没有简单的方法可以解决这个问题?

\documentclass{article}
\usepackage{fancyvrb}
\DefineShortVerb{\|}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[circle,draw] (E) {Foo};
\end{tikzpicture}
\end{document}

错误是:

/usr/local/texlive/2011/texmf-dist/tex/generic/pgf/math/pgfmathparser.code.tex:
673: Missing \endcsname inserted.
<to be read again> 
                   \let 
l.673 ...eoperator{||}{or}        {2}{infix} {200}

以下问题:在数学模式下禁用 shortvrb讨论了在数学模式下禁用\ShortVerb,但是那里的解决方案对于这种情况不起作用。

答案1

就我个人而言,我一直都喜欢这样,但这不是这是正确方法的原因:\AtBeginDocument{\DefineShortVerb{\|}}但它足以放置\DefineShortVerb{\|}\usepackage{tikz}

 \documentclass{article}
\usepackage{fancyvrb,shortvrb} 
% possible is  \AtBeginDocument{\MakeShortVerb{\|}}   
\usepackage{tikz}
\DefineShortVerb{\|}

\begin{document}
And we can |_change_| the character before the picture.

\SaveVerb{Verb}|_OK_!_verbatim_material_!|

\begin{tikzpicture}\node at (1,0) [rectangle,draw,red,fill=yellow!25]{\UseVerb{Verb}};
\node at (1,1.1) [circle,draw,fill=blue!15] {$\|A\| \leq \epsilon$};
\end{tikzpicture} 

And we can still |_change_| the character after the picture.   

\tikz \draw (0,0) -| (2,2); 

\pgfmathparse{0||0} \verb+ \pgfmathparse{0||0} + 0 or 0 : \pgfmathresult

\pgfmathparse{0||1}  \verb+ \pgfmathparse{0||1} + 0 or 1 :  \pgfmathresult

\pgfmathparse{1||1} \verb+ \pgfmathparse{1||1} + 1 or 1 : \pgfmathresult 

\end{document} 

我使用 pgf 2.1 cvs 但我认为我总是使用 \AtBeginDocument{\MakeShortVerb{\|}}。

在此处输入图片描述

答案2

我把 undefine 命令放在每张图片的开头,并在图片之后重新启用。这看起来像一个解决方案:

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

\tikzset{execute at begin picture={\UndefineShortVerb{\|}},
         execute at end   picture={\DefineShortVerb{\|}}}
\begin{document}
And we can |_change_| the character before the picture.

\begin{tikzpicture}
\node[circle,draw] {$\|A\| \leq \epsilon$};
\end{tikzpicture}

And we can still |_change_| the character after the picture.
\end{document}

在此处输入图片描述

但不确定它是否坚固。


编辑读了评论后,我意识到上面的例子有点作弊,所以我尝试了一个没有\|但有独立|对的例子,结果失败了。为了解决这个问题,我们需要真正强制引入“un”定义。

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

\tikzset{every picture/.prefix code={\UndefineShortVerb{\|}},
         execute at end picture={\DefineShortVerb{\|}}}

\begin{document}
We can |_&$change#$\_| the character before the picture.
\tikz[baseline=-0.35em]{ \node { |A|} ; }
And we can still |change| the character after the picture. I didn't know that 
pipe pair is rendered as an emdash-like seperator.

\begin{tikzpicture}
\node[circle,draw] {|A|};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

这个问题可以用更根本的方式来处理。我在这里附上了一些我的旧补丁幻想VRB包裹。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{tikz}
\makeatletter
\tikzset{
  every begin picture/.style={every picture/.prefix code={#1}},
  every end picture/.style={execute at end picture={#1}},
  every begin picture={\FV@am@undefineshortverb},
  % 'every picture' is set in the tikz scope. Hence the following is redundant:
  % every end picture={\FV@am@defineshortverb}
}
\def\FV@am@defineshortverb{}
\def\FV@am@undefineshortverb{}
\def\FV@am@swap#1#2{#2#1}
\def\FV@am@csname#1#2{%
  \expandafter\FV@am@swap\expandafter{\csname#2\endcsname}{#1}%
}
\def\DefineShortVerb{\FV@Command{}{DefineShortVerb}}
\def\FVC@DefineShortVerb#1{%
  \edef\reserved@a{\expandafter\@car\string#1x\@nil}%
  \ifx\reserved@a\@backslashchar
    \ifcsname FV@CC@\string#1\endcsname
      \FV@Error{`\expandafter\@gobble\string#1' is already a short
        verb character}\FV@eha
    \fi
  \else
    \FV@Error{Token `\detokenize{#1}' isn't escaped}\FV@eha
  \fi
  \FVC@@DefineShortVerb#1%
}
\def\FVC@@DefineShortVerb#1{%
  \begingroup
  \lccode`\~=`#1%
  \lowercase{\endgroup
    \def\reserved@a{\edef~}\let\reserved@b~%
  }%
  \FV@am@csname\let{FV@AC@\string#1}\reserved@b
  \FV@am@csname\edef{FV@CC@\string#1}{\the\catcode`#1}%
  \FV@am@csname\let{FV@KV@\string#1}\FV@KeyValues
  \reserved@a{%
    \let\noexpand\FV@KeyValues
    \FV@am@csname\noexpand{FV@KV@\string#1}%
    \noexpand\FVC@Verb\expandafter\@gobble\string#1%
  }%
  \g@addto@macro\dospecials{\do#1}%
  \g@addto@macro\@sanitize{\@makeother#1}%
  \g@addto@macro\FV@am@defineshortverb{\DefineShortVerb#1}%
  \g@addto@macro\FV@am@undefineshortverb{\UndefineShortVerb#1}%
  \catcode`#1=\active
}
\def\UndefineShortVerb#1{%
  \edef\reserved@a{\expandafter\@car\string#1x\@nil}%
  \ifx\reserved@a\@backslashchar
    \ifcsname FV@CC@\string#1\endcsname\else
      \FV@Error{`\expandafter\@gobble\string#1' is not a short
        verb character}\FV@eha
    \fi
  \else
    \FV@Error{Token `\detokenize{#1}' isn't escaped.}\FV@eha
  \fi
  \FV@UndefineShortVerb#1%
}
\def\FV@UndefineShortVerb#1{%
  \catcode`#1=\csname FV@CC@\string#1\endcsname\relax
  \FV@am@csname\let{FV@CC@\string#1}\relax
  \begingroup
  \lccode`\~=`#1%
  \lowercase{\endgroup\def\reserved@a{\let~}}%
  \begingroup
  \FV@am@csname{\endgroup\reserved@a}{FV@AC@\string#1}%
  \FV@am@stackdeplete{\do#1}\dospecials
  \FV@am@stackdeplete{\@makeother#1}\@sanitize
  \FV@am@stackdeplete{\DefineShortVerb#1}\FV@am@defineshortverb
  \FV@am@stackdeplete{\UndefineShortVerb#1}\FV@am@undefineshortverb
}
% I introduced the macro \FV@am@stackdeplete because I saw a possible
% failure edge case with the scheme in fancyvrb about stack depletion
% (not popping).
\def\FV@am@stackdeplete#1#2{%
  \begingroup
  \def\remove@a##1\FV@nil{##1}%
  \def\remove@b##1\FV@nil{}%
  \def\remove@c##1#1##2\FV@nil##3\FV@nil##4\FV@nnil{%
    \expandafter\endgroup##3\def#2{##1##2}\FV@nil
  }%
  \expandafter\remove@c#2\FV@nil\remove@a\FV@nil
    #1\FV@nil\remove@b\FV@nil\FV@nnil
}
\makeatother

\DefineShortVerb{\|}

\begin{document}
And we can |_change_$%#| the character before the picture.

\SaveVerb{Verb}|#%_OK_$_and_£_!#_verbatim_material|

\begin{tikzpicture}
\node at (1,0) [rectangle,draw,red,fill=yellow!25]{\UseVerb{Verb}};
\node at (1,1.4) [circle,draw,fill=blue!15] {|A| $\|A\| \leq \epsilon$};
\end{tikzpicture}

And we can still |$%#_change_| the character after the picture.
\end{document}

相关内容