4

I have enabled shortvrb so that I can enclose text in pipes to write small pieces of code. Unfortunately this conflicts with math mode when I write $|A|$ to indicate the cardinality of a set. How can I disable shortvrb in math mode ?

2 Answers 2

7
\documentclass{article}
\usepackage{fancyvrb}
\DefineShortVerb{\|}
\everymath{\UndefineShortVerb{\|}\vert}

\begin{document}

$|A|$     

\end{document}

However, it has a problem with footnotes. The reason why the following make more sense:

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

\begin{document}

$\vert A\vert$     

\end{document}
3
  • unfortunately the first form interacts in funky ways with other parts of the document :( (not footnotes, actually)
    – NoWhereMan
    Dec 16, 2011 at 10:19
  • However the \DefineShortVerb and \UndefineShortVerb couple is still very useful, I can enable the macro only in those sections where I really need it and keep it disabled otherwise.
    – NoWhereMan
    Dec 16, 2011 at 10:22
  • @NoWhereMan: You can use \DefineShortVerb and \UndefinedShortVerb wherever you want. Dec 16, 2011 at 10:38
2

The following seems to work:

\usepackage{fancyvrb}
\DefineShortVerb{\|}

\makeatletter
\edef|{\unexpanded{%
  \ifmmode
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi{\vert}}{\unexpanded\expandafter{|}}}
\makeatother

but of course you won't be able to use \UndefineShortVerb and \DefineShortVerb again.

It's best to resort to \vert or, even better, to \lvert and \rvert| provided byamsmath`, when a bar in math mode is needed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .