# Remove all math for spelling and grammar checking

I'm compiling a LaTeX file to pdf and then I use a pdf to text converter to convert the file to plain text. I then use LanguageTool to do grammar checking on that plain text file. The LaTeX source has some customizations to improve the quality of the plain text file (e.g., I removed page headers, page number, multicolumn environments, and I'm using a huge paper size).

Unfortunately, LanguageTool finds a lot of the grammar errors in the math parts of the file, which clearly is not very useful. So I was wondering: is it possible to just remove all math from the pdf output? Clearly, I could just delete all math from the LaTeX source, but that is not an acceptable way. The math should just not be rendered in the pdf.

-
I was just working on a similar solution to @barbarabeeton's below. The problem (if you want to consider grammar checking, which is what makes it a very interesting question, to me) is that the math technically forms part of the sentence structure. How can math (inline or display) be replaced with some generic noun (or potentially other parts of speech, which gets even trickier) for the purposes of grammar checking, but preserve any punctuation found at the end of a displayed math environment? This is quite a challenge in my mind, but maybe it can be done. –  Paul Gessler Jul 12 at 18:19
@PaulGessler: Yes, I was thinking about this as well, However, I cannot think of a generic string that would make the following two sentences grammatically correct: "The variable $x$ is nonnegative." "It follows that $x=1$." Maybe there is a clever use of a string with a double meaning? –  Yori Jul 12 at 19:26
(Using Barbara's solution below I replaced all math by the word "spam" and amusingly got the output "Let spam, and define spam, spam, spam, and spam.") –  Yori Jul 12 at 19:35

this really should be a comment, but it's a little too complicated.

if your math is either all in-line or displayed using the $...$ notation, suppressing it is quite easy:

\documentclass{article}
\def$#1${}
\def$$#1$${}
\catcode\$=13 \def$#1${} \begin{document} some text$xyz\$ with embedded $$abc$$ math.
some display math as well:
$def$
more text
\end{document}


things get trickier when you use \begin{xxx} ... \end{xxx} environments, and that doesn't even consider starred environments. i haven't licked that problem yet.

many of the amsmath environments ingest the entire content between the \begin and \end markers, so could be redefined to just ignore that instead of measuring and setting.

this sounds like an interesting project for a package. anyone who wants to can steal the ideas laid out above.

-
Note to editor(s): Respecting poster's style. –  Paul Gessler Jul 12 at 19:30
Thank you! You are right about the \begin{xxx} ... \end{xxx} environments. There is actually a package (comment) that does something similar. I use it to suppress tables and figures. It's not perfect though; I sometimes do get compilation errors that I do not understand. –  Yori Jul 12 at 19:30
Perhaps also consider environ with \RenewEnviron{<env>}{}. –  Werner Jul 12 at 20:02
@Werner -- somehow i didn't think there was one of those, but there is. but wouldn't it be \renewenvironment{<env>}{}{}? (i couldn't find one with the name in CamelCase.) (i'll come back to this later; right now, i'm on my way to a concert.) –  barbara beeton Jul 12 at 20:50
@DavidMulder We don't follow such things here for the sake of SE network. More in this post, since this pops up every now and then meta.tex.stackexchange.com/questions/3802/… Also there is no duty involved on TeX-SX, it is all community driven voluntary work. –  percusse Jul 13 at 9:10

Here's a LuaLaTeX-based solution for suppressing math display-style environments, i.e., not letting them produce any output.

It employs the comment package, and it sets up (for now) lua functions to replace all instances of \begin{displaymath}, , and \begin{align} with \begin{comment} as well as to replace all instances of \end{displaymath}, , and \end{align} with \end{comment}. Starred versions of these environments are also handled, i.e., equation* and align* environments also get replaced with comment environments.

It should be straightforward to augment the code to process additional display math environments such as gather and multline.

By adding the functions to LuaTeX's "process_input_buffer" callback, the replacements are done during a very early stage of processing of the tex file, viz., before TeX gets to do any processing.

The code is admittedly a bit clumsy for now, because it requires two separate functions for each math environment that should be excluded. I suppose this could be remedied by making use of Lua's lpeg library, which provides some pretty fancy pattern matching methods.

Note that the proposed approach has the following two limitations: First, the various \end{...} statements -- \end{displaymath}, \end{align*}, etc -- must be the only items on the line and must start at the beginning of the line; this is a requirement of the comment package. Second, if your document already makes use of the comment package and its eponymous environment, you will run into trouble if the code in the pre-existing comment portions contains math environments.

The MWE below consists of two files: The main "driver" file and a file that should be called "mathcomment.lua"; the latter contains the lua code and is loaded by the driver file with a \directlua{ require(...) } directive. The following two screenshots show the output produced by the driver file if the instruction \directlua{ require( "mathcomment.lua" ) } is (a) included or (b) commented out.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath,comment,luatexbase}
\directlua{require("mathcomment.lua")} % if commented out, display math stuff is not suppressed
\setlength\textwidth{2in} %% just for this example

\begin{document}
\noindent
aaa
\begin{displaymath}
a^2+b^2=c^2a
\end{displaymath}
bbb
$$\label{eq:einstein} E=mc^2$$
ccc
\begin{align}
1+1&=2\\
2+2&=4
\end{align}
ddd
\begin{align*}
0+0&=0\\
a+a&=2a
\end{align*}
ee
\end{document}


-- mathcomment.lua

--displaymath
local function comment_begin_displaymath ( line )
return string.gsub ( line,
"\\begin{displaymath}", "\\begin{comment}" )
end
local function comment_end_displaymath ( line )
return string.gsub ( line,
"\\end{displaymath}",  "\\end{comment}" )
end

--equation, equation*
local function comment_begin_equation ( line )
return string.gsub ( line,
"\\begin{equation%*?}", "\\begin{comment}" )
end
local function comment_end_equation ( line )
return string.gsub ( line,
"\\end{equation%*?}",  "\\end{comment}" )
end

--align, align*
local function comment_begin_align ( line )
return string.gsub ( line,
"\\begin{align%*?}", "\\begin{comment}" )
end
local function comment_end_align ( line )
return string.gsub ( line,
"\\end{align%*?}",  "\\end{comment}" )
end

-- register the functions as callbacks

comment_begin_displaymath, "comment_begin_displaymath" )
comment_end_displaymath, "comment_end_displaymath" )

