Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 ?

share|improve this question

2 Answers 2

\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}
share|improve this answer
    
unfortunately the first form interacts in funky ways with other parts of the document :( (not footnotes, actually) –  NoWhereMan Dec 16 '11 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 '11 at 10:22
    
@NoWhereMan: You can use \DefineShortVerb and \UndefinedShortVerb wherever you want. –  Marco Daniel Dec 16 '11 at 10:38

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.