# breqn and lineno incompatibility

Journals often require line numbering for the review process. This is achieved using lineno package. Next, it seems like a good idea to use breqn package (dmath environment) to auto-break long formulas.

But, not switching line numbering off before dmath environment (\begin{nolinenumbers}, \end{nolinenumbers}), causes pdftex to hang (it just consumes 100% CPU, and compilation never finishes).

1. Why is this happening?
2. How to redefine dmath (and dmath*) environment to have nolinenumbers environment automaticly added around it.

\documentclass[twocolumn,12pt]{article}
\usepackage{breqn}
\usepackage{lineno}

\begin{document}
\linenumbers

First paragraph.

\begin{nolinenumbers}
\begin{dmath}
f(x)=x^9+x^8-x^7+x^6-x^5-x^4-x^3+x^2-x+1.
\end{dmath}
\end{nolinenumbers}
\end{document}

-
Do you really mean pdftex? Your MWE compiles fine on my machine using pdfLaTeX. For your other request you can use etoolbox and issue \BeforeBeginEnvironment{dmath}{\begin{nolinenumbers}} \AfterEndEnvironment{dmath}{\end{nolinenumbers}} –  Jörg Dec 11 '12 at 14:44
I didn't say that this code didn't compile. If you read carefully you will see "But, not switching line numbering off before dmath environment...causes pdftex to hang". For this code to compile, I have to use \begin{nolinenumbers} ... \end{nolinenumbers}. Your solu –  troubler Dec 11 '12 at 14:47
Your solution for the 2nd question doesn't seem to work: ./Untitled.tex:21: LaTeX Error: \begin{nolinenumbers} on input line 17 ended by \end{document}. –  troubler Dec 11 '12 at 14:53
How can you use pdftex and \documentclass? Anyway, the lineno manual states that it does not work well with math environments and provides a few patches for this. As you identified, one way to solve is to turn off line numbering for math. –  Jörg Dec 11 '12 at 14:55
After clicking Typeset in TeXShop, I see process pdftex consuming 99% CPU. –  troubler Dec 11 '12 at 15:12

Load etoolbox before breqn:

\documentclass[twocolumn,12pt]{article}

\usepackage{etoolbox}
\usepackage{breqn}
\usepackage{lineno}

\BeforeBeginEnvironment{dmath}{\begin{nolinenumbers}}%
\AfterEndEnvironment{dmath}{\end{nolinenumbers}}

\begin{document}
\linenumbers

First paragraph.

%\begin{nolinenumbers}
\begin{dmath}
f(x)=x^9+x^8-x^7+x^6-x^5-x^4-x^3+x^2-x+1.
\end{dmath}
%\end{nolinenumbers}

Second Paragraph
\end{document}


-
This works. Thanks! –  troubler Dec 11 '12 at 15:01
This is what my comment suggested, the order of loading etoolbox is important. –  Jörg Dec 11 '12 at 15:04

The lineno manual states that it "does not work well with display math" (p.13). One way is to wrap equations in in the linenomath environment for common math environments, but this does not work for dmath. So we may use etoolbox to patch dmath to exclude linenumbers automatically (and equivalently for dmath*).

\documentclass[twocolumn,12pt]{article}

\usepackage{etoolbox}

\usepackage{breqn}
\usepackage{lineno}

\BeforeBeginEnvironment{dmath}{\begin{nolinenumbers}}
\AfterEndEnvironment{dmath}{\end{nolinenumbers}}

\BeforeBeginEnvironment{dmath*}{\begin{nolinenumbers}}
\AfterEndEnvironment{dmath*}{\end{nolinenumbers}}

\begin{document}
\linenumbers

First paragraph.

\begin{dmath}
f(x)=x^9+x^8-x^7+x^6-x^5-x^4-x^3+x^2-x+1.
\end{dmath}

Second paragraph

\begin{dmath*}
f(x)=x^9+x^8-x^7+x^6-x^5-x^4-x^3+x^2-x+1.
\end{dmath*}

\end{document}


Loading etoolbox after breqn and carefully reading the error message reveals that etoolbox fails to patch \end and hence the \AfterEndEnvironment will not work. There appears to be an incompatibility (probably breqn patches something as well). So simply load etoolbox before.

-
linenomath doesn't work with dmath environment! If it did, I would not be asking this question! –  troubler Dec 11 '12 at 15:08