11

I have a long chain of inequalities and I would like to add some explanation for some of the steps under those inequalities. I have seen people do this like \stackrel{(i)}{\leq}. This is what I would like but I would like to have a counter that does this for me, so (i), (ii), and so on with an automatic increment so that I will not have to change everything if I add a line.

The biggest issue that I can think of and which I don't know how to solve is that that counter has to be reset when I use it again in a new equation (or something similar) environment. Any suggestions?

3

Here's a try:

\documentclass{article}
\usepackage{amsmath}
\newcounter{hints}
\renewcommand{\thehints}{\roman{hints}}
\newcommand{\hintedrel}[2][]{%
  \refstepcounter{hints}%
  \if\relax\detokenize{#1}\relax\else\label{#1}\fi
  \mathrel{\overset{\mathrm{(\thehints)}}{\vphantom{\le}{#2}}}%
}
\newcommand{\restarthintedrel}{\setcounter{hints}{0}}

\begin{document}
$a\hintedrel[lab1]{\le}b\hintedrel[lab2]{<}c$

(\ref{lab1}): because it's so.

(\ref{lab2}): I tell you it is.

$\restarthintedrel a\hintedrel[lab3]{\le}b\hintedrel[lab4]{<}c$

(\ref{lab3}): because it's so.

(\ref{lab4}): I tell you it is.
\end{document}

The \hintedrel command has an optional argument, a label for referring later to the inequality.

Before a new chain of hinted inequalities you can write \restarthintedrel to restart from 1.

Different implementation

\documentclass{article}
\usepackage{amsmath,etoolbox}
\AfterEndEnvironment{equation}{\restarthintedrel}
\AfterEndEnvironment{align}{\restarthintedrel}
\newcounter{hints}
\renewcommand{\thehints}{\roman{hints}}
\newcommand{\hintedrel}[2][]{%
  \stepcounter{hints}%
  \if\relax\detokenize{#1}\relax\else\csxdef{hint@#1}{\thehints}\fi
  \mathrel{\overset{\textrm{(\thehints)}}{\vphantom{\le}{#2}}}%
}
\newcommand{\restarthintedrel}{\setcounter{hints}{0}}
\newcommand{\hintref}[1]{\csuse{hint@#1}}

\begin{document}
\begin{equation}
a\hintedrel[lab1]{\le}b\hintedrel[lab2]{<}c\overset{*}{\le}d
\end{equation}

(\hintref{lab1}): because it's so.

(\hintref{lab2}): I tell you it is.

\begin{align}
a&\hintedrel[lab3]{\le}b\\
&\hintedrel[lab4]{<}c
\end{align}

(\hintref{lab3}): because it's so.

(\hintref{lab4}): I tell you it is.
\end{document}

This assumes that the hints will be referred to only after they are defined (with the standard \label-\ref mechanism amsmath would raise an error). You have to add similar \AfterEndEnvironment for each math environment you use.

  • Thanks! I've tested it and it seems to look good, but why is it not possible to use \stackrel? If I look carefully I think that (i) is not really centered above the \le. Furthermore, would it be possible to automatically reset the counter or would that become too cumbersome? – Jonas Teuwen Sep 25 '11 at 15:54
  • I've added a missing pair of braces that will center the (i). The problem of resetting the counter is: when do you want to reset it? Say, for example, that a chain is split into two for typographical reasons: the numbering should continue. – egreg Sep 25 '11 at 16:02
  • I don't think that would be a problem to reset the counter if I split the chain into two because I would have explained already then what (i) and (ii) and so on mean. – Jonas Teuwen Sep 25 '11 at 16:06
  • See edited answer – egreg Sep 25 '11 at 16:19
  • Thanks. This is exactly what I wanted. One more question: What does \csuse do? I cannot find it using Google. – Jonas Teuwen Sep 25 '11 at 16:27
10

This might be what you're after, using a counter that increments at every use and resets at the start of every \section in your document. This can be modified, of course:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{chngcntr}% http://ctan.org/pkg/chngcntr

\newcounter{numrel}% Counter for numering relations
\counterwithin*{numrel}{section}% Counter resets at every section
\newcommand{\numrel}[1]{% Relation numbering
  \stepcounter{numrel}% Increment numrel counter
  \overset{(\roman{numrel})}{#1}% Print counter + relation
}
\begin{document}
\section{First section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\overset{(i)}{\leq} cx^3+dx^2+ex+f \\
       &\overset{(ii)}{<} gx^4+hx^3+ix^2+jx+k  
\end{align*}
\section{Second section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq} cx^3+dx^2+ex+f \\
       &\numrel{<} gx^4+hx^3+ix^2+jx+k  
\end{align*}
\end{document}

Numbered relation using \stackrel

In the example above, the first section uses \overset while the second uses the newly defined \numrel command, to show the similarities. It would also be possible to modify the code to accommodate for using references so that your document text will change according to the change of adding/removing a line in your equation. The chngcntr package was used for more flexibility in formatting the counter resetting.


In order to accommodate the possibility to reference the counter within your text, here's a modified minimal example that uses the etoolbox package to reset the counter at the end of the align* environment, as well as some amsmath counter manipulation using Multiply defined labels using hyperref. It uses two counters, the global counter to produce unique reference hooks (important for hyperref), and a local counter which is actually displayed and reset for each equation. The \numrel{<rel>}{<lab>} now takes two mandatory arguments. The first is the relation <rel> while the second is the label <lab> that may be referenced elsewhere:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{hyperref}% https://ctan.org/pkg/hyperref

\newcounter{numrellocal}% Local counter for numering relations
\renewcommand{\thenumrellocal}{\roman{numrellocal}}% Counter numrellocal uses lowercase roman numerals
\newcounter{numrelglobal}% Global counter for numering relations
\renewcommand{\thenumrelglobal}{\roman{numrellocal}}% Counter numrelglobal uses lowercase roman numerals, but results in value from numrellocal

\makeatletter
\newcommand{\numrel}[2]{% Relation numbering
  \stepcounter{numrellocal}% Increment local counter
  \refstepcounter{numrelglobal}% Increment global counter and create correct reference hook, with label-text from local counter
  \ltx@label{#2}% Label numrel counter
  \overset{(\thenumrellocal)}{#1}% Print counter + relation
}
\makeatother
\AfterEndEnvironment{align*}{\setcounter{numrellocal}{0}}% Resets numrellocal at the end of align*

\begin{document}
\section{First section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\stackrel{(i)}{\leq} cx^3+dx^2+ex+f \\
       &\stackrel{(ii)}{<} gx^4+hx^3+ix^2+jx+k  
\end{align*}
\section{Second section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel1} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel2} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel1}), you will notice it is different from (\ref{rel2}).
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel3} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel4} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel3}), you will notice it is different from (\ref{rel4}).

\end{document}

Numbered relations with referencing capability

  • Excellent! Is there a general "name" to use for equation environment like equation, align and so on or should I write a line for each environment that resets the counter after that environment? Also, how do I refer to (i) and so on? There are not really labels defined for those lines. – Jonas Teuwen Sep 25 '11 at 16:03
  • the "less than" in the last line isn't neatly aligned with the other two relations. perhaps this could be fixed by applying \clap to the reference label; the label itself isn't wide enough to cause a problem. – barbara beeton Nov 21 '15 at 16:00
  • I took the liberty of fixing the second version so it becomes compatible with hyperref. – egreg Nov 21 '15 at 16:46
  • @Werner I also changed \stackrel into \overset and removed the nonsense \ensuremath. It should be \overset{\textup{(\thenumrel)}}{#1}, probably. – egreg Nov 21 '15 at 16:52
  • @egreg : I'm not sure your edit fixed all issues with hyperref. That is, the reference hooks would be `numrel.i', where i is the value of the current counter, and when it is reset it produces duplicate hooks, which means the hyperlinks always link to the first instance. I put in an edit to fix this, by using a global counter to index the hooks, yet which when displayed yields the desired local counter. – miforbes Sep 4 '16 at 5:48

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.