8

I've mimicked Donald Knuth by setting | as a verbatim delimiter. To give it its normal meaning in math mode, I've set

\everymath{\catcode`\|=12}
\everydisplay{\catcode`\|=12}

However I'd like to append this to \everymath instead of erasing its previous content (if any). I've tried

\everymath{a}
\everymath{\the\everymath b}

but it doesn't work. Any idea?

PS: this question is for plain TeX.

lvaneesbeeck
  • 3,095
  • 2
    ConTeXt provides macros \appendtoks and \prependtoks for such tasks. You can use \appendtoks b \to \everymath to achieve what you want. There must be something equivalent in LaTeX; if not, you can always copy the ConTeXt definitions. – Aditya Dec 25 '13 at 03:52
  • @Aditya it wasn't explicit in my question but I'm looking for a solution for plain TeX. Do you know where I can find the definition of \appendtoks? – lvaneesbeeck Dec 25 '13 at 05:49
  • 3
    Have you tried something like: \expandafter\everymath\expandafter{\the\everymath b}. Seems to work for me in everything but display mathmode. – A.Ellett Dec 25 '13 at 06:43
  • @A.Ellett Nice! It doesn't work in display math since display math put the \everydisplay token list. \everymath works for non-display math mode. – lvaneesbeeck Dec 25 '13 at 12:03

2 Answers2

5

The method won't work: if you have, say,

\section{Proof that $|x|\ge 0$}

the setting of \catcode`|=12 wouldn't take effect, because | would have already been tokenized.

The safest way is to say

\catcode`|=\active
\protected\def|{\ifmmode\expandafter\vert\else\expandafter\activebar\fi}

\def\activebar{...<whatever you want>...}

If you want to use Plain TeX without e-TeX extensions, the resulting command would be quite fragile, so use with care:

\def|{\relax\ifmmode\expandafter\vert\else\expandafter\activebar\fi}

(you find the reason for \relax in the TeXbook).


Let's try a Plain TeX document:

\input manmac
\everymath\expandafter{\the\everymath\catcode`|=12 }
\everydisplay\expandafter{\the\everydisplay\catcode`|=12 }

\beginsection $|x|$ is never negative.

|the $p$-norm| is
$$
\Vert x\Vert=\root p\of{|x_1|^p+\cdots+|x_n|^p}
$$
\bye

You get

! Argument of \\ has an extra }.
<inserted text> 
                \par 
<to be read again> 
                   }

because | has already been tokenized when the argument of \beginsection was being absorbed.

With

\input manmac

\let\manmacbar| % after manmac | is active
\protected\def|{%
  \ifmmode
    \expandafter\vert
  \else
    \expandafter\manmacbar
  \fi
}

\beginsection $|x|$ is never negative.

|the $p$-norm| is
$$
\Vert x\Vert=\root p\of{|x_1|^p+\cdots+|x_n|^p}
$$
\bye

you get no error and the output is what's expected:

enter image description here

egreg
  • 1,121,712
  • \protected is not a plain tex command. – barbara beeton Dec 25 '13 at 14:33
  • @barbarabeeton It's an e-TeX primitive; if pdftex is used, it's available. – egreg Dec 25 '13 at 14:34
  • Why do we need \protected there? – Henri Menke Mar 23 '14 at 22:18
  • 1
    @HenriMenke With a simple \def, if | is used as the first item in an \halign based construction, it would be evaluated as if in text, not in math. A \relax before \ifmmode could do for this particular case, but the resulting macro would be fragile (not surviving \edef). – egreg Mar 23 '14 at 22:26
4

A. Ellet gave the answer in the comments (edit: only one \expandafter is needed):

\everymath{a}
\everymath\expandafter{\the\everymath b}

sets \everymath to ab

lvaneesbeeck
  • 3,095