1

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)?

0

2 Answers 2

1

The lines

\catcode`\'=\active 
\def\svprime{\active@math@prime}
\def'{\ifmmode\expandafter\svprime\else\expandafter\ABC\fi}
\def\ABC{ABC}

almost had it working. But then I finally realized that since \pr@m@s employs a test for the repeated ', I had to make ' active before defining \pr@m@s, or else it would always fail the \ifx test for the repeated '.

Commented out are other parts associated with the \prime definition in latex.ltx.

\documentclass{article}
\makeatletter
\catcode`\'=\active 
%\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}
\def'{\ifmmode\expandafter\active@math@prime\else\expandafter\ABC\fi}
\def\ABC{ABC}
\makeatother
\begin{document}
Text mode: x'x
\begin{equation}
a = v'(t) = x''(t)
\end{equation}
\end{document}

enter image description here

3
  • At which point of my above package code shall I insert \def\pr@m@s{...}? I cannot get it work.
    – wehro
    Commented Sep 20, 2019 at 21:29
  • @wehro I do not speak the syntax of expl3, so I can't provide a fix, but I would say, as part of both \Activate and \Deactivate, I would, after, redefining the catcode, issue the definition for \pr@m@s. Commented Sep 20, 2019 at 23:29
  • That sounds logical, but unfortunately, it does not work. The error message remains.
    – wehro
    Commented Sep 21, 2019 at 8:40
0

The macro \pr@m@s still looks for a category code 12 apostrophe. Patching it is easy.

\documentclass{article}
\usepackage{regexpatch}

\makeatletter
\regexpatchcmd{\pr@m@s}{'}{\cA'}{}{} % \pr@m@s should look for an active '
% now define the active '
\catcode`'=\active
\protected\def'{\ifmmode\expandafter\active@math@prime\else ABC\fi}
\makeatother

\begin{document}

' in text mode

$f'+f''$

\end{document}

enter image description here

In a real world it would probably be

\protected\def'{%
  \ifmmode
    \expandafter\active@math@prime
  \else
    \expandafter\wehro@active@text@prime
  \fi
}

where \wehro@active@text@prime is the macro you want to use in text mode.

1
  • At which point of my above package code shall I insert \regexpatchcmd...? I cannot get it work.
    – wehro
    Commented Sep 20, 2019 at 21:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .