40

While searching for the source of missing line numbers, I realized that paragraphs followed immediately by an equation have no line number, but this is fixed when a space is included,

e.g. the following document has no line numbers:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{lineno}
\linenumbers
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit: 
$$1+1=2$$ 
\end{document}

But this one does:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{lineno}
\linenumbers
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit: 

$$1+1=2$$ 
\end{document}

The only difference is the space above the equation.

  • Why is this the case (and is there a simple workaround)?

  • Is it incorrect to include an indented equation in a paragraph, or is this just a 'feature' of lineno?

6 Answers 6

29

$$...$$ is obsolete, see Why is \[ ... \] preferable to $$ ... $$?. If you use the correct LaTeX displayed math environment, the numbering works without the empty line:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{lineno}
\linenumbers
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit: 
\[1+1=2\] 
\end{document}

enter image description here

6
  • thank you, is $ also obsolete? Commented Aug 15, 2011 at 21:04
  • 3
    @David: that's true, see: Are \( and \) preferable to $. At least: \( ... \) is LaTeX syntax, so better supported in LaTeX. $...$ is plain TeX. However, usually both work well, many prefer $...$ because it's easier to write or just classic.
    – Stefan Kottwitz
    Commented Aug 15, 2011 at 21:09
  • 3
    @David: \( and \) are fragile by default. So, as Will Robertson said, don't use them unless you use fixltx2e package. And the default \( and \) have very few advantages compared with $.
    – Leo Liu
    Commented Aug 16, 2011 at 5:05
  • @StefanKottwitz, what could I do if I need numbered equations?
    – Sigur
    Commented Apr 3, 2013 at 20:08
  • @Sigur Use an equation environment.
    – Stefan Kottwitz
    Commented Apr 3, 2013 at 20:37
35

For line numbering to be done correctly the math environments has to be wrapped using the \begin{linenomath*} and \end{linenomath*} code:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{lineno}
\linenumbers
\begin{document}
For line numbering to be done correctly the math environments has to be wrapped using the ''linenomath`` code as follows: 

\begin{linenomath*}
    \begin{equation}
        a^2=b^2+c^2
    \end{equation}
\end{linenomath*}
some text here some text here  some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text heresome text here. 
\end{document}

enter image description here

2
  • 3
    This is particularly valuable, because it works for the AMS align environment, too!
    – AJK
    Commented Nov 21, 2015 at 0:41
  • What's the difference between having linenomath and not having linenomath?
    – Y. zeng
    Commented May 22, 2022 at 1:59
25

Mathematics environments need to be wrapped by \begin{linenomath*} and \end{linenomath*} as mentioned in Bamzi's answer.

A quick way to change all your equations is to renew the equation environment by adding the code

\let\oldequation\equation
\let\oldendequation\endequation

\renewenvironment{equation}
  {\linenomathNonumbers\oldequation}
  {\oldendequation\endlinenomath}

before \begin{document}.

4
  • Welcome to TeX.SX!
    – Bobyandbob
    Commented Dec 5, 2017 at 15:18
  • 3
    I'd say this is the most convenient answer. Thanks! Commented Oct 18, 2018 at 10:36
  • 2
    This answer should be upvoted to top. Definitely.
    – WDC
    Commented Nov 26, 2018 at 3:19
  • How do we apply lineno with \begin{equation*} and \end{equation*} The above does not seem to work for me for unsing lineno on paragraphs where I have unnumbered equations. Commented Jul 26, 2022 at 23:59
3

As @Denis has said, the mathlines option can be used in some cases (for article, but not for iopart, for example). If you do so, and you are also using the amsmath package, be sure to load lineno after amsmath. Sometimes other packages load amsmath, be aware of that; you can check the log file to see this. This MWE can be used to easily see the effect of that change:

\documentclass{article}
% \usepackage[mathlines]{lineno}
\usepackage{amsmath}
\usepackage[mathlines]{lineno}

\begin{document}
    \linenumbers

    Line number

    No line number if lineno is loaded before amsmath!
    \begin{equation}
        x = y
    \end{equation}

    Line number
\end{document}
1
  • Beware that this still screws up when using the align environment. It will then neither number the math line nor the text lines of the paragraph above it.
    – Bastian
    Commented Jul 30 at 14:45
2

Use the mathlines option for lineno and write:

\begin{linenomath}
\begin{align*}
z_{1}&=x_{1}+y_{1}\\
z_{2}&=x_{2}+y_{2}
\end{align*}
\end{linenomath}

to obtain

enter image description here

1
  • (a) This requires \usepackage[mathlines}{lineno}. (b) The output is still erroneous as there are three line numbers for two lines of math.
    – Bastian
    Commented Jul 30 at 14:48
2

The answer from Stefan Pinnow fixed the equation command. Use this to fix the align command in the same style:

\let\oldalign\align
\let\oldendalign\endalign

\renewenvironment{align}
  {\linenomathNonumbers\oldalign}
  {\oldendalign\endlinenomath}
3
  • 2
    Your answer is basically a duplicateof R.M asnwer here
    – Roland
    Commented Mar 24, 2022 at 3:57
  • How do we apply lineno with \begin{align*} and \end{align*} The above does not seem to work for me for using lineno on paragraphs where I have unnumbered equations Commented Jul 27, 2022 at 0:39
  • @Roland no it's not, this answer says explicitely that it is to fix the align command
    – Tropilio
    Commented Aug 10, 2023 at 15:10

You must log in to answer this question.

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