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 want to compress numbers of sections in a reference, but without repeating the chapter number, while maintaining full functionality of of hyperref and \ref.

\documentclass{book}

\usepackage{hyperref}
\usepackage{bookmark}

\begin{document}

\chapter{intro}
I want the hyperref functionallity and automated references of 
\ref{sec:chap3sec3}--\ref{sec:chap3sec5}; 
but the look of 3.3--5. 
\ref{sec:chap3sec3}--\hyperref[sec:chap3sec5]{5} does half of what I want, 
but I don't know how to get the section number without chapter number.

\chapter{two}
\chapter{three}
\section{chap3sec1}
\section{chap3sec2}
\section{chap3sec3}\label{sec:chap3sec3}
\section{chap3sec4}
\section{chap3sec5}\label{sec:chap3sec5}

\end{document}

How can I do this?

share|improve this question
    
Would using cleveref be an option? –  Gonzalo Medina May 8 '12 at 14:22
    
@GonzaloMedina: \crefrange{eq1}{eq5} is typeset as Eqs.~(1.1) to~(1.5) according to the manual; \crefrangelabelformat allows to change the to~, but not the individual labels. Unless I missed something in the manual cleveref is no help. –  Markus Schmassmann May 8 '12 at 14:36
    
I second Gonzalo's suggestion that you consider using the cleveref package and its \crefrange command. All sections in your document are numbered with the respective chapter number prefixed to the the section numbers, right? If that's the case, I think you're risking creating a lot of confusion among your readers if you omit the chapter number prefix from some of the section numbers used in a cross-reference. "Sections 3.3--3.5" is unambiguous, though not all that elegant. Do you want your readers to puzzle, however briefly, over whether "Sections 3.3--5" means "Section 3.3 to Chapter 5"? –  Mico May 8 '12 at 15:16
    
@Mico: Given the context I use it in (outline of the MSc thesis, effectively an inline list) "3.3--5" is clear, so I would prefer it as asked. –  Markus Schmassmann May 8 '12 at 15:32
    
@MarkusSchmassmann -- Both clarity and ambiguity are in the eye of the beholder, right? I must confess that I don't know how to implement the formatting requirement you've set out -- at least not within the framework of the cleveref package. –  Mico May 8 '12 at 16:35
add comment

1 Answer

LaTeX only stores the complete reference number. To get the section part of the number, the reference first needs to be extracted. Then the chapter part before the first dot can be stripped:

\usepackage{refcount}

\makeatletter
\newcommand*{\stripref}[1]{%
  \refused{#1}%
  \begingroup
    \edef\x{\getrefbykeydefault{#1}{}{\noexpand\textbf{??}}}%
    \hyperref[{#1}]{%
      \expandafter\stripref@aux\x.\@nil
    }%
  \endgroup
}
\def\stripref@aux#1.#2\@nil{%
  \ifx\\#2\\%
    #1%
  \else
    \stripref@dot#2\@nil
  \fi
}
\def\stripref@dot#1.\@nil{#1}
\makeatother

Usage: \ref{sec:chap3sec3}--\stripref{sec:chap3sec5}

share|improve this answer
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.