0

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:

enter image description here

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.

Thank you in advance for your help!


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}

enter image description here

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)\\}
3
  • @Zarko I would need the equation number so that I can refer to it later in the document.
    – f10w
    May 20, 2021 at 11:01
  • @Zarko Please see the update.
    – f10w
    May 20, 2021 at 11:10
  • @Zarko I'm able to make it work now, but not sure if that's the optimal solution.
    – f10w
    May 20, 2021 at 11:15

3 Answers 3

1

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

inline equation tags

\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}
        \begin{equation}\label{option1}
            \text{The quick brown fox jumps over the lazy dog. We have }    a + b + c = d.
        \end{equation}
    \endgroup
    Therefore, ...
    \begin{equation}
        a^2+b^2=c^2
    \end{equation}

    \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, ...

    \begin{equation}
        a^2+b^2=c^2
    \end{equation}

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

Credit for the right align code to this answer.

2
  • 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).
    – f10w
    May 20, 2021 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, 2021 at 16:27
1

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} 

enter image description here

1
  • Interesting solution. Thanks!
    – f10w
    May 20, 2021 at 14:03
0

The following does the job:

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

You must log in to answer this question.

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