There are a few very different ways to enter and to exit math mode:
- TeX math shift character, catcode 3, typically
$
- LaTeX macros
\(<math>\) and \[<math>\]
- LaTeX environments
math and displaymath
amsmath environments
- 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

pythonorperlorawkcould preprocess theTeXsource and remove all math. You'd have to be a little careful to detect escaped$signs inside math mode. – Ethan Bolker Jan 06 '13 at 18:16nomath.texfor subsequent compilation, leaving the original input document whole. I'll upvote a wizardly answer that uses justTeXin the preamble. – Ethan Bolker Jan 06 '13 at 21:45