Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
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
show 1 more comment

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
  \ensuremath{\stackrel{(\roman{numrel})}{#1}}% Print counter + relation
}
\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} 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 \stackrel 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. 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

\newcounter{numrel}% Counter for numering relations
\renewcommand{\thenumrel}{\roman{numrel}}% Counter numrel uses lowercase roman numerals
\let\textlabel\label% Use text \label rather than math \label
\newcommand{\numrel}[2]{% Relation numbering
  \begingroup%
  \refstepcounter{numrel}% Increment numrel counter and create correct reference hook
  \textlabel{#2}\endgroup% Label numrel counter
  \ensuremath{\stackrel{(\thenumrel)}{#1}}% Print counter + relation
}
\AfterEndEnvironment{align*}{\setcounter{numrel}{0}}% Resets numrel 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

share|improve this answer
    
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
    
I've updated my answer with the capability to reference the relations using labels. It also automatically resets the counter, so you don't need to manually reset it. –  Werner Sep 25 '11 at 16:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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