comment_begin_equation, "comment_begin_equation" )
comment_end_equation, "comment_end_equation" )

comment_begin_align, "comment_begin_align" )
comment_end_align, "comment_end_align" )


Addition by yori: (I think the code below is a useful addition, but I did not want to create a new answer for this, because I just generalized the code above.) The following code is a bit shorter and easier to extend:

-- mathcomment.lua

local function comment_environment_function(env_regex)
return function(line)
line = string.gsub(line, "\\begin{" .. env_regex .. "}", "\\begin{comment}")
line = string.gsub(line, "\\end{" .. env_regex .. "}", "\\end{comment}")
return line
end
end

-- register the functions as callbacks

local environments = { "displaymath", "equation%*?", "align%*?" }

for _,env in pairs(environments) do
comment_environment_function(env),
"comment_" .. env)
end

-
Nice! I wonder if the first limitation can be dealt with by automatically adding a newline character after the \begin{...} commands. I have no experience with LuaTeX and tried just adding \n to the replacement string, but the line splitting has finished when the hook is run, so that the \n is just inserted in the current line. I don't know how to fix that. –  Yori Jul 13 at 10:16
@Yori - Many thanks for posting the alternative Lua code -- much easier to parse and to extend! Regarding your follow-up question: It looks like what's needed to solve that issue is a way to insert an extra, all-blank line either before \begin{...} or after \end{...}. Of course, having the various \end{...}` statements on lines by themselves is probably good coding practice anyway and should thus be implemented anyway, right? –  Mico Jul 13 at 11:11
I agree that it is good coding practice. However, I sometimes indent an equation with a few spaces. That's a case where this code breaks. Once you're aware of the problem, it's not a big deal to fix that in the original LaTeX code. I just ran a grammar check with your code and barbara's code, and the output is much less noisy. Great work! –  Yori Jul 13 at 11:56

Have you tried using TeXstudio together with LanguageTool? This combination should work fine for you, since TeXstudio has the option to communicate with the LanguageTool build-in webserver. At least under Windows this way works fine for me.

Another option would be to simply copy & paste the text from the PDF into the LanguageTool GUI. By either ways the math gets ignored without changing the LaTeX code itself.

-
Thanks for the suggestion. I don't use TeXstudio, but may give this a shot. The other option, copy and pasting, is a good option for shorter texts, but I'm working on a 700-page book. The advantages of the copy & paste method is that you'd be able to paste into Microsoft Word, which may have the best grammar checker available (not sure if this is true, I don't have MS Word). –  Yori Jul 16 at 8:20