0

Sometimes I use software to generate Latex which does not support breqn package. It can create equation environment.

Is it possible to add some code, in the preamble only, which converts any \begin{equation}...\end{equation} to dmath environment?

The problem is when there is a \tag{number} at the end of the equation. Otherwise, using \renewenvironment would work.

Here is a MWE

\documentclass[12pt,notitlepage]{article}

\usepackage{amsmath}
\usepackage{breqn}
\renewenvironment{equation}{\begin{dmath*}}{\end{dmath*}}

\begin{document} 

\begin{equation}
-\eta\omega_{y}+\left(  -3y' \xi_{y}-2\xi_{x}+\eta_{y}\right)
\omega-\xi\omega_{x}+\left(  -y' \eta_{y}+\left(  y'\right)
^{2}\xi_{y}+y' \xi_{x}-\eta_{x}\right)  \omega_{y'}+\eta
_{xx}+\eta_{yy}\left(  y' \right)  ^{2}-
\xi_{yy}\left(  y' \right)^{3}-2\xi_{yx}\left(  y'\right)  ^{2}+2\eta_{yx}y'-\xi_{xx} y'=0\tag{1A} 
\end{equation}

\end{document}

Because of the \tag{...} there at the end, it fail to compile. (using lualatex) gives

/usr/local/texlive/2023/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))

! Package amsmath Error: \tag not allowed here.

See the amsmath package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.62 ...\right)  ^{2}+2\eta_{yx}y'-\xi_{xx} y'=0\tag
                                                  {1A}
? 

If I remove the \tag{1A} it works and compiles giving

enter image description here

Since can't have \tag{} in dmath*, it has to be translated to

\begin{dmath}[number=1A]
.....
\end{dmath}

But I do not know how check for \tag{...} and generate the correct dmath.

So there are two cases: equation with no tag in it, which becomes

\begin{dmath*}
.....
\end{dmath*}

This case I can do using

\renewenvironment{equation}{\begin{dmath*}}{\end{dmath*}}

The second case is where there is a \tag{N} in the equation, then this should become

\begin{dmath}[number=N]
.....
\end{dmath}

And it is this second case which is difficult.

Is it possible to handle both cases using some code in preamble? I use lualatex and the code to use can only be added to preamble, not after \begin{document}

Any suggestions how to handle both cases? The main issue is handling \tag{}. Nothing else is generated by the software, such as \label, It only generates \tag or no \tag at the end.

The reason I need to do this, is that some equations generated are very long, and I use breqn package to break them. But the software I use does not support dmath environment. Editing the code by hand to do the changes is not an option for me.

TL 2023

2
  • why have you redefined the (numbered) equation env to be the (not numbered) dmath* rather than dmath ??? Commented Sep 23, 2023 at 8:10
  • @DavidCarlisle Because it is my mistake. I actually never use equation environment myself when I write direct Latex, so not familiar with it. But SW generates these. I only use align and dmath. So I thought an equation without a tag, is like align* or like dmath*, i.e. no equation number. But when I just tried it, I found equation with no `tag`` still generate an automatic equation number.
    – Nasser
    Commented Sep 23, 2023 at 15:40

1 Answer 1

2

First of all, you want dmath, not dmath*.

\documentclass[12pt,notitlepage]{article}

\usepackage{amsmath}
\usepackage{breqn}

\ExplSyntaxOn

\RenewDocumentEnvironment{equation}{b}
 {
  \nasser_equation:n { #1 }
 }{}

\tl_new:N \l__nasser_equation_body_tl

\cs_new_protected:Nn \nasser_equation:n
 {
  \regex_match:nnTF { \c{tag} } { #1 }
   { \__nasser_equation_tag:n { #1 } }
   { \begin{dmath}#1\end{dmath} }
 }

\cs_new_protected:Nn \__nasser_equation_tag:n
 {
  \tl_set:Nn \l__nasser_equation_body_tl { #1 }
  \regex_replace_once:nnN
   { (.*)\c{tag}\{(.*?)\}(.*) } % search
   { \c{begin}\{dmath\}\[number=\2\]\1\3\c{end}\{dmath\} }
   \l__nasser_equation_body_tl
  \tl_use:N \l__nasser_equation_body_tl
 }

\ExplSyntaxOff

\begin{document} 

No tag here, normal numbering
\begin{equation}
-\eta\omega_{y}+\left(  -3y' \xi_{y}-2\xi_{x}+\eta_{y}\right)
\omega-\xi\omega_{x}+\left(  -y' \eta_{y}+\left(  y'\right)
^{2}\xi_{y}+y' \xi_{x}-\eta_{x}\right)  \omega_{y'}+\eta
_{xx}+\eta_{yy}\left(  y' \right)  ^{2}-
\xi_{yy}\left(  y' \right)^{3}-2\xi_{yx}\left(  y'\right)  ^{2}+2\eta_{yx}y'-\xi_{xx} y'=0
\end{equation}

Tag at the end
\begin{equation}
-\eta\omega_{y}+\left(  -3y' \xi_{y}-2\xi_{x}+\eta_{y}\right)
\omega-\xi\omega_{x}+\left(  -y' \eta_{y}+\left(  y'\right)
^{2}\xi_{y}+y' \xi_{x}-\eta_{x}\right)  \omega_{y'}+\eta
_{xx}+\eta_{yy}\left(  y' \right)  ^{2}-
\xi_{yy}\left(  y' \right)^{3}-2\xi_{yx}\left(  y'\right)  ^{2}+2\eta_{yx}y'-\xi_{xx} y'=0\tag{1A} 
\end{equation}

Tag at the beginning
\begin{equation}\tag{B}
-\eta\omega_{y}+\left(  -3y' \xi_{y}-2\xi_{x}+\eta_{y}\right)
\omega-\xi\omega_{x}+\left(  -y' \eta_{y}+\left(  y'\right)
^{2}\xi_{y}+y' \xi_{x}-\eta_{x}\right)  \omega_{y'}+\eta
_{xx}+\eta_{yy}\left(  y' \right)  ^{2}-
\xi_{yy}\left(  y' \right)^{3}-2\xi_{yx}\left(  y'\right)  ^{2}+2\eta_{yx}y'-\xi_{xx} y'=0
\end{equation}

No \verb|\left| and \verb|\right|
\begin{equation}
-\eta\omega_{y}+(  -3y' \xi_{y}-2\xi_{x}+\eta_{y})
\omega-\xi\omega_{x}+(  -y' \eta_{y}+(  y')
^{2}\xi_{y}+y' \xi_{x}-\eta_{x})  \omega_{y'}+\eta
_{xx}+\eta_{yy}(  y' )  ^{2}-
\xi_{yy}(  y' )^{3}-2\xi_{yx}(  y')  ^{2}+2\eta_{yx}y'-\xi_{xx} y'=0
\end{equation}
\end{document}

The last example is just for showing how the formula should be typeset, without horribly large parentheses.

enter image description here

As usual, the output of breqn is far from being nice.

You must log in to answer this question.

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