7

Is there a way to get all the inline math ie., equations enclosed with $...$ and \(...\) in a TeX file should print in the end of the document like endfloat functionality.

RampSmart
  • 103
  • 1
    Yes! If you define a macro say \inlineeq{..} and add collect them. But why? – yannisl Nov 09 '23 at 09:55
  • @Yiannis: Without a macro definition by using any package or by means of any patch the value should get print from $...$ to end of the document. Basically i need this for a conversion activity, to compare every inline math before and after conversion. – RampSmart Nov 09 '23 at 10:01
  • here is a solution for the \(..\) `\documentclass{article} \ExplSyntaxOn \clist_new:N\inline_math_store_clist \def(#1){\begingroup \clist_put_right:Nn\inline_math_store_clist{#1} }% \def){\endgroup}% \def\collectedequations{ \clist_map_inline:Nn\inline_math_store_clist{ \math##1\endmath }}
    \ExplSyntaxOff \begin{document}

    A test (a^2) and (a^3)

    \collectedequations \end{document}`

    – yannisl Nov 09 '23 at 10:38

4 Answers4

8

In plain TeX, we can use \everymath primitive register:

\def\eqlist{}
\def\collectedequations{\everymath={}
   \ifx\eqlist\empty no equations\else \eqlist \fi}
\everymath={\savemath}
\def\savemath#1${#1$\global\addto\eqlist{$#1$\par}}
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}

A test $a^2=aa$ and $a^3=aaa$ and $a+b+c=42$.

\collectedequations

\bye

wipet
  • 74,238
  • 2
    Brilliant! I forgot about everymath – yannisl Nov 09 '23 at 11:17
  • Excellent! Will this macro impact $$...$$ display equation. If yes, what can be the alternative. – RampSmart Nov 09 '23 at 12:31
  • The \everymath is working only with in-line math. The display math runs an alternative TeX register \everydisplay which isn't used in the example macro. – wipet Nov 09 '23 at 12:57
  • I am getting the below error message for display equation because of the above macro that's y requested for suggestion.

    ! Display math should end with $$.

    \global l.155 \end{equation}

    ? ! Argument of \savemath has an extra }.

    \par l.155 \end{equation}
    – RampSmart Nov 09 '23 at 13:16
  • It seems that you are using LaTeX. I never guarantee that my macros will work with LaTeX. Maybe, LaTeX manages with \everymath and \everydisplay by a specific way... – wipet Nov 09 '23 at 13:46
  • Did you try to add a footnote to the text? – egreg Nov 09 '23 at 16:36
  • Yes, below modifed LaTeX macro is working for footnote – RampSmart Nov 09 '23 at 16:43
7

Beware of methods suggesting \everymath:

\def\eqlist{}
\def\collectedequations{\everymath={}
   \ifx\eqlist\empty no equations\else \eqlist \fi}
\everymath={\savemath}
\def\savemath#1${#1$\global\addto\eqlist{$#1$\par}}
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}

$0=0$

a\footnote{b}

\collectedequations

\bye

Running tex on this example raises the quite cryptic error message

Runaway argument?
! Paragraph ended before \next was complete.
<to be read again>
                   \par
l.11

Assuming LaTeX you can use \(...\) with a suitable redefinition.

\documentclass{article}

\ExplSyntaxOn

\seq_new:N \g__rampsmart_formulas_seq

