# How to ignore ending punctuation in an equation environment?

I use the following to export all equations in a long document as a separate pdf (and subsequently, png files):

\documentclass{article}
\newif\ifexport
%\exporttrue
\ifexport
\RequirePackage{mathtools}
\mathtoolsset{showonlyrefs}
\RequirePackage[active,tightpage]{preview}
\PreviewEnvironment{equation}
\PreviewEnvironment{equation*}
\PreviewEnvironment{align}
\else\fi
\begin{document}
My equations may be part of a sentence, such as
$$x = y.$$

But when exporting them (\verb|\exporttrue|), I would like to discard the punctuation: the previous equation should look exactly like this one:
$$x = y$$

Is that possible?
\end{document}

My equations may be part of a sentence, but when exporting them \exporttrue, I would like to discard the punctuation. Is that possible?

## Switch

There is already a switch, \ifexport, which can be used:

x = y\ifexport\else.\fi

Or a macro can be defined, depending on switch \ifexport:

\documentclass{article}
\newif\ifexport
%\exporttrue
\ifexport
\RequirePackage{mathtools}
\mathtoolsset{showonlyrefs}
\RequirePackage[active,tightpage]{preview}
\PreviewEnvironment{equation}
\PreviewEnvironment{equation*}
\PreviewEnvironment{align}
\newcommand*{\textpunct}[1]{}
\else
\newcommand*{\textpunct}[1]{#1}
\fi
\begin{document}
My equations may be part of a sentence, such as
$$x = y\textpunct{.}$$
\end{document}

## Active math punctuation characters

There is no easy way to detect the punctuation at the end of an equation. The following example, therefore, makes the assumption, that the punctuation character only appears before \end (, \end{gather}, \end{align}, ...). Then the example makes the period and comma active in math mode to look ahead for \end:

\documentclass{article}
\newif\ifexport
\exporttrue
\ifexport
\RequirePackage{mathtools}
\mathtoolsset{showonlyrefs}
\RequirePackage[active,tightpage]{preview}
\PreviewEnvironment{equation}
\PreviewEnvironment{equation*}
\PreviewEnvironment{align}
\newcommand*{\mathperiod}{}
\mathchardef\mathperiod=\mathcode\.
\newcommand*{\mathcomma}{}
\mathchardef\mathcomma=\mathcode\,
\begingroup
\makeatletter
\lccode\~=\.
\lowercase{\gdef~{\@ifnextchar\end{}\mathperiod}}
\lccode\~=\,
\lowercase{\gdef~{\@ifnextchar\end{}\mathcomma}}
\endgroup
\AtBeginDocument{%
\mathcode\.="8000\relax
\mathcode\,="8000\relax
}%
\fi
\begin{document}
My equations may be part of a sentence, such as
$$x.z = y.$$
\end{document}
• Good point. However, my question involves a long document that I would prefer not to change (except in the preamble, of course). I know I could do that with regular expressions, but it would certainly be more elegant by defining something that gobbles the punctuation.
– bers
Apr 4 '16 at 22:00
• @bers x=4!: An automatic solution cannot know, whether the exclamation mark belongs to the text or it is the math operator for factorial. Apr 4 '16 at 22:07
• You are right. Let's assume I don't yell (my math) at my readers, so punctuation is either a trailing . or ,.
– bers
Apr 4 '16 at 22:08
• Your updated answer doesn't appear to remove punctuation marks from the ends of lines in an align environment except for the very last line (which is followed by \end{align}).
– Mico
Apr 5 '16 at 6:39
• @Mico Yes, therefore I have written, that the punctuation is assumed to be before \end only. Of course, the contexts can be enlarged for the detection of punctuation chars, like a following \ or &. On the other hand the probability for ambiguities will increase. And at the end the development for the "clever" macros will cost much more time than a "stupid" manual fix of the markup. Apr 5 '16 at 8:10

Here's a LuaLaTeX-based solution. It adds the instruction \directlua{do_something=true} to the \ifexport clause and provides a Lua function called remove_trailing_punct which -- if \ifexport is "true" -- removes punctuation marks that occur at the ends of lines inside equation[*] and align[*] environments. Commas, periods ("full stops"), and semicolons, are the punctuation marks that are removed from the ends of lines. The code works both on lines terminated with \\ (i.e., those inside an align or align* environment) and on lines that end with just a punctuation mark.

In case you're not familiar with LuaLaTeX: As it is (for the most part) a superset of pdfLaTeX, you won't need to change much at all in your code to get your document to compile under LuaLaTeX. Just (a) copy the code block from \usepackage{luacode} to \end{luacode} to the top of your preamble, (b) load the fontspec package (as is done in the example below), and (c) avoid loading the fontenc and inputenc packages. That's all -- really.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{mathtools,unicode-math}

\usepackage{luacode}
\begin{luacode}
do_something    = false
in_display_math = false
function remove_trailing_punct ( buff )
if do_something == true then
if string.find ( buff , "\\begin{equation%*?}" ) or
string.find ( buff , "\\begin{align%*?}" ) then
in_display_math = true
elseif string.find ( buff , "\\end{equation%*?}" ) or
string.find ( buff , "\\end{align%*?}" ) then
in_display_math = false
elseif in_display_math == true then
buff = string.gsub ( buff , "^(.+)%s-[%.%,%;]%s-(\\\\)%s-$" , "%1".."%2" ) buff = string.gsub ( buff , "^(.+)%s-[%.%,%;]%s-$" , "%1" )
end
end
return buff
end
remove_trailing_punct, "remove_trailing_punct" )
\end{luacode}

\newif\ifexport
\exporttrue
\ifexport
\mathtoolsset{showonlyrefs}
\RequirePackage[active,tightpage]{preview}
\PreviewEnvironment{equation}
\PreviewEnvironment{equation*}
\PreviewEnvironment{align}
\PreviewEnvironment{align*}
\directlua{ do_something = true }
\fi

\begin{document}
\begin{align*}
a/b &= c , \\
u*v &= w .
\end{align*}

\begin{equation*}
x+y = z  .
\end{equation*}
\end{document}
• @bers - I've updated my answer so that it correctly removes trailing punctuation marks on lines that end with "\\".
– Mico
Apr 5 '16 at 14:07