[Tex/LaTex] Remove everything that is in math mode

commentsmath-mode

The package comment.sty allows one to typeset

\begin{comment}
stuff that will be suppressed from output file
\end{comment}

or

\begin{myCustomEnv}
stuff that will be suppressed from output file
\end{myCustomEnv}

One can even suppress the contents of already defined environments, for instance {proof}.

In this spirit, I would like to suppress everything that is in math mode, whatever the way it starts: $, \(, $$, \begin{displaymath}, etc.
The output should be as if the contents of everything that is in math mode was not in the source file.

Can this be done?

Best Answer

There are a few very different ways to enter and to exit math mode:

  1. TeX math shift character, catcode 3, typically $
  2. LaTeX macros \(<math>\) and \[<math>\]
  3. LaTeX environments math and displaymath
  4. amsmath environments
  5. Not directly in the source: macros that were defined with the original catcode of $ cannot be hidden with the described methods.

1. TeX math shift

An easy redefintion of $ (catcode 13, active) solves this.

\catcode`$=\active
\makeatletter
\def${%                        % TeX math shift (not anymore)
    \@ifnextchar${\killB}{\killA}%
}
\makeatother
\def\killA#1${\ignorespaces}   % TeX inline math
\def\killB$#1$${\ignorespaces} % TeX display math

The macro \killA kills inline math ($<math>$) with removing any spaces that comes after the last $. Without \ignorespaces, something like Text $ f(x) $ text would be typset as Text␣␣text. (This would be disallowed by “The output should be as if the contents of everything that is in math mode was not in the source file.”)

2. LaTeX macros \(<math>\) and \[<math>\]

A TeX re-definition of those macros suffice:

\def\(#1\){\ignorespaces} % LaTeX inline math
\def\[#1\]{\ignorespaces} % LaTeX display math

Without the white-space problem (see above), the following would work too:

\let\[\iffalse
\let\]\fi
\let\(\iffalse
\let\)\fi

3. LaTeX environments math and displaymath

The LaTeX environments math and displaymath use internally $ and \[ and \], but the solutions above do not work here (they do more harm instead).

But with the help of the environ package we can simply redefine those environments:

\def\killMe#1{%                % for math environments
    \expandafter\let\csname #1\endcsname\relax
    \expandafter\let\csname end#1\endcsname\relax
    \NewEnviron{#1}{}%
    }

\killMe{displaymath}
\killMe{math}

The first two lines of the \killMe macro let \<environment> and \end<environment> to \relax so that \NewEnviron thinks they aren’t already defined. \NewEnviron automatically inserts a \ignorespaces in the \end part of the environment.

4. amsmath environments

The amsmath package provides a few environments that are “silenced” in the same way with the \killMe macro. For example:

\killMe{align}
\killMe{align*}

Code

\documentclass{article}
\usepackage{amsmath}
\usepackage{environ}

\def\killMe#1{%                      for math environments
    \expandafter\let\csname #1\endcsname\relax
    \expandafter\let\csname end#1\endcsname\relax
    \NewEnviron{#1}{}%
}

    \def\(#1\){\ignorespaces}%        LaTeX inline math
    \def\[#1\]{\ignorespaces}%        LaTeX display math

%   \let\[\iffalse
%   \let\]\fi
%   \let\(\iffalse
%   \let\)\fi

    \catcode`$=\active
    \makeatletter
    \def${%                            TeX math shift
        \@ifnextchar${\killB}{\killA}%
    }%
    \makeatother
    \def\killA#1${\ignorespaces}%     TeX inline math
    \def\killB$#1$${\ignorespaces}%   TeX display math

    \killMe{align}%
    \killMe{align*}%
    \killMe{displaymath}%              displaymath relies on \[ and \] and cannot live
    %                                  without the right definition of \[ and \]
    \killMe{math}%      %              math relies on $ as math shift character and would work
    %                                  although $ is active and redefined


\begin{document}
H%
$ i^n TeX $
e%
\( i^n LaTeX \)
l%
$$ o^ut TeX $$
l%
\[ o^ut LaTeX \]
o
\begin{align}
 f(x) & = x^2
\end{align}
W%
\begin{align*}
 f(x) & = x^2
\end{align*}
o%
\begin{displaymath}
 f(x) = x^2 (displaymath)
\end{displaymath}
r%
\begin{math}
    2^3 (math)
\end{math}
ld!

Text $ f(x) $ text
\end{document}

Output

enter image description here

Related Question