6

I've noticed that the \verb command does not react well to addtional active characters. E.g.

\documentclass{minimal}
\begin{document}

\catcode`\/=\active

    \verb§/§

\end{document}

will fail.

Is there a way to let \verb know that another character has been made active? Or, if not, what's a good work-around or solution?


Thanks for the input on using § as the \verb delimiter. Noted.

The reason I use § is because of the similarity to $; I like to use a shorthand command \def§{\verb§} for inline verbatim sequences (especially code).

FK82
  • 683
  • 2
    The problem with § is that it means different things at different times, by default it's an unsupported punctuation character, if you use inputenc it will (like all 8bit characters) be made active, if you use latin1 encoding it is a single token, if you use utf8 encoding with inputenc it is two tokens, if you use utf8 with xetex it is one token, so for example your \def§ would fail if you defined it before loading inputenc – David Carlisle Sep 02 '14 at 13:06
  • @DavidCarlisle Ok. Can you suggest any character. I've seen the pipe | and + being used often. Are these the recommended delimiters? – FK82 Sep 02 '14 at 14:24
  • probably: any ascii character is more or less safe – David Carlisle Sep 02 '14 at 15:24

3 Answers3

8

I'm not sure using § here is really supported, but ignoring that, the list of characters made safe is \dospecials:

\documentclass{minimal}
\expandafter\def\expandafter\dospecials\expandafter{\dospecials\do\/}
\begin{document}

\catcode`\/=\active

    \verb§/§

\end{document}
David Carlisle
  • 757,742
5

There are two lists in LaTeX for stuff like verbatim:

  • \verb and environment verbatim are using a general list \dospecials. Each element is a pair, \do followed by a one-character command.

  • \index and \glossary are using \@sanitize. Each element is a pair, \@makeother followed by the one-character command.

The following adds support for a new active / to both commands:

\makeatletter
\g@addto@macro\dospecials{\do\/}
\g@addto@macro\@sanitize{\@makeother\/}
\makeatother

BTW: \verb§/§

§ is a little dangerous beast regarding the input encoding. The character needs two bytes in encoding UTF-8. Then it will only work in LuaTeX/XeTeX, whereas TeX/pdfTeX will see two bytes and the first byte is used as delimiter. The problem is not visible with output font encoding OT1 and without inputenc, because the 8-bit bytes are not set because of the 7-bit fonts. With \tracinglostchars=2 this can be seen as warnings:

Missing character: There is no � in font cmtt10!
Missing character: There is no � in font cmr10!

The first is the second byte of the first § inside the verbatim text, the second is the second byte of the closing § outside, because the first byte was used as delimiter.

If \usepackage[utf8]{inputenc} is in use, then the disrupted second byte will cause errors:

! Package inputenc Error: Keyboard character used is undefined
(inputenc)                in inputencoding `utf8'.
Heiko Oberdiek
  • 271,626
4

If you use fancyvrb you have a nice interface:

\documentclass{article}
\usepackage{fancyvrb}

\catcode`\/=\active
\def/{SLASH} % just for the example

\fvset{defineactive=\edef/{\string/}}

\begin{document}
ab/cd \Verb|ab/cd|
\end{document}

enter image description here

You might define the active slash in \Verb (or the verbatim environments) with other meanings, of course.

egreg
  • 1,121,712