1

I am looking for a way to insert a counter in the math mode $ .* $. In particular, I am writing a document in LaTex and I want to check the grammar within the document with a language tool. First, I removed all the math by following the answer to the following question: remove everything in math mode. However, by removing all the math, the language tool reports errors because of the empty string between words. Therefore, I would like to insert a counter in order to have something between words. I tried the following

\newcounter{equat}
\setcounter{equat}{1}
\newcommand{\newEQ}{\refstepcounter{equat}\Alph{equat}}
\def$#1${\newEQ}

but apparently, I cannot use \stepcounter, \refstepcounter or \addtocounter in \def$#1${.*}. Does anyone know how I can increase the above counter? I thought about inserting the \refstepcounter before (or after) the math mode, but I cannot find how to do it.

I thank everyone in advance!


Working example

I have this:

\documentclass{article}

\usepackage{comment}
\usepackage{verbatim}
\def\[#1\]{}
\def\(#1\){}
\catcode`\$=13
\def$$#1$${}
\def$#1${}

\makeatletter
\renewenvironment{subequations}{\comment}{\endcomment}
\renewenvironment{equation}{\comment}{\endcomment}
\renewenvironment{alignat}{\comment}{\endcomment}
\renewenvironment{align}{\comment}{\endcomment}
\renewenvironment{equation*}{\comment}{\endcomment}
\renewenvironment{alignat*}{\comment}{\endcomment}
\renewenvironment{align*}{\comment}{\endcomment}
\makeatother

\begin{document}
    This is an example with $ E = m c^2 $ and 
    \begin{equation*}
        \cos^2(\alpha) + \sin^2(\alpha) = 1.
    \end{equation*}
\end{document}

If I compile the above code, I get:

This is an example with and 

However, I want something like the following

This is an example A with B.

where A and B are the alphabetic conversion of a counter.

If I put the command \newEQ inside the redefinition of the above environment, the I solve this problem only for the environments:

\makeatletter
\renewenvironment{subequations}{\newEQ\comment}{\endcomment\ }
\renewenvironment{equation}{\newEQ\comment}{\endcomment\ }
\renewenvironment{alignat}{\newEQ\comment}{\endcomment\ }
\renewenvironment{align}{\newEQ\comment}{\endcomment\ }
\renewenvironment{equation*}{\newEQ\comment}{\endcomment\ }
\renewenvironment{alignat*}{\newEQ\comment}{\endcomment\ }
\renewenvironment{align*}{\newEQ\comment}{\endcomment\ }
\makeatother

Indeed, I got the following result

This is an example with A
4
  • Please write a minimal working example. See link for more details what it means.
    – user202729
    Jan 2 at 11:06
  • 4
    You can't use \def$#1$ in the first place, no matter what you're doing. If you want numbered equations use the equation environment.
    – Skillmon
    Jan 2 at 11:10
  • 2
    "no matter what you're doing" might factually be incorrect, this is possible with a bit of extra code, but I won't ever advise a beginner to do this, this is bound to bring lots of errors.
    – Skillmon
    Jan 2 at 11:24
  • @Skillmon I do not want to number the equation. By the way, the working example is the same as in the question I linked (where there is also the line catcode'$=\active before \def$#1$. I want to substitute the formulas into the math mode with a counter in order to have a clean PDF to analyze with a language tool
    – G.F
    Jan 2 at 13:02

1 Answer 1

1

The following works in the minimal example. Please note that \def$#1${} does overwrite the prior \def$$#1$${}, instead the following defines only $ and uses LaTeX's \@ifnextchar to see if the following char is another $, based on that it either gobbles until the next $ or $$.

Also I used the alphalph package so that your document can contain more than 26 equations.

\documentclass{article}

\usepackage{amsmath}
\usepackage{alphalph}

\newcounter{myequat}
\renewcommand*\themyequat{\AlphAlph{\value{myequat}}}
\newcommand*\newEQ{}
\protected\def\newEQ{\refstepcounter{myequat}\themyequat}

\usepackage{comment}
\usepackage{verbatim}
\protected\def\[#1\]{\newEQ}
\protected\def\(#1\){\newEQ}
\makeatletter
\catcode`\$=13
\protected\def${\newEQ\@ifnextchar$\@gobbledisplay\@gobbleinline}
\def\@gobbledisplay$#1$${}
\def\@gobbleinline#1${}
\makeatother

\makeatletter
\renewenvironment{subequations}{\newEQ\comment}{\endcomment}
\renewenvironment{equation}{\newEQ\comment}{\endcomment}
\renewenvironment{alignat}{\newEQ\comment}{\endcomment}
\renewenvironment{align}{\newEQ\comment}{\endcomment}
\renewenvironment{equation*}{\newEQ\comment}{\endcomment}
\renewenvironment{alignat*}{\newEQ\comment}{\endcomment}
\renewenvironment{align*}{\newEQ\comment}{\endcomment}
\makeatother

\begin{document}
    This is an example with $ E = m c^2 $ and 
    \begin{equation*}
        \cos^2(\alpha) + \sin^2(\alpha) = 1.
    \end{equation*}
\end{document}
4
  • Thank you so much! It works but there is still a problem. When formulas are into the title of a section, it is not possible to compile as I got this message Missing \endcsname inserted.. Is it possible to solve this latter problem?
    – G.F
    Jan 2 at 14:35
  • @G.F how should they get numbered in the ToC? Independent from their occurences later in the document? If so, put \protect before each $ in section titles, or change the line defining it to \protected\def${\@ifnextchar$\@gobbledisplay\@gobbleinline}.
    – Skillmon
    Jan 2 at 15:26
  • same for \( and \[, add a \protected in front of \def.
    – Skillmon
    Jan 2 at 15:28
  • thank you so much!
    – G.F
    Jan 2 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .