2

What's the difference between \refstepcounter{} and \phantomsection in anchoring at specific locations?

I apologize if I am mistaken but as far as I know they are both used in setting anchors to make hyperlinks link correctly when they sometimes don't link properly so I was wondering when I should use one or the other.

I already tried looking and searching everywhere (sorry if this is a duplicate question) but I have yet to find a comprehensive answer that explains and differentiates the two commands and when they should be used.

As an example, I want to create a new chapter macro that basically goes like this:

\newcommand{\newchapter}[1]{%
    \singlespacing
    \begingroup
    \clearpage
    \let\clearpage\relax
    \refstepcounter{chapter}
    \chapter*{#1}
    \label{chap:#1}
    \addcontentsline{toc}{chapter}{#1}
    \endgroup
    \doublespacing
}

or

\newcommand{\newchapter}[1]{%
    \singlespacing
    \begingroup
    \clearpage
    \let\clearpage\relax
    \phantomsection
    \chapter*{#1}
    \label{chap:#1}
    \addcontentsline{toc}{chapter}{#1}
    \endgroup
    \doublespacing
}

They both seemingly appear to have the same result and they both link properly to each chapter whenever the macro \newchapter is called. Thanks in advance!

3

Both \refstepcounter and \phantomsection do

\hyper@anchorstart{\@currentHref}\hyper@anchorend}

which sets an anchor. The latter is simpler, because it doesn't involve raising (or lowering) the anchor and so is good to be issued in vertical mode.

2
  • 1
    Ah I see, can you please elaborate on what raising/lowering the anchor means? May 15 at 5:04
  • 1
    @FarrelAhmed When the anchor is needed in an equation, for instance, it is raised so that the whole equation is shown at a click on the hyperlink. The amount of raising or lowering depends on the context, but for \phantomsection it's not needed.
    – egreg
    May 15 at 15:06

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.