i'm looking for a hook or package that would print references to equations differently depending on whether a reference goes to same chapter where the equation is located or to a different chapter.
i mean the following situation.
Suppose that equations are numbered consequently starting from number 1 in each chapter, i.e. (1), (2), (3)... I want a reference to an equation 13 in a chapter 2 typed just as (13) if it is refrenced from same chapter 2 but as (2.13) if same equation is mentioned in any other chapter. Can one help me?
Preferebly that this package should be consistent with hyperref package.
Document Classes ⇒ Q: smart equation numbering
NEW: TikZ book now 40% off at Amazon.com for a short time.
Q: smart equation numbering
Try the following example:
Remarks:
Code: Select all
\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage{amsmath}
\usepackage[colorlinks,linkcolor=blue]{hyperref}
\renewcommand{\theequation}{\arabic{equation}}
% ------ Main code ------
\makeatletter
\@ifpackageloaded{amsmath}{}{\def\tagform@#1{(#1)}}
\@ifpackageloaded{hyperref}%
{\def\chapterref#1{\expandafter\real@setref\csname rChapter@#1\endcsname\@firstofone{#1}}
\def\ceqref#1{\edef\@RefEqa{\thechapter\null}\edef\@RefEqb{\chapterref{#1}}%
\ifx\@RefEqa\@RefEqb%
\textup{\tagform@{\ref{#1}}}%
\else%
\ifHy@colorlinks%
\textup{\tagform@{\textcolor{\@linkcolor}{\chapterref{#1}.}\ref{#1}}}%
\else%
\textup{\tagform@{\chapterref{#1}.\ref{#1}}}
\fi%
\fi}}%
{\def\chapterref#1{\expandafter\@setref\csname rChapter@#1\endcsname\@firstofone{#1}}
\def\ceqref#1{\edef\@RefEqa{\thechapter\null}\edef\@RefEqb{\chapterref{#1}}%
\ifx\@RefEqa\@RefEqb%
\textup{\tagform@{\ref{#1}}}%
\else%
\textup{\tagform@{\chapterref{#1}.\ref{#1}}}%
\fi}}
\AtBeginDocument{
\def\clabel#1{\label{#1}\@bsphack%
\protected@write\@auxout{}{\string\newclabel{#1}{\thechapter}}%
\@esphack}
\immediate\write\@auxout{%
\gdef\string\newclabel{\string\@newl@bel{\string rChapter}}}}
\makeatother
% ------------------------
\begin{document}
\chapter{This is the first chapter}
\section{First section of the first chapter}
\lipsum[1]
\begin{equation} \clabel{eq:c1e1}
a+b=c.
\end{equation}
\lipsum[2]
\begin{align}
a_1+a_2&=a_3, \clabel{eq:c1e2} \\
b_1+b_2&=b_3. \clabel{eq:c1e3}
\end{align}
\section{Second section of the first chapter}
\lipsum[3]
\begin{multline}\clabel{eq:c1e4}
a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p \\
=q+r+s+t+u+v+w+x+y+z.
\end{multline}
\chapter{This is the second chapter}
\section{First section of the second chapter}
\lipsum[4]
\begin{equation} \clabel{eq:c2e1}
\alpha+\beta=\gamma.
\end{equation}
\lipsum[5]
\begin{equation} \clabel{eq:c2e2}
a+b=c
\end{equation}
\section{Tests}
\newcommand{\printlabeldata}[1]{Label \texttt{#1}:\quad Equation \eqref{#1}
in Chapter \chapterref{#1} $\Longrightarrow$ \ceqref{#1}}
\noindent
\printlabeldata{eq:c1e1}\\
\printlabeldata{eq:c1e2}\\
\printlabeldata{eq:c1e3}\\
\printlabeldata{eq:c1e4}\\
\printlabeldata{eq:c2e1}\\
\printlabeldata{eq:c2e2}
\end{document}
- Put the code marked as "Main code" in the preamble of your document. If you use hyperrref or amsmath, load them before that code, just as in the example.
- In every formula you want to cite, write \clabel instead of \label. To cite it, use \ceqref instead of \ref or \eqref.
- When hyperref is loaded, only the equation number links to the corresponding formula (not the chapter number, if any). To see it even more clearly, remove the colorlinks option (or write colorlinks=false).
- The code writes in the aux file a line (which starts by \newclabel) to "remember" the chapter number corresponding to a given label. This is done by the \clabel command. In the next LaTeX run, this information is retrieved by \ceqref.
- As a by-product, you also have a new command called \chapterref. It gives the chapter number associated to a label.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.