TransWikia.com

No section numbers, but still have PDF bookmarks with hyperref

TeX - LaTeX Asked on June 26, 2021

I have a document that has many sections. I don’t want section numbers to show up in the document, but I do want the sections to show up automagically as bookmarks in the PDF using the hyperref package.

I know I can use the section* command to eliminate section numbers in the document, but this also removes the bookmark. Is there some sort of compromise?

7 Answers

You can just suppress the appearance of the section number:

documentclass[a4paper]{article}
usepackage{hyperref}
usepackage{bookmark}
makeatletter
renewcommand@seccntformat[1]{}
makeatother

begin{document}
section{A}
a
newpage
section{B}
b
end{document}

The macro @seccntformat is responsible for printing the section number; by redefining it to "do nothing" the number is not printed, but hyperref is able to create the bookmark correctly.

The bookmark package is a good add-on, as it avoids some weaknesses of the original implementation of bookmark creation.

Correct answer by egreg on June 26, 2021

hyperref provides bookmarking functionality by means of

pdfbookmark[<level>]{<text>}{<name>}

or

currentpdfbookmark{<text>}{<name>}

in addition to others (see the hyperref documentation). Here is a minimal example:

enter image description here

documentclass{article}
usepackage{lipsum}% http://ctan.org/pkg/lipsum
usepackage{hyperref}% http://ctan.org/pkg/hyperref
begin{document}
section*{First section} pdfbookmark{First section}{sec:first} lipsum[1]
section*{Second section} pdfbookmark{Second section}{sec:second} lipsum[2]
section*{Third section} pdfbookmark{Third section}{sec:third} lipsum[3]
section*{Last section} pdfbookmark{Last section}{sec:fourth} lipsum[4]
end{document}

It would be possible to make this form part of the section command section.

If you're only after bookmarks, and not so much concerned with internal document hyper references, use the bookmark package instead.

Answered by Werner on June 26, 2021

It is also possible to turn off heading numbering completely. Since you didn't explicitly say that you only want unnumbered sections, this might be an alternative.

documentclass[11pt,english]{article}
usepackage[T1]{fontenc}
usepackage{babel}
usepackage{blindtext}
usepackage{hyperref}

setcounter{secnumdepth}{0}

begin{document}
  blinddocument
end{document}

This way you can use the known section command. Again, note that this would turn off numbering of all heading levels.


enter image description here

Answered by Thorsten Donig on June 26, 2021

I've provided a way to make LaTeX essentially treat unnumbered sections in the same way as numbered sections (but pretend that they are really deep so that LaTeX won't number them), hopefully without breaking other things. This is same approach used in classes like amsart.

This means that one can have numbered and unnumbered sections in the same document, and all will appear in the table of contents and bookmarks as usual (the latter if hyperref is loaded, optional), with hyperlinks pointing to the right places, without any effort except loading the package! See the comments in the code below for more details. Note that this doesn't really need to be in a package, I just thought it might be more convenient.

However, note that the table of contents gets an entry in the table of contents, which may not be desirable! My example below illustrates how to suppress that also.

% save this as fix-unnumbered-sections.sty
ProvidesPackage{fix-unnumbered-sections}

% This package is available at http://tex.stackexchange.com/q/33696/
% It patches the standard classes so that they treat unnumbered and numbered sections equally! (At least, I've tested it with the article class.)
% As a result, even the unnumbered sections gets entries in the TOC and, when hyperref is loaded, they also get bookmarks as per the default hyperref setting for bookmarks.
% Unnumbered sections can also have short titles for TOC and bookmark purposes, just like numbered sections.
%
% It works by pretending the unnumbered sections are deeper than c@secnumdepth (in fact, just by assuming that they are 1000 (@m) levels deep).
% I believe there are no side effects to this...

% We just pretend that @ssect, which LaTeX uses to treat unnumbered [sub*]sections differently to (maybe) numbered ones (if they are no deeper than c@secnumdepth levels), doesn't exist.
% Instead, we always use @sect, which LaTeX uses for numbered sections, and sections which would otherwise be numbered if they weren't deeper than c@secnumdepth levels.


RequirePackage{etoolbox}
patchcmd{@startsection}{@ssect{#3}{#4}{#5}{#6}}{@dblarg{@sect{#1}{@m}{#3}{#4}{#5}{#6}}}{}{PackageError{fix-unnumbered-sections}{Unable to patch string@startsection; are you using a non-standard document class?}@ehd}

Here is a test file to explore the behaviour (needs two runs):

documentclass[a4paper]{article}
usepackage[bookmarksopen=true,bookmarksnumbered=true]{hyperref}
usepackage{fix-unnumbered-sections}
usepackage{lipsum}

begin{document}
bgroupmakeatletterletaddtocontents@gobbletwo
tableofcontentsegroup


section[First section]{This section is numbered}
lipsum[1]
subsection*[Unnumbered subsection]{This subsection is not numbered}
lipsum[2]
subsection[Numbered subsection]{But this one is}
lipsum[3]
newpage
section*{This section is not numbered}
lipsum[4]
subsection*{And neither are its descendents}
lipsum[5]
end{document}

Output from the sample code illustrating bookmarks and table of contents

Answered by cyberSingularity on June 26, 2021

Here is a very short solution that does not require any changes to the preamble, provided you are loading hyperref:

section*{Acknowledgements}% The * makes this section unnumbered. 
addcontentsline{toc}{section}{Acknowledgements}% This command makes the unnumbered section also appear in the pdf bookmarks.

Note: you do not need to call a table of contents in the beginning of the file for this to work.

Answered by Tom Fiore on June 26, 2021

By using the bookmarks package, it is possible as follows:

section*{Acknowledgements}
hypertarget{Acknowledgements}{}
bookmark[dest=Acknowledgements]{Acknowledgements}

Credits go to https://tex.stackexchange.com/a/11965/9075 for hypertarget.

Answered by koppor on June 26, 2021

Can't upvote but I want to point to the comment from @cgnieder (Thank you! You helped a lot. I even signed up for this)

For everyone with scrartcl: you have to use addsec{title} instead of section*{title}

It will solve all your bookmark and appearance problems (toc, hyperref, markboth, ...)

Answered by rzickler on June 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP