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.

In this document:

\documentclass{article}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{AVerb}{Verbatim}{commandchars=\\\{\}}
\def\foo#1-#2;{}
\begin{document}
\begin{AVerb}
  \foo x-z;
\end{AVerb}
\end{document}

The use of \foo in AVerb produces an error:

! Argument of \foo has an extra }.
<inserted text>
                \par
l.11   \foo x-z;

The catcode for - in the AVerb environment is 13 (active), but in normal text it is 12 (other).

I tried setting the catcode for - to 12 using fancyvrb's codes option but that didn't help. I believe this is because - can be a ligature and fancyvrb overrides my setting.

Is there a way to make this work?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Yes, the problem is exactly that: your delimiter has category code 12 and TeX can't find it in verbatim text where it's active exactly for the reason you're mentioning. The code

\begingroup\lccode`~=`-
  \lowercase{\endgroup\def\foo #1~#2;}{}

will define \foo with the first argument delimited by an active -.

If you need \foo also in other places, then define it as you did and say also

\begingroup\lccode`~=`-
  \lowercase{\endgroup\def\vfoo #1~#2;}{}

Then, with the help of the mighty \usepackage{etoolbox}, say

\preto{\AVerb}{\let\foo\vfoo}

(after having defined the AVerb environment).

How does the trick work? We set, in a group, the lowercase equivalent of ~ equal to -; the operation \lowercase acts only on characters (not on control sequences), not changing category codes. It does not expand or execute commands, but simply does the conversion and puts back the token list for examination by the input processor. Only after this conversion \endgroup is executed, so the equivalence is forgotten, but ~ has already been transformed into an active hyphen.

share|improve this answer
    
Defining vfoo works as advertised, though it took me awhile to figure out that the active status of ~ also played a role (in making - active). However, \let\foo\vfoo does not work -- I get the same error. But that's ok, I can use \vfoo. –  Justin Bailey Nov 15 '11 at 18:03
1  
@JustinBailey I've added a trick and also some comments. –  egreg Nov 15 '11 at 18:26
    
Cool, thanks for the updated text. I haven't seen etoolbox before. Very nice! –  Justin Bailey Nov 15 '11 at 20:00
add comment

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.