\RenewDocumentCommand{(}{} { \rampsmart_collectmath:w }

\cs_new_protected:Npn \rampsmart_collectmath:w #1 ) { \seq_gput_right:Nn \g__rampsmart_formulas_seq { $#1$ } $#1$ }

\NewDocumentCommand{\printformulas}{+m} { \cs_set:Nn __rampsmart_collectmath_print:nn { #1 } \seq_map_indexed_function:NN \g__rampsmart_formulas_seq __rampsmart_collectmath_print:nn }

\ExplSyntaxOff

\begin{document}

\section{Text}

(0=0) is true; to the contrary, (0=1) is false. [ x(yz)=x(yz) ]

\section{Formulas}

Here are all the formulas:

\printformulas{#1. #2\par}

\section{Again}

In a different format: \begin{enumerate} \printformulas{\item #2} \end{enumerate}

\end{document}

The argument to \printformulas is the template to be used: #1 stands for the serial number and #2 for the actual formula.

enter image description here

Following Yiannis' invitation, here's an example of how one can use the two arguments in the template for getting hyperlinks.

\documentclass{article}
\usepackage{hyperref}

\ExplSyntaxOn

\seq_new:N \g__rampsmart_formulas_seq

\RenewDocumentCommand{(}{} { \rampsmart_collectmath:w }

\cs_new_protected:Npn \rampsmart_collectmath:w #1 ) { \seq_gput_right:Nn \g__rampsmart_formulas_seq { $#1$ } \hypertarget{cf@\seq_count:N \g__rampsmart_formulas_seq}{$#1$} }

\NewDocumentCommand{\printformulas}{+m} { \cs_set:Nn __rampsmart_collectmath_print:nn { #1 } \seq_map_indexed_function:NN \g__rampsmart_formulas_seq __rampsmart_collectmath_print:nn }

\NewDocumentCommand{\formulalink}{mm} { \hyperlink{cf@#1}{#2} }

\ExplSyntaxOff

\begin{document}

\section{Text}

(0=0) is true. \newpage

To the contrary, (0=1) is false. [ x(yz)=x(yz) ]

\section{Formulas}

Here are all the formulas:

\printformulas{#1. #2\par}

\section{Hyperlinks}

\begin{enumerate} \printformulas{\item \formulalink{#1}{#2}} \end{enumerate}

\end{document}

The target is given a name based on the current number of items in the sequence where we store the formulas, so we can use the index for defining the hyperlink.

enter image description here

egreg
  • 1,121,712
3

Here is a short solution, works for the \(...\)

\documentclass{article}
\ExplSyntaxOn
\clist_new:N\inline_math_store_clist
\long\def\(#1\){\begingroup
\clist_put_right:Nn\inline_math_store_clist{#1}
$#1$
}%
\def\){\endgroup}%
\def\collectedequations{
\clist_map_inline:Nn\inline_math_store_clist{
  \math##1\endmath\par
}}  
\ExplSyntaxOff
\begin{document}

A test (a^2=aa) and (a^3=aaa) and ( a+b+c=42)

\collectedequations \end{document}

yannisl
  • 117,160
0

Thanks @Wipet and @Yiannis your input really helped me put to find a solution on this request. Then, solution discussed by @Circumscribe from the link Automatically list all equations from document also helped to achieve the whole. I have supplied my code change below for LaTeX files.

\documentclass{article}

\usepackage{etoolbox}

\newbox\savedeqs
\newbox\inlinemath
\newbox\unnummath
\newbox\inlineparen
\newbox\unnumparen
\newwrite@mathfile

\newcommand\saveandprinteq[1]{%
\begingroup
\expandafter\let\csname @currenvir\expandafter\endcsname\csname listeq@@currenvir\endcsname
\expandafter\let\csname end@currenvir\expandafter\endcsname\csname listeq@end@currenvir\endcsname
\edef\listeq@temp{%
\noexpand\begin{@currenvir}%
\unexpanded{#1}%
\noexpand\end{@currenvir}%
}%
\savecounters@%
\global\setbox\savedeqs=\vbox{\unvbox\savedeqs\listeq@temp}%
\restorecounters@%
\listeq@temp%
\endgroup}

\newcommand*\listeqpatch[1]{% %% <- patches equation environment
\expandafter\let\csname listeq@#1\expandafter\endcsname\csname #1\endcsname
\expandafter\let\csname listeq@end#1\expandafter\endcsname\csname end#1\endcsname
\renewenvironment{#1}{\collect@body\saveandprinteq}{}}

\begingroup
\catcode`$=\active
\protected\gdef${@ifnextchar$@doubledollar@singledollar}
\protected\gdef@doubledollar$#1$${\edef\listueq@temp{\detokenize{$$#1$$}}%
\savecounters@%
\global\setbox\unnummath=\vbox{\unvbox\unnummath\listueq@temp}%
\restorecounters@%
\protected@write@mathfile{}{\listueq@temp}}
\protected\gdef@singledollar#1${\edef\listieq@temp{\detokenize{$#1$\par}}%
\savecounters@%
\global\setbox\inlinemath=\vbox{\unvbox\inlinemath\listieq@temp}%
\restorecounters@%
\protected@write@mathfile{}{\listieq@temp}}
\protected\gdef(#1){
\edef\listipeq@temp{\detokenize{$#1$\par}}%
\savecounters@%
\global\setbox\inlineparen=\vbox{\unvbox\inlineparen\listipeq@temp}%
\restorecounters@%
\protected@write@mathfile{}{\listipeq@temp}}%
\protected\gdef[#1]{
\edef\listupeq@temp{\detokenize{$$#1$$}}%
\savecounters@%
\global\setbox\unnumparen=\vbox{\unvbox\unnumparen\listupeq@temp}%
\restorecounters@%
\protected@write@mathfile{}{\listupeq@temp}}
\endgroup

\AtBeginDocument{%
\immediate\openout@mathfile=\jobname-MathFile.tex
\catcode`$=\active}

\newcommand\listofequations{%
\immediate\closeout@mathfile%
\catcode`$=3%
\clearpage%
\pagestyle{empty}%
\textbf{\Large List of Equations}\vskip10pt
\input \jobname-MathFile.tex%
\unvbox\savedeqs}

%% Patching equation environments
\listeqpatch{gather}
\listeqpatch{alignat}
\listeqpatch{xalignat}
\listeqpatch{align}
\listeqpatch{flalign}
\listeqpatch{equation}
\listeqpatch{eqnarray}
\listeqpatch{multline}
\listeqpatch{gather*}
\listeqpatch{alignat*}
\listeqpatch{xalignat*}
\listeqpatch{align*}
\listeqpatch{flalign*}
\listeqpatch{equation*}
\listeqpatch{eqnarray*}
\listeqpatch{multline*}

\begin{document}

$a+b=0$

\(c-d=0\)

$$a+b=0$$

\[c-d=0\]

\begin{equation}
a+b=0
\end{equation}

... ... ...

\listofequations

\end{document}

RampSmart
  • 103
  • Please never but html markup into code blocks. If anybody copies your code, it is full of <br /> and if anybody tries to format your code, they become visible. Removing them all is a pain! Have you read egreg's warning about using this? – cfr Nov 10 '23 at 06:03
  • Thanks egreg and cfr. I'll avoid
    in future post.
    – RampSmart Nov 10 '23 at 07:19