Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
A script in python or perl or awk could preprocess the TeX source and remove all math. You'd have to be a little careful to detect escaped $ signs inside math mode. –  Ethan Bolker Jan 6 '13 at 18:16
3  
@EthanBolker yes, but that would change the input; it sounds like the OP only wants to change the output with the flick of a switch in the preamble –  cmhughes Jan 6 '13 at 18:59
    
@cmhughes The script output would go to nomath.tex for subsequent compilation, leaving the original input document whole. I'll upvote a wizardly answer that uses just TeX in the preamble. –  Ethan Bolker Jan 6 '13 at 21:45
    
How do you want to treat text material that appears in a math mode environment? –  Andrew Swann Jan 7 '13 at 12:23
add comment

2 Answers

I would also suggest to go for some external solution that drops the math material from the file and writes a new one. One reason for it is that LaTeX internally uses math in many places which are not obvious at all (and not necessarily real math) the \LaTeXe logo for example.

Anyway just for the fun of it here is a solution within LaTeX (totally unsupported and not necessarily complete, perhaps one has to disable much more):

\documentclass{article}

\makeatletter
\def\nomath{%
\font\dummyft@=dummy \relax
   \count@\sixt@@n
   \loop
    \ifnum\count@ >\z@
     \advance\count@\m@ne
     \global\textfont\count@\dummyft@
     \global\scriptfont\count@\dummyft@
     \global\scriptscriptfont\count@\dummyft@
    \repeat
   \global\let\mathversion\@gobble
   \frozen@everymath={}
   \frozen@everydisplay={}
  \let\boldmath\relax
  \let\unboldmath\relax
  \let\mv@normal\@empty
  \let\mv@bold\@empty
}
\makeatother

\begin{document}
test1: $x$

test2:
\[ a=b\]

test3: \LaTeX{} and $a=b$ but see:  \LaTeXe

\begin{itemize} \item test4 $a=b$ \end{itemize}

test5:  \parbox[c]{1cm}{now what? $a=b$}

\bigskip

\nomath \ldots{} and again \ldots

test1: $x$

test2:
\[ a=b\]

test3: \LaTeX{} and $a=b$ but see:  \LaTeXe

\begin{itemize} \item test4 $a=b$ \end{itemize}

test5:  \parbox[c]{1cm}{now what? $a=b$}
\end{document}

But for the sample document it seems to do suppress all math successfully (more or less). Note, that I did really nasty stuff like changing \frozen@everymath, which is frozen for a reason, but there you go.

Also note the deficiencies in the resulting output:

enter image description here

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.