5

I am trying to define a verbatim-like command for regular expressions. I say verbatim-like because I’d like the regex operators to be typeset specially. The idea is that you can type a regular expression like (a|b)+ but it would be formatted neatly such as with the + superscripted. For simplicity, I started from fancyvrb and redefined the catcodes of the operators.

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{bera}
%\usepackage{cmbright}

\def\lparen{\textrm{(}}
\def\rparen{\textrm{)}}
\def\plus{\ensuremath{^+}}
\def\pipe{\textrm{\thinspace\textbar\thinspace}}

\CustomVerbatimCommand{\regex}{Verb}{showspaces,codes={%
\catcode`+=\active\lccode`~=`+\lowercase{\let~\plus}%
\catcode`(=\active\lccode`~=`(\lowercase{\let~\lparen}%
\catcode`)=\active\lccode`~=`)\lowercase{\let~\rparen}%
\catcode`|=\active\lccode`~=`|\lowercase{\let~\pipe}}}

\begin{document}
Whitespace is defined by \regex!( |\t|\n|\f|\v|\r)+!.
\end{document}

The problem I’m having is that it works fine with some font packages (bera, txfonts, pxfonts, lmodern) but not others (cmbright). The error is

(/opt/local/share/texmf-texlive/tex/latex/cmbright/omlcmbrm.fd
! Argument of @providesfile has an extra }.
<inserted text> 
                par 
l.24            [2005/04/13 v8.1 (WaS)]

It appears as though during the verbatim a .fd file is trying to load and since I have redefined the meanings of ( and ) it isn’t working. I know it’s dangerous to make common symbols like ( and ) active, but as long as it was limited to the verbatim I thought it would be fine. In the case of cmbright, just using math mode before the regex causes the .fd file to load first and everything is fine.

Any way to get around this? Or is this just not going to work? I could just use the commandchars feature of fancyvrb to escape to LaTeX when I want to typeset an operator specially, but I was hoping for this type of approach.

2

Force the reading of the math font at begin document:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{bera}
\usepackage{cmbright}

\newcommand\lparen{\textrm{(}}
\newcommand\rparen{\textrm{)}}
\newcommand\plus{\ensuremath{^+}}
\newcommand\pipe{\textrm{\thinspace\textbar\thinspace}}

\CustomVerbatimCommand{\regex}{Verb}{showspaces,codes={%
  \catcode`+=\active\lccode`~=`+\lowercase{\let~\plus}%
  \catcode`(=\active\lccode`~=`(\lowercase{\let~\lparen}%
  \catcode`)=\active\lccode`~=`)\lowercase{\let~\rparen}%
  \catcode`|=\active\lccode`~=`|\lowercase{\let~\pipe}}%
}

\makeatletter
\AtBeginDocument{\check@mathfonts}
\makeatother

\begin{document}
Whitespace is defined by \regex!( |\t|\n|\f|\v|\r)+!.
\end{document}

enter image description here

  • Knew there must be something like that. Thanks. However, that fixes the problem for cmbright, but not for iwona (and probably others is my guess). – D. Atkinson Dec 27 '15 at 0:05
  • @D.Atkinson Are you doing \usepackage[math]{iwona}? If I call it like this, the workaround is good. – egreg Dec 27 '15 at 0:16
  • I wasn't, but I am now, and it works fine. Thanks again! My approach still seems somewhat dangerous to me if a font definition file could be loaded while the catcodes are changed but as of now it works for all the fonts I would use. – D. Atkinson Dec 27 '15 at 0:22
  • @D.Atkinson Changing category codes is always a bit dangerous; in this case it's the activation of ( that confuses LaTeX (the optional argument to \ProvidesFile in omsiwona.fd contains (). – egreg Dec 27 '15 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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