2

Suppose that I have an equation

a = b + c   (1.5)

in the chapter 2. Within the chapter 2 I would like to refer it as "equation (1.5)". But from other chapters I want to refer this equation differently, like "equation (2;1.5)".

Usually I write equations in the form

\begin{equation}
\label{1.5}
a = b + c.
\end{equation}

and at the point of reference write "\eqref{1.5}". But I cannot mark the same equation with two labels in order to choose the appropriate one in different references. How can I achieve that?

Ilia
  • 135
  • I would rather suggest a) a better labeling scheme (other than 1.5) and b) using something that keeps the chapter information as well! –  Feb 10 '17 at 17:25
  • welcome to tex.sx. does this answer your question: Smartly omit chapter number in equation numbering? – barbara beeton Feb 10 '17 at 17:33
  • 1
    it is best to never use numbers in \label as it is confusing for anyone looking at the source when those numbers are different from the printed number. – David Carlisle Feb 10 '17 at 17:34
  • @Christian Hupfer, @David Carlisle, Yes, using numbers in \label is not a good solution, and I need time to time to perform a context replacement in order to keep those numbers equal to the printed ones. But when there are too many equations it becomes impossible to invent name for each one, and the only identity that I'm capable to remember about an equation is its number in specific section inside specific chapter. – Ilia Feb 10 '17 at 19:19
  • Well then, use section and chapter names in the label as well. – Johannes_B Feb 11 '17 at 16:37
  • If you are doing the labeling to match the printed output, make it with the referencing as well. In other words, keep yourself from labeling by hand and do everything by hand. – Johannes_B Feb 11 '17 at 16:37

2 Answers2

2

Don't use such label names like 1.5 they are error-prone because the equation number changes (this is exactly the point of having easy to remember names instead of numbers)

I am using zref to store the chapter as well and extract it afterwards with \moreref (as a wrapper to \zref@extract)

\eqref does not work with zref-labels, so either redefine \eqref or provide a wrapper as well.

\documentclass{book}


\usepackage{amsmath}

\numberwithin{equation}{section}
\renewcommand{\theequation}{\arabic{section}.\arabic{equation}}
\usepackage[user]{zref}


\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@addprop{main}{chapter}

\newcommand{\moreref}[1]{%
  \zref@ifrefundefined{#1}{%
  }{%
    (equation \zref@extract{#1}{chapter},\zref{#1})%
  }%
}

\newcommand{\zeqref}[1]{%
  equation \zref{#1}%
}

\makeatother
\begin{document}


\chapter{Number One}
\chapter{Number Two}


\section{A section}
\begin{equation}
\zlabel{someequation}
a = b + c.
\end{equation}

In \moreref{someequation} or \zeqref{someequation}

\end{document}

Update with an ugly hack

Explanation: The \morelabel command sets two labels, a fake one and the real one.

Since equation (or rather: amsmath does not allow two labels inside of equation and redefines \label I used the outside label definition for the fake label, which is stored in \@currentlabel.

The \longref command extracts the more::#1 label then.

\documentclass{book}


\usepackage{amsmath}

\numberwithin{equation}{section}



\renewcommand{\theequation}{\arabic{section}.\arabic{equation}}

\makeatletter
\let\latex@@label\label

\newcommand{\morelabelformat}{%
  (equation \thechapter,\theequation)%
}
\newcommand{\morelabel}[1]{%
  \let\@oldcurrentlabel\@currentlabel%
  \edef\@currentlabel{\morelabelformat}
  \latex@@label{more::#1}%
  \let\@currentlabel\@oldcurrentlabel
  \label{#1}%
}

\newcommand{\longref}[1]{%
  \@ifundefined{r@more::#1}{%
    % Do nothing!
  }{%
    \ref{more::#1}%
  }%
}
\makeatother

\begin{document}
\chapter{Number One}
\chapter{Number Two}


\section{A section}
\begin{equation}
\morelabel{someequation}
a = b + c.
\end{equation}

In \longref{someequation} or \eqref{someequation}

\end{document}

enter image description here

  • Oh, your solution is too complicated for me and requires time to understand. I'm not so familiar with Latex... – Ilia Feb 10 '17 at 18:51
  • 1
    It is a working solution for a complicated request. Otherwise you have to change your strategy –  Feb 10 '17 at 18:54
  • 1
    @Ilia - You seem to be under the impression that there must be simple solutions to not-so-simple requests. Why is that? – Mico Feb 10 '17 at 19:17
  • @Mico: Perhaps I should have suggested \renewcommand{\theequation}{\thechapter.\arabic{section}.\arabic{equation}} and some messing around with \p@equation ;-) –  Feb 10 '17 at 19:18
  • @Mico, Friends, I'm mathematician and not Latex specialist. I do not want to become a сar serviceman in order to drive a car... – Ilia Feb 10 '17 at 19:41
  • 1
    @ilia - While on the subject of car-related figures of speech: Christian has handed you a key to a shiny new Porsche (or maybe a Mercedes -- both are made in the state he lives in...) Why not just say "thank you" and drive off into the sunset? :-) – Mico Feb 10 '17 at 21:06
  • 1
    @Mico: :D :D :D Don't forget Audi ;-) –  Feb 10 '17 at 21:10
  • 1
    @Ilia If you want a feature a car does not provide you have either built yourself or buy another car that has this feature ;-) –  Feb 10 '17 at 21:11
  • @Christian, @Mico, Unfortunately, all these cars are too expensive for me ;-). I would like something simple... Say, a command \longref which takes chapter number (e.g. "2") as an additional parameter and prefixes regular reference with "2;". Because such long references are rare, there is no sense to provide the full-blown implementation for that. – Ilia Feb 11 '17 at 15:36
  • 1
    @Ilia: This means you want add the chapter number manually, having to remember it each time from chapter which equation is? –  Feb 11 '17 at 15:54
  • @Christian, Exactly. Once more, it is rare situation. – Ilia Feb 11 '17 at 15:58
  • @Ilia: See the update at the end -- it's not really shorter and I don't recommend it. –  Feb 11 '17 at 16:03
  • 2
    @Ilia Your question is not How to drive a car, your question is of an engineering type. You want a serious and complicated modification to the way your car works. In real live, you pay an engineer money to develop the modification and a mechanic money to install the modification. Here, you get help completely free of charge. The helpers pay your charge by investing their time. Remember, time is money ;-) – Johannes_B Feb 11 '17 at 16:50
  • @Johannes_B, Your comment made me feel guilty. Believe me, when I asked my question I was sure it is quite simple. Even now, it seems to me surprising how the apparently elementary desire turns to be "serious and complicated modification". But if you tell me it is so, I will use "manual" numbering in the case of "long" references. As Knuth was doing that. Not a bad company. – Ilia Feb 11 '17 at 19:43
1

I am sorry to say it, but the simplest solution for the situation as depicted in the question is: Do not just the labels by hand, do it for the references as well. In other words: Do not use the label-ref-mechanism at all.

Knuth has numbered stuff by hand as well.

Johannes_B
  • 24,235
  • 10
  • 93
  • 248