10

I am trying to adjust equation range numbering from (1.10)-(1.15) to (1.10-15).

By calling this in the preamble:

\crefrangelabelformat{equation}{(#3#1#4-#5#2#6)}

and then this in the document:

\Crefrange{eq:1-10}{eq:1-15}

I have gotten the range in side the same brackets, i.e. (1.10-1.15), but I am having trouble stripping the section number from the second label in the range. Is there some way to adjust the {eq:1-15} label prior to calling \Crefrange (i.e. changing 1.15 to 15), and then change it back after the call? I need to change it back as in other locations I reference 1.15 individually and so need to retain the section number. Alternately can I strip the section number from the formatted text (#2) passed to \crefrangelabelformat?

Any help appreciated.

  • 1
    Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). – karlkoeller Jul 31 '13 at 5:36
  • 2
    Is there a chance you'll need to cross-reference a range of equations across sections, e.g., something such as (1.15-2.5)? – Mico Jul 31 '13 at 6:08
12

#1 and #2 of \crefrangelabelformat contains the formatted equation number. The following example expands them and stores them in \eq@ref@a and \eq@ref@b. Then both are analyzed, whether the equation numbers have a dot separator. If yes, then the first parts are compared and omitted in \eq@ref@b, if the first parts are equal.

\documentclass{report}
\usepackage{cleveref}

\makeatletter
\crefrangelabelformat{equation}{%
  \protected@edef\eq@ref@a{#1}%
  \protected@edef\eq@ref@b{#2}%
  \expandafter\expandafter\expandafter
  \eq@ref@check\expandafter\eq@ref@a\expandafter.\expandafter\@nil
                           \eq@ref@b.\@nil
  (%
    #3\eq@ref@a#4%
    --%
    #5\eq@ref@b#6%
  )%
}
\def\eq@ref@check#1.#2\@nil#3.#4\@nil{%
  \def\eq@tmp{#2}%
  \ifx\eq@tmp\@empty
  \else
    \def\eq@tmp{#4}%
    \ifx\eq@tmp\@empty
    \else
      \def\eq@tmp@a{#1}%
      \def\eq@tmp@b{#3}%
      \ifx\eq@tmp@a\eq@tmp@b
        \expandafter\def\expandafter\eq@ref@b\expandafter{%
          \eq@strip@dot#4\@nil
        }%
      \fi 
    \fi
  \fi
}
\def\eq@strip@dot#1.\@nil{#1}
\makeatother

\begin{document}
\crefrange{eq:first}{eq:second} and \crefrange{eq:second}{eq:third}
\chapter{First chapter}
\addtocounter{equation}{9}
\begin{equation}
\label{eq:first}
1=1
\end{equation}
\addtocounter{equation}{4}
\begin{equation}
\label{eq:second}
2=2
\end{equation}
\chapter{Second chapter}
\begin{equation}
\label{eq:third}
3=3
\end{equation}
\end{document}

Result

  • You may want to alert readers that this method assumes that a common prefix is terminated by a single . ("dot"). Taking your example and adding the instructions \usepackage{amsmath} \numberwithin{equation}{section} to the preamble (prior to loading cleveref, naturally) will create a cross-reference of the form eqs. (1.0.10--0.15) rather than eqs. (1.0.10-15). Put differently, 1. rather than 1.0. is recognized as the common prefix. Can your method be generalized to handle the case of equation numbers that are numbered by both chapter and section (or some similar scheme)? – Mico Nov 17 '13 at 15:07
  • 1
    @Mico: Thanks, for the hint. Yes, the code uses the first dot as separator. Also more complex numbers (e.g. several dots) can be parsed with TeX. There are packages that help in the task of parsing, e.g. package xstring with \StrBefore, \StrBehind, \StrCut and other macros. – Heiko Oberdiek Nov 18 '13 at 15:40
  • Many thanks for this follow-up. Real quick: In tex.stackexchange.com/a/145356/5001, Toby Cubitt (the creator of cleveref) has provided a solution to a related question, on how to suppress the "equation prefix" in the second part of a cross-reference to a bunch of subequations. (E.g., if five equations are numbered 1a to 1e, the cross-reference will come out as (1a--e).) Toby's solution can, remarkably, handle the case of prefixes containing one or more "dots" as well. – Mico Nov 18 '13 at 19:21
  • 1
    @Mico It's not particularly "remarkable", just straightforward TeX :) Heiko's answer was helpful in thinking through what to strip in general. My \crefstripprefix macro strips a common prefix, but only up to the final consecutive run of characters of the same 'type' (letters or digits). I think this will do the right thing in most cases, e.g. subequations, or subsections with arbitrary separators. Of course, you can still cook up unusual label formats that will break it if you try hard enough. I'll add the \crefstripprefix command to the next cleveref release, along with some examples. – Toby Cubitt Nov 24 '13 at 13:12

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.