I'd like to create bookmarks dynamically, each time I use a custom heading (defined with \newenvironment) where the heading text becomes the bookmark name.

I'm using the bookmark package for hyperref, and I've considered using \pdfbookmark or \section within the environment declaration, but I need to be able to pass the environment contents into the command in order for the bookmark text to match the heading text.

I've also considered using \begin{section} and \end{section}, something like:


But I only get the first letter in the bookmark name, and it screws up my heading:

enter image description here

There are 7 headings, so my heading now begins with the number. How can you use unnumbered sections with \begin{section} ?

Also, only the first letter of my "Appendix" heading is taken and used for the bookmark, while the rest of the text is on the following line.

Here's a very simple example where headers appear on separate pages. When I compile it with pdflatex, I'd like bookmarks to point to each header's page.

\usepackage[colorlinks=true,linkcolor=blue,urlcolor=black,bookmarksopen=true]{hyperref} %bookmarks for pdf
\usepackage[open, openlevel=1]{bookmark}



\begin{heading}First Heading\end{heading}

Text in the first section


\begin{heading}Second Heading\end{heading}

Text in the second section


How can I configure bookmarks to be set for every use of my heading environment?

  • 1
    Welcome to TeX.SE. \begin{section}...\end{section} is 'wrong', The gobbling of your section title occurs because \begin{section} sees A only and the rest is not treated as an argument delimited by {}. The special use case of this is a little bit unclear and we don't see any real code in action. If you really need this setup, try \newenvironment{heading}[1]{\section{#1}}{}, however – user31729 Oct 27 '17 at 18:50
  • I also don't see the point in using an environment here at all, but this might be clarified in a compilable document, which is (still) missing (yet) – user31729 Oct 27 '17 at 19:02
  • The full heading looks more like this: \newenvironment{heading}{\fontsize{16}{19.2}\bfseries\selectfont\color{title}}{\vspace{3mm}} Where I need a heading to be formatted a particular way. The heading is referenced several places in the document as well as in a separate variables file, where the heading could be referenced multiple times. Because of this, I can't put the \section tags everywhere there is a header, instead I'd like to define the bookmarks in the \newenvironment declaration. I wouldn't doubt if there's a better approach to this. – Andrew Oct 27 '17 at 19:25
  • That's not sufficient code. Please show a compilable document, not just fragments. You can add it to the post above, but not as a comment. – user31729 Oct 27 '17 at 19:28
  • Most times there is a better approach ... – user31729 Oct 27 '17 at 19:30

There is no need to use an environment, but if you insist, you can always consider using environ:

enter image description here


\usepackage[bookmarksopen = true]{hyperref}

    \raisebox{\baselineskip}[0pt][0pt]{\pdfbookmark[1]{\BODY}{\BODY}}% Set bookmark
    \BODY}% Set heading


First Heading
Text in the first section


Second Heading
Text in the second section


\headingfont can be adjusted to suit your needs.

  • Thank you! The environ package worked great. What I really needed was to be able to pass \BODY into the \pdfbookmark call. Works great! – Andrew Oct 31 '17 at 18:17

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.