# Spotting strings that are typos in “text mode” but that are right in “math mode”

My copy editor work is largely dedicate do spot typos. I have a kind of typos formed by strings like, e.g., ".", ",", ":"... (a space before a punctuation sign) that we consider typos in text-mode (e.g. "as follow :") but aren't a problem inside in-line math or math environments (e.g. "$\mu .$").

Usually we use custom emacs' query-replace regexp to find and correct these typos but, as emacs doesn't distinguish math mode from text mode (I know, I can do some tricks to do that but it is not a robust solution), I typically have a lot of false positives (and this is very boring).

I was wondering if there is a native LaTeX solution to do that. I mean not the query-replace but to highlight some matched strings.

I found, e.g., the xesearch package but it doesn't handle punctuation search.

Here is a MWE:

\documentclass[11pt]{article}
\pagestyle{empty}
\begin{document}

Punctuation , typos .

No problem with this string
$$(\mu + \nu) ,$$
\end{document}


I also found l3regex but I think it performs regexp works on the LaTeX code and not on the pre-formatted code as xesearch does.

Any hints, suggestions or helpful ideas are welcome.

Edit. As I answered in comments: "I'm asking for a technique to distinguish strings that are in math mode from strings that are in text mode and highlight only the second ones".

Edit 2. I moved and posted a related question here: XeTeXinterchartoks: Why the “)” in the layout of “\eqref” and (\ref{…}) are interpreted differently?

• $\mu ,$ is wrong anyway. In my experience, errors like these are so various that finding a catch-all regex is hopeless. And l3regex (now in the L3 kernel) is not related. – egreg Jul 27 '17 at 12:14
• @egreg Obviously I'm not asking for a catch-all regex! ;-) I'm asking for a technique to distinguish strings that are in math mode from strings that are in text mode and highlight only the second ones. – Gabriele Nicolardi Jul 27 '17 at 13:12
• Just so we understand, you are asking for an editor function, rather than a LaTeX function. The answer if it exists will, obviously, be editor specific. – Steven B. Segletes Jul 27 '17 at 13:23
• @StevenB.Segletes I'm asking for both things. I use emacs as editor and I can use features like syntax-table or fontification to do some tricks but I imagine that only LaTeX can exactly tell if a string is in tex or math mode. Also a viewer allowing regex search would be helpful (but I know this is very tricky) – Gabriele Nicolardi Jul 27 '17 at 13:35
• ChkTex picks up this and several other related errors. – Andrew Swann Aug 2 '17 at 8:38

You could use xelatex and \XeTeXinterchartoks:

\documentclass[11pt]{article}
%
\usepackage{xcolor}

\newXeTeXintercharclass\mypunctclass
\XeTeXcharclass \. \mypunctclass
\XeTeXcharclass \, \mypunctclass
\XeTeXinterchartokenstate=1
\XeTeXinterchartoks 4095 \mypunctclass{\textcolor{red}{TYPO}}

\pagestyle{empty}
\begin{document}

Punctuation , typos .

Punctuation, typos.

No problem with this string
$$(\mu + \nu) ,$$
\end{document}


• I get: ! Bad register code (4095). l.9 \XeTeXinterchartoks 4095 \mypunctclass{\textcolor{red}{TYPO}} Do I need to upgrade mi TexLive version? pdfTeX 3.14159265-2.6-1.40.16 (TeX Live 2015) – Gabriele Nicolardi Jul 27 '17 at 21:23
• You have an older xetex, change the number to 255 (or update your system). – Ulrike Fischer Jul 27 '17 at 21:26
• I tried with 255 but I got: ! TeX capacity exceeded, sorry [main memory size=5000000]. – Gabriele Nicolardi Jul 27 '17 at 21:31
• Hm. Looks as if doesn't like the color. It works with \XeTeXinterchartoks 255 \mypunctclass{TYPO}. – Ulrike Fischer Jul 27 '17 at 21:37
• Ok, thanks. This solution seems very promising. Where can I get the infos to learn about this feature of xelatex. I'd like to learn how to create other highlights by myself (e.g. for strings like "( ", " ]" or " ?"... so to have the complete set of punctuation errors. I programmed to update my TexLive in September. – Gabriele Nicolardi Jul 27 '17 at 21:47

In keeping with the Carlisle tradition, I am naming this "gem" \z. Done here for commas, periods and colons, but added loops could be set up for other tests.

I would be remiss if I didn't draw your attention to Ulrike's comments, noting that there are many pitfalls for \z. For example verbatim material, as well as the occurrence of the offending syntax inside a valid argument list are likely to cause compilation errors.


\documentclass[11pt]{article}
\usepackage{xcolor,amsmath}
\makeatletter
\newcommand\z[1]{\def\z@save{}\z@A#1 ,\z@end\z@save}
\long\def\z@A#1 ,#2\z@end{%
\z@B#1 .\z@end%
\ifx\z@end#2\else\zz@{\ifmmode{} ,\else\colorbox{cyan}{ ,}\fi}\z@A#2\z@end\fi}
\long\def\z@B#1 .#2\z@end{%
\z@C#1 :\z@end%
\ifx\z@end#2\else\zz@{\ifmmode{} .\else\colorbox{orange}{ .}\fi}\z@B#2\z@end\fi}
\long\def\z@C#1 :#2\z@end{%
\zz@{#1}%
\ifx\z@end#2\else\zz@{\ifmmode{} :\else\colorbox{yellow}{ :}\fi}\z@C#2\z@end\fi}
\makeatother
\pagestyle{empty}
\begin{document}
\z{
Punctuation , typos : such as 3 .75 and such .

No problem with this string
$$(3 .7\mu + \nu) ,$$

\begin{align}
\mathcal{A}_{\text{reg}}:\qquad ( - l_u/2+\epsilon_u , - l_\phi/2+\epsilon_\phi )
\rightarrow ( l_u/2-\epsilon_u , l_\phi/2-\epsilon_\phi )\,. \end{align}
And again , are there these problems : a nd b .
}
\end{document}


• This is impressive, but wouldn't this be more conveniently handled in LuaLaTeX? – JPi Jul 27 '17 at 13:45
• @JPi If only I knew lualatex... – Steven B. Segletes Jul 27 '17 at 13:45
• Wow. A whole document body in an argument. It better shouldn't contain verbatim material, or tikz pictures, or keyval arguments with spaces. Try e.g. \num[group-digit , groupseparator= . ]{123456}. – Ulrike Fischer Jul 27 '17 at 14:13
• Ok, It works with small files but when I wrap a 3000 lines long .tex file someting goes wrong. I have this error: (/usr/local/texlive/2015/texmf-dist/tex/latex/base/omscmr.fd) [5] [6] [7] [8] [9] [10] [11] [12] [13] ! Missing $inserted. <inserted text>$ l.3066 }\end{document} – Gabriele Nicolardi Jul 27 '17 at 14:16
• @GabrieleNicolardi This isn't unexpected, I'm more surprised that it worked for 13 pages. – Ulrike Fischer Jul 27 '17 at 14:18