Wednesday, March 5, 2008

Verbatim text as command argument

In my dissertation I will refer to the N|uu text corpus. The corpus has reference units which are called e.g. NA071112-01_E.008. I thought all the references to the text corpus are markup-worthy entities so I needed a command like \ru (short for reference unit) to markup those references with it like this: \ru{NA071112-01_E.008}.

As you know, the underscore “_” is a special character used in math mode for subscript. So it is not allowed to occur as a part of some string. I did not want to write \ru{NA071112-01\_E.008} instead. I looked for a way to feed commands verbatim text arguments.

I stumbled upon the fancyvrb package, which looks really promising for this, especially in the documentation on p. 17 where some magic aftersave is used. But the aftersave parameter is heavily underdocumented there. That’s just my luck. I could not find out what aftersave actually does and if it will enable me to write someting like \ru{NA071112-01_E.008}.

I then sought help at comp.text.tex (topic: fancyvrb problems) and Enrico Gregorio was so kind to write me this TeX code:


\def\ruspeciallist{\do\_} % add the special characters you need with "\do\X" (X is the characater)

\def\rucatcodes{\def\do##1{\catcode`##1=12 }\ruspeciallist}
\newbox\rubox
\def\ru{\afterassignment\dorusetup\let\next= }
\def\dorusetup{\setbox\rubox=\hbox\bgroup
\rucatcodes \aftergroup\dorufinish}
\def\dorufinish{\box\rubox}


An extensively commented version is also there.

In the last line between “\dorufinish{” and “\box\rubox}” come all the commands you want to apply the the argument of \ru. BUT:

Enrico: “A limit of this approach is that the string is never read as an
argument, so that it is not available for, say, writing an index
entry: we have it only in typeset form, inside the box.”

Ok, this certainly is a nice piece of code but it does not essentially allow verbatim text to be an argument of a command. It is just a work-around to get that verbatim text typeset somehow (and that's what I have originally asked for in the newsgroup). So, in that sense, verbatim text as argument still is an open issue.

Enrico — after I told him I actually may want to index that stuff — suggested a much simpler and elegant solution for my particular needs.

Anyway, if anybody of you readers finds out how to use the aftersave of fancyvrb package do inform me in the comments to this post. I’d really like to know. I think it has something to do with TeX’s \expandafter.

4 comments:

Chris said...

I think a generic answer to your original problem (how to deal with peculiarities of the verb environment) might be the url package. Particularly \defurl command looks like it might allow you to index and access verb argument. Have not tried it, but might be worth a look.ofili

help writing dissertation said...

I think XeTeX works well with both LaTeX and ConTeXt macro packages. Its LaTeX counterpart is invoked as xelatex

dissertation services said...

so??? I don't get in finall - which one is better to use?? tell me please

Anonymous said...

Thanks for sharing this post. This article helps me a lot. It is useful. Your content is brilliant in my opinion. I think this is engaging and eye-opening material. I hope you will continue writing such a great articles. great-term-paper.com