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}
, \begin{equation}
, and \begin{align}
with \begin{comment}
as well as to replace all instances of \end{displaymath}
, \end{equation}
, 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
\begin{equation}\label{eq:einstein}
E=mc^2
\end{equation}
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
luatexbase.add_to_callback( "process_input_buffer",
comment_begin_displaymath, "comment_begin_displaymath" )
luatexbase.add_to_callback( "process_input_buffer",
comment_end_displaymath, "comment_end_displaymath" )
luatexbase.add_to_callback( "process_input_buffer",
comment_begin_equation, "comment_begin_equation" )
luatexbase.add_to_callback( "process_input_buffer",
comment_end_equation, "comment_end_equation" )
luatexbase.add_to_callback( "process_input_buffer",
comment_begin_align, "comment_begin_align" )
luatexbase.add_to_callback( "process_input_buffer",
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
luatexbase.add_to_callback( "process_input_buffer",
comment_environment_function(env),
"comment_" .. env)
end