6

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.

  • 5
    It is not a good idea to put eq numbers in the text as your reader will probably have a hard time keeping track of them. Remember you can refer to page number to any label. – daleif Apr 2 '17 at 16:59
  • 3
    welcome to tex.sx. although the equations you mention are small, think of your reader -- if the reference isn't quite close by, it will be very hard to find what is being referred to if the number is buried in the text. – barbara beeton Apr 2 '17 at 16:59
  • 1
    Are you OK with loading the hyperref package, or does something in your document's setup indicate that hyperref must not be used? Please advise. – Mico Apr 2 '17 at 17:00
  • I do indeed realize that my idea is not the most preferable option. As I said other solutions are very welcome. I just don't want to clutter my text with small equations left right and center. @Mico, the hyperref package may be used. (it is already as a matter of fact) – gertian Apr 2 '17 at 17:18
  • 1
    How about adding the reference into a \marginpar? This way the reader can spot them easily (at least the numbering). – Skillmon Apr 2 '17 at 17:35
10

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}
  • Hey thank you for this great suggestion, I still have some questions though: Is there a way to modify the usual labels of equations such that they are aligned with this new type of equation ? And if I may ask some more advice, what would you suggest in case of two equations on the same line. How do I clarify which one I refer to ? – gertian Apr 2 '17 at 18:03
  • @gertian see my edit for this. – Skillmon Apr 2 '17 at 18:12
  • @gertian if you're still interested in this, I made an edit improving the code (not the behavior, sadly) and showing some limitations. – Skillmon Apr 5 '17 at 15:11
  • Thank you very much ! Sorry for my late respond I was on a field trip and failed to find WiFi ! – gertian Apr 5 '17 at 17:46
11

enter image description here

\documentclass{article}

\usepackage{amsmath}
\newcommand{\inlineeqnum}{\refstepcounter{equation}~~\mbox{(\theequation)}}

\begin{document}

Some text $E = mc^2 \inlineeqnum\label{eqn:einstein}$ some more text.

Some text \eqref{eqn:einstein} some more text.

\end{document}

Consider the possible confusion when viewing the equation number as a possible element of the equation.

  • Thanks for your solution. It is exactly what I asked for. But after seeing it in person it turns out that I like Skillmons suggestion more. – gertian Apr 5 '17 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.