# How to set bookmark anchor so \section- and \section*-bookmark behaves the same

If I make a \section and \section* bookmark, the anchor points to the two sections in a different place. A small vertical space is added to \section* (see .png-files). How can I make sure the anchor is placed at the same place?

I need this solution since I would like to make a List of Appendices, where the entries only need to be showned in LoA and not ToC, but I still need the section numbers in the text and LoA.

\documentclass{book}

\usepackage{bookmark}
\hypersetup{bookmarksnumbered=true}

\begin{document}
\chapter{Chapter one}

References here:

ref: \ref{chap2} (chapter)

ref: \ref{chap2sec1} (section)

ref: \ref{chap2sec2} (star-section)

\chapter{Chapter two}\label{chap2}

...(contents of chapter 2)...

\section{Sec one}\label{chap2sec1}

...(contents of chapter 2 - section 1)...

\refstepcounter{section}

...(contents of chapter 2 - section 2)...

\end{document}


EDIT (1): I have added a red box to show the wrong bookmark anchor for \section*. The spacing between sections are correct it's only the anchor settings that's wrong.

• Could you please draw over the images which is that added space? – U.Martinez-Corral Sep 29 '14 at 13:45
• I have added a red box now. Please see edit comments. Thanks. – TobiasDK Sep 30 '14 at 7:03

You can adjust the way \section works and make \section* act the same way \section does, only with some minor modifications:

\documentclass{book}

\usepackage{bookmark}
\hypersetup{bookmarksnumbered=true}

\usepackage{xparse}
\makeatletter
\renewcommand{\@seccntformat}[1]{\csname the#1\endcsname\space}% Just for this example
\let\oldsection\section
\RenewDocumentCommand{\section}{s o m}{%
\IfBooleanTF{#1}
{{% \section*
\renewcommand{\refstepcounter}[1]{\phantomsection}% Gobble counter stepping
\renewcommand{\@seccntformat}[1]{}% Gobble section number formatting
\oldsection{#3}% \section*[.]{..}
}}{% \section
\IfNoValueTF{#2}
{\oldsection{#3}}% \section{..}
{\oldsection[#2]{#3}}% \section[.]{..}
}%
}
\makeatother

\begin{document}
\chapter{Chapter one}

References here:

ref: \ref{chap2} (chapter)

ref: \ref{chap2sec1} (section)

ref: \ref{chap2sec2} (star-section)

\chapter{Chapter two}\label{chap2}

...(contents of chapter 2)...

\section{Sec one}\label{chap2sec1}

...(contents of chapter 2 - section 1)...

\stepcounter{section}%
\section*{\thesection{} Sec two}\label{chap2sec2}

...(contents of chapter 2 - section 2)...

\end{document}


xparse is used to easily capture the arguments typically managed by \section (starred-version, optional and mandatory arguments).

When \section* is called, we update three things which are typical for \section*:

1. The counter stepping \refstepcounter is made into a no-op;

2. The section counter formatting macro \@seccntformat is converted into a no-op;

3. The contents-related writing is updated to be specific to the input, thereby avoiding a possible \numberline printing.

Since \section and \section* now use the same procedure for printing the header (namely \@sect, internally), the output hyperlinks jump to the same vertical location.

Your example is slightly contrived as it includes a manual \refstepcounter as part of Section 2.2. However, I don't see any problems with the hyperlink jumps using the updated version of \section defined above.