For some reason, I need a package providing an active apostrophe. For the sake of simplicity, let's say that every apostrophe shall be replaced by ABC
in text mode. In math mode, the default behaviour shall remain.
The apostrophe character is defined as “math-active” in latex.ltx
, lines 4548 to 4563:
\def\active@math@prime{^\bgroup\prim@s}
{\catcode`\'=\active \global\let'\active@math@prime}
\def\prim@s{%
\prime\futurelet\@let@token\pr@m@s}
\def\pr@m@s{%
\ifx'\@let@token
\expandafter\pr@@@s
\else
\ifx^\@let@token
\expandafter\expandafter\expandafter\pr@@@t
\else
\egroup
\fi
\fi}
\def\pr@@@s#1{\prim@s}
\def\pr@@@t#1#2{#2\egroup}
This code makes sure that a sequence of apostrophe characters is converted into a sequence of prime symbols in math mode.
To fulfil my needs, I have devised the following code:
\begin{filecontents}{mypackage.sty}
\RequirePackage{xparse}
\ProvidesExplPackage {mypackage} {2019-09-20} {1.0} {My package}
\cs_new:Npn \mypackage_apostrophe:
{
\mode_if_math:TF
{
\active@math@prime % latex.ltx, line 4548
}
{
ABC % dummy text for testing
}
}
\char_set_active_eq:NN ' \mypackage_apostrophe:
\NewDocumentCommand \Activate { }
{
\char_set_catcode_active:N '
}
\NewDocumentCommand \Deactivate { }
{
\char_set_catcode_other:N '
}
\end{filecontents}
\documentclass{article}
\usepackage{mypackage}
\begin{document}
a'b $f'(x)$ $f''(x)$
\Activate
a'b $f'(x)$ $f''(x)$
\Deactivate
a'b $f'(x)$ $f''(x)$
\end{document}
This code produces an error message if the apostrophe character is active and a second derivation $f''(x)$
is needed:
! Double superscript. \active@math@prime ->^
\bgroup \prim@s
l.30 a'b $f'(x)$ $f''
(x)$
How can I avoid this error message (keeping '
as active)?