# fancyvrb ShortVerb and tikz incompatibity

I've been working on a document that has a lot of little verbatim bits so I'm using fancyvrb \DeflneShortVerb{\|}. Now I want to add a tikz picture, and the whole thing fails. It there a simple way around this?

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


The error is:

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


The following question: Disable shortvrb in math mode discusses disabling \ShortVerb in math mode, but the solutions there won't work for this case.

-
PGF uses || for the or operator and this produces the error. –  Gonzalo Medina Jun 15 '12 at 21:49
@GonzaloMedina I understand the source of the error, I'm wondering if there's a way around it short of (i) using a different short verb escape or (ii) including the image with \includegraphics (If there's no solution I'll just do the latter, most likely.) –  Alan Munn Jun 15 '12 at 21:52
Your problem arrives because you use \DefineShortVerb{\|} before the call to TikZ. After it's your problem to know what character to use for the verbatim form. –  Alain Matthes Jun 16 '12 at 7:11

Personally, I always did like that but it's not a reason for this is the right method : \AtBeginDocument{\DefineShortVerb{\|}} but it's enough to place \DefineShortVerb{\|} after \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}


I work with pgf 2.1 cvs but I think I always used \AtBeginDocument{\MakeShortVerb{\|}}.

-
Thanks. This is the simpler answer by far. –  Alan Munn Jun 16 '12 at 14:47
@AlanMunn My solution is correct with pgf 2.1 cvs but with pgf 2.1 percusse's answer is fine. Perhaps you need to accept percusse's answer. The cvs version is not current version. –  Alain Matthes Jun 16 '12 at 15:29
Are you sure? I can run your example with pgf 2.1 with no problem. –  Alan Munn Jun 16 '12 at 15:59
My example yes! but \tikz{\node {|A|};} gives an error with pgf 2.1 and not with pgf 2.1 cvs. You can load tikz without an error but a problem appears if you try to use | inside a tikz environment. –  Alain Matthes Jun 16 '12 at 16:15
If you don't need verbatim text inside a node you can use my solution with pgf 2.1. –  Alain Matthes Jun 16 '12 at 16:18

I have put the undefine command at the beginning of each picture and reenabled after the picture. It looks like a solution:

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


Not sure if it's robust though.

EDIT After reading the comments, I realized that the example above is a little bit cheating so I tried an example without \| but standalone | pair and it broke down. To fix that we need to really force the introduction of the "un"definition go all the way up.

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


-
Of course |foo| will not be verbatim inside a tikzpicture, but I don't think this will be a problem. –  egreg Jun 15 '12 at 22:10
Beautiful. Thanks, I hoped there was a solution like this. So many little bits of TikZ to know about... (@egreg No, I have no need for verbatim in the picture). –  Alan Munn Jun 15 '12 at 22:11
With \DefineShortVerb{\|} after \usepackage{tikz}it's not useful to use execute at begin picture etc.or perhaps I don't see a subtlety. –  Alain Matthes Jun 16 '12 at 7:06
If you want to avoid emdash character, you can use \usepackage[T1]{fontenc} –  Alain Matthes Jun 16 '12 at 9:55
@Altermundus Nice! Thank you. –  percusse Jun 16 '12 at 9:58

The problem can be dealt with in a more fundamental way. I include here some of my old patches to fancyvrb package.

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

-
Thanks, Ahmed, that's also useful. –  Alan Munn Jun 16 '12 at 5:22
In this case, you don't need to define something with execute at begin picture`. –  Alain Matthes Jun 16 '12 at 6:25