2

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}
\section*{\thesection{}{\quad}Sec two}\label{chap2sec2}
\addcontentsline{toc}{section}{\protect\numberline{\thesection}Sec two}

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

\end{document}

Normal section bookmark (\section) Star-section bookmark (\section*)

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
0

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
\let\oldaddcontentsline\addcontentsline
\RenewDocumentCommand{\section}{s o m}{%
  \IfBooleanTF{#1}
    {{% \section*
      \renewcommand{\refstepcounter}[1]{\phantomsection}% Gobble counter stepping
      \renewcommand{\@seccntformat}[1]{}% Gobble section number formatting
      \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{toc}{section}{#3}}% Next entry _will_ be a section
      \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.

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.