# Putting numbered equations in the remaining space of the line

I would like to define a new equation environment that displays the equation inline, but with its number being displayed at the end of the line (without text between them).

More concretely, the following code:

The quick brown fox jumps over the lazy dog. We have
\begin{equation-inline}
a + b + c = d.
\end{equation-inline}
Therefore, ...


should output something like this:

Motivation: I am writing a document with some space constraints and I would be able to save a few lines by replacing some display-style centered equations with the above.

There have been quite a lot of questions on labeled inline equations, for example this one or this one, but my question is a bit different: I would like the equation number to be displayed at the end of the line so that it can be easily located.

Update:

With the following definition of equation-inline, the equation is displayed correctly but referencing to it doesn't work:

\documentclass{article}
\usepackage{amsmath}

\newenvironment{equation-inline}{$\stepcounter{equation}}{$\hfill(\theequation)\\}

\begin{document}

The quick brown fox jumps over the lazy dog. We have
\begin{equation-inline}\label{eq:x}
a + b + c = d.
\end{equation-inline}
According to~\eqref{eq:x}, we have...

\end{document}


Update 2:

Replacing \stepcounter{equation} in the above with \refstepcounter{equation} works (credit):

\newenvironment{equation-inline}{
% Put \refstepcounter at the beginning, because
% package hyperref' sets the anchor here.
\refstepcounter{equation}%
$}{$\hfill(\theequation)\\}

• @Zarko I would need the equation number so that I can refer to it later in the document.
– Khue
May 20 at 11:01
• @Zarko Please see the update.
– Khue
May 20 at 11:10
• @Zarko I'm able to make it work now, but not sure if that's the optimal solution.
– Khue
May 20 at 11:15

Although I disagree stylistically, this is how you could do it:

\documentclass[fleqn]{article}
\usepackage{amsmath}

\newcommand*\inlineTag{%
\refstepcounter{equation}%
\hspace*{0em plus 1fill}\makebox{(\theequation)}%
}

\begin{document}

\section{Option 1 (the manual way)}
The quick brown fox jumps over the lazy dog.
%
\begingroup
\setlength{\mathindent}{0pt}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
$$\label{option1} \text{The quick brown fox jumps over the lazy dog. We have } a + b + c = d.$$
\endgroup
Therefore, ...
$$a^2+b^2=c^2$$

\noindent
The inline equation is \eqref{option1}.

\section{Option 2 (the automatic way)}

The quick brown fox jumps over the lazy dog.\\
The quick brown fox jumps over the lazy dog. We have $a + b + c = d.$ \inlineTag\label{option2}\\
Therefore, ...

$$a^2+b^2=c^2$$

\noindent
The inline equation is \eqref{option2}.

\end{document}


Credit for the right align code to this answer.

• Thanks and +1. Option 1 is not interesting to me, while Option 2 is quite similar to my solution (I prefer using an environment because then I can switch easily between equation and equation-inline).
– Khue
May 20 at 11:31
• You shouldn't leave a blank line before a display; it will foul up the vertical spacing and will allow a page break. And if the text following the display is a continuation of the paragraph, you shouldn't have a blank line there either (and you won't need \noindent). May 20 at 16:27

It can be done with the linegoal package, which measures the remaining space on a line (requires two compilations), and nccmath:

\documentclass{article}
\usepackage{linegoal}
\usepackage{nccmath}
\usepackage{lipsum}

\newenvironment{inlineequation}{%
\minipage{\linegoal}\useshortskip\fleqn[0.5em]\equation}{\endequation\endfleqn\endminipage\linebreak}

\begin{document}

\noindent The quick brown fox jumps over the lazy dog. We have
\begin{inlineequation}
a + b + c = d.
\end{inlineequation}
\lipsum[11]

\end{document}


• Interesting solution. Thanks!
– Khue
May 20 at 14:03

The following does the job:

\newenvironment{equation-inline}{
% Put \refstepcounter at the beginning, because
% package hyperref' sets the anchor here.
\refstepcounter{equation}%
$}{$\hfill(\theequation)\\}