[Tex/LaTex] Inline equation number

cross-referencinginline()math-mode

I would like to be able to give inline equations using $…$ a label such that I can refer to it later in my thesis.

My goal in pseudocode:

Some text $mc^2(eqn number)\label{some eqn}$ some more text

And than be able to use

some text \ref{some eqn} some more text

Obviously the above does not work but I hope it gets along what I am hoping to achieve.


I will also sketch the reason for this inquiry because I am definitely open for other solutions than my idea above.

I frequently come across small formula that are not really worth a full:

\begin{equation}...\end{equation}

or

\begin{align} ... \end{align}

and are better mentioned in line. The problem is that it also happens that I need to refer to these small formula later in the work.

Best Answer

In case my \marginpar idea is ok for you, this would allow it:

\documentclass{scrartcl}

\usepackage{lipsum}
\usepackage{marginnote} % better placement than marginpar

%CODEBLOCK
\newcounter{inlinelabel}[section]
\let\theinlinelabelbak\theinlinelabel
\renewcommand{\theinlinelabel}{(\thesection.\theinlinelabelbak)}
\newcommand{\inlinelabel}[1]{%
    \marginnote{%
        \refstepcounter{inlinelabel}\label{#1}%
        \theinlinelabel%
    }%
}
%CODEBLOCK

\begin{document}
\section{first section}
$mc^2$\inlinelabel{ineq:mc2}
\lipsum[1]
also see equation \ref{ineq:mc2}.
\end{document}

To avoid confusion, perhaps you shouldn't use a custom counter but the equation counter. For this change the above contents of %CODEBLOCK to:

\newcommand{\inlinelabel}[1]{%
    \marginnote{%
        \refstepcounter{equation}\label{#1}%
        \theequation%
    }%
}

EDIT: For the alignment of the other equationlabels, try (it works for align of the amsmath-package, but I didn't test any others):

\makeatletter
\def\tagform@#1{\maketag@@@{\marginnote{#1}}}
\makeatother

On the other issue: You might also put the label next to the equation, like in Werner's answer. Other than that I have no idea at the moment.

EDIT2: I just noticed, that \marginnote places two notes invoked on the same line above each other. Because of this I changed the command \inlinelabel:

\newcommand{\inlinelabel}[2][0]{%
    \refstepcounter{equation}\label{#2}%
    \marginnote{%
        \ifnum#1>0%
            \addtocounter{equation}{-#1}%
            \newcount\inlinecount%
            \inlinecount=0%
            \loop\ifnum\inlinecount<#1%
            \setbox0\hbox{\theequation}%
            \hskip\wd0,% This % was missing resulting in more space than 1ex
            \hskip1ex%
            \stepcounter{equation}%
            \advance\inlinecount by 1%
            \repeat%
        \fi%
        \theequation%
    }%
}

Now you can call the \inlinelabel with an optional argument which is the number of prior equations in that line. The result looks like this:

results

And this is the content of \begin{document}...\end{document} in the above image:

$mc^2$\inlinelabel{ineq:mc2}$E=mgh$\inlinelabel[1]{ineq:emgh}$A=\pi
r^2$\inlinelabel[2]{ineq:apir}
\lipsum[1]
also see equation \ref{ineq:mc2}.
\begin{align}
    E = hf
    \label{eqn:ehf}
\end{align}

EDIT3: I've changed the function again (now the commas in the marginnotes are only printed once not multiple times). The new complete MWE shows some limitations of this code.

\documentclass{scrartcl}

\usepackage{lipsum}
\usepackage{marginnote} % better placement than marginpar
\usepackage{amsmath}

\makeatletter
\newcommand{\@commaskip}{%
    \setbox0\hbox{,}%
    \hskip\wd0\relax%
}
\newcommand{\@labelskip}{%
    \setbox0\hbox{\theequation}%
    \hskip\wd0%
    \stepcounter{equation}%
}
\newcommand{\@additionalspace}{\hskip1ex}

\newcommand{\inlinelabel}[2][0]{%
    \refstepcounter{equation}\label{#2}%
    \marginnote{%
        \ifnum#1>0%
            \addtocounter{equation}{-#1}%
            \newcount\@inlinecount%
            \@inlinecount=0%
            \@labelskip%
            \loop\ifnum\@inlinecount<\numexpr#1-1%
                \@commaskip%
                \@labelskip%
                \@additionalspace%
                \advance\@inlinecount by 1%
            \repeat%
            ,\@additionalspace%
        \fi%
        \theequation%
    }%
}

\def\tagform@#1{\maketag@@@{\marginnote{#1}}}
\makeatother

\begin{document}
\section{first section}
$E=mc^2$\inlinelabel{ineq:mc2}
nice
$E=mgh$\inlinelabel[1]{ineq:emgh}
equations
$A=\pi r^2$\inlinelabel[2]{ineq:apir}
being
$U=2\pi r$\inlinelabel[3]{i}
nice
$E=hf$\inlinelabel[4]{ii}. $H=ot$\inlinelabel[5]{iii}.
\lipsum[1]
also see equation \ref{ineq:mc2}.
\begin{align}
    E = hf
    \label{eqn:ehf}
\end{align}
Note that \verb|\eqref| doesn't work with this setup: \eqref{eqn:ehf}. This is
because of the changes made to \verb|\tagform@|
\end{document}
Related Question