5

I am trying to generate my own recipe collection. I've got my inspiration from An Aesthetically Pleasing Recipe Book Template. It is really similar but I've tweaked a few things. So far, it's working well but I found something that bugs me and I do not know how to solve it.

I wanted to generate a pdf file from my collection with easy access to the recipe so I used the hyperref package and its pdfbookmark command. It works until I am trying to use accented characters in that command. It seems that those characters are breaking the bookmark rendering.

  • If I remove the accented characters, it works.
  • If I replace é by \`e, it does not work.
  • I've tried to use utf8x with \PrerenderUnicode{é} instead of utf8 as mentioned in this answer, but it does not work.
  • I've tried to use the unicode option in the hyperref package as mentioned in this answer, but it does not work.

Here is a minimal (kind of) working example of my collection:

\documentclass[twoside, 12pt]{article}
\usepackage[letterpaper, headheight=15pt, left=1cm, right=1cm, top=2cm, bottom=2cm]{geometry}
\usepackage{fancyhdr}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks,bookmarks,unicode]{hyperref}
\usepackage{bookmark}

\newcommand{\group}[1]{
    \pdfbookmark{#1}{#1}
}

\newcommand{\recipe}[1]{%
    \newpage%
    \pagestyle{fancy}%
    \fancyhead[RO,LE]{\footnotesize{\textbf{#1}}}%
    \fancyfoot[RO,LE]{\thepage}%
    \fancyfoot[C]{}%
    \pdfbookmark[1]{#1}{\pdfmdfivesum{#1}}%
    \section*{#1}%
}

\begin{document}
    \group{Boissons}
    \recipe{Dark \& stormy}
    \recipe{Limonana}

    \group{Desserts}
    \recipe{Tiramisu}
    \recipe{X-mas cake}

    \group{Poisson}
    \recipe{Gravlax de saumon au miel et à la moutarde}
    \recipe{Pain de saumon}

    \group{Viande}
    \recipe{Boulets de Liège}
    \recipe{Poulet à l'ananas}
    \recipe{Ragoût à la joue de bœuf et lard paysan}
\end{document}

What should I do to allows the bookmarks to work properly with accents?

8
  • Just add the instruciton \usepackage[T1]{fontenc} in the preamble.
    – Mico
    Feb 3, 2021 at 13:02
  • @Mico I've tried that as well with no luck :/
    – A.D.
    Feb 3, 2021 at 13:14
  • use \pdfbookmark[1]{#1}{\pdfmdfivesum{\detokenize{#1}}} Feb 3, 2021 at 13:18
  • 1
    in no way geometry can affect hyperref here. In your second example you removed the bookmark package and that has an effect, as bookmark changes the pdfbookmark command. Feb 3, 2021 at 13:31
  • 2
    well it should work with the bookmark package too, but you then need \detokenize or the links will be wrong. Feb 3, 2021 at 15:05

3 Answers 3

3

I suggest you (a) load the fontenc package with the option T1, (b) simplify the way you load the hyperref package, and (c) not load the bookmark and inputenc packages unless your TeX distributions is several years old and hasn't been updated in more than three years.

enter image description here

\documentclass[twoside, 12pt]{article}
\usepackage[letterpaper, headheight=15pt, hmargin=1cm, vmargin=2cm]{geometry}
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}         % <-- new
\usepackage[bookmarks]{hyperref} % <-- simplified

\newcommand{\group}[1]{\pdfbookmark{#1}{#1}}
\newcommand{\recipe}[1]{%
    \newpage%
    \pagestyle{fancy}%
    \fancyhead[RO,LE]{\footnotesize\textbf{#1}}%
    \fancyfoot[RO,LE]{\thepage}%
    \fancyfoot[C]{}%
    \pdfbookmark[1]{#1}{\pdfmdfivesum{#1}}%
    \section*{#1}%
}

\begin{document}
\group{Boissons}
    \recipe{Dark \& stormy}
    \recipe{Limonana}

\group{Desserts}
    \recipe{Tiramisu}
    \recipe{X-mas cake}

\group{Poisson}
    \recipe{Gravlax de saumon au miel et à la moutarde}
    \recipe{Pain de saumon}

\group{Viande}
    \recipe{Boulets de Liège}
    \recipe{Poulet à l'ananas}
    \recipe{Ragoût à la joue de bœuf et lard paysan}
\end{document}
7
  • the bookmarks option is not the same as loading the bookmark package which quite extends and improves the bookmark handling. Feb 3, 2021 at 13:20
  • 1
    I'm not sure why discouraging bookmarks, which is generally better than the bookmarks option to hyperref and was written for the purpose of improving bookmark management.
    – egreg
    Feb 3, 2021 at 13:27
  • @egreg -- I use Acrobat Reader (AR) as my pdf viewer, on a MacOS 12.2 "Big Sur" system. If I follow your (implicit?) suggestion, i.e., if I do not specify the bookmarks option when loading hyperref but do load the bookmark package, then opening the pdf file in AR and clicking on one of the bookmark tabs does absolutely nothing. In contrast, with the answer I posted above, I do get the desired behavior. That is, incidentally, why I posted the answer the way I did. Maybe other pdf viewers behave differently?
    – Mico
    Feb 3, 2021 at 15:26
  • you write as comment "simplified" and don't really explain why you remove bookmark. To me it makes the impression as if you think it is unneeded. But actually it slows down the compilation, (without bookmark, you need two to get the bookmarks), and you loose the additional features of the package. Feb 3, 2021 at 15:35
  • And regarding the bookmarks doing nothing: That's why I wrote that \detokenize is needed, at least for the bookmarks with accent. Feb 3, 2021 at 15:36
3

The rendering of the bookmarks in your example should work fine if you compile with a current tex system and the pdf viewer can handle unicode.

What doesn't work are the actual links: the bookmark package and hyperref have slightly different methods and settings to handle commands in destination names and so create different names in your example. So you need to use \detokenize to make them to simple strings:

\documentclass{article}
\usepackage[unicode]{hyperref}
\usepackage{bookmark}
\begin{document}
xxx
\newpage 
aaa
\pdfbookmark[1]{1. Ragoût à la joue de bœuf et lard paysan}
  {\pdfmdfivesum{zzzRagoût à la joue de bœuf et lard paysan}}


\pdfbookmark[1]{2. Ragoût à la joue de bœuf et lard paysan}
  {\pdfmdfivesum{\detokenize{xxxRagoût à la joue de bœuf et lard paysan}}}
\end{document}

In the first bookmark will give a warning

 pdfTeX warning (dest): name{68378F0B27FA693E17106CB4A2941F21.
 1} has been referenced but does not exist, replaced by a fixed one

and jump to the first page, but the second will work.

2

An alternative is to use the sectioning commands. The hyperref package will generate bookmarks for all sections, subsections, and so on, down to the current bookmarksdepth (which by default is the \tocdepth).

It lets you use all the standard commands for section formatting, create a table of contents page in addition to the PDF bookmarks, and make references to a \label.

\documentclass[twoside, 12pt]{article}
\tracinglostchars=2 % Warn if the current font is missing a glyph
\usepackage[letterpaper, headheight=15pt, left=1cm, right=1cm, top=2cm, bottom=2cm]{geometry}
\usepackage{fancyhdr}
\usepackage[hidelinks,bookmarks,bookmarksdepth,unicode]{hyperref}
\usepackage{libertinus} % Or your fonts of choice.
\usepackage{microtype}

\setcounter{tocdepth}{2} % Create bookmarks for subsections (recipes)

\newcommand{\group}[1]{%
  \clearpage%
  \section{#1}%
  \markright{}%
}

\newcommand{\recipe}[1]{%
  \pagebreak%
  \subsection*{#1}%
  \markright{#1}%
}

\pagestyle{fancy}%
\fancyhead[RE,LO]{\textsc{\nouppercase{\leftmark}}}
\fancyhead[RO,LE]{\footnotesize{\textbf{\rightmark}}}%
\fancyfoot[RO,LE]{\thepage}%
\fancyfoot[C]{}%

\begin{document}
    \group{Boissons}\label{gr:boissons}
    \recipe{Dark \& stormy}\label{rx:darknstormy}
    \recipe{Limonana}\label{rx:limonana}

    \group{Desserts}\label{gr:desserts}
    \recipe{Tiramisu}\label{rx:tiramisu}
    \recipe{X-mas cake}\label{rx:xmascake}

    \group{Poisson}\label{gr:poisson}
    \recipe{Gravlax de saumon au miel et à la moutarde}\label{rx:gravlax}
    \recipe{Pain de saumon}\label{rx:painsaumon}

    \group{Viande}\label{gr:viande}
    \recipe{Boulets de Liège}\label{rx:liegs}
    \recipe{Poulet à l'ananas}\label{rx:lananas}
    \recipe{Ragoût à la joue de bœuf et lard paysan}\label{rx:ragoutpaysan}
\end{document}

That code is compatible with both LuaLaTeX and PDFTeX. I personally recommend LuaTeX except when a publisher forces you to use PDFTeX, but use what you prefer.

5
  • 1
    \let\tocdepth=2? Are you sure?
    – egreg
    Feb 3, 2021 at 21:35
  • @egreg No. I had it working earlier, but that looks like a goof.
    – Davislor
    Feb 3, 2021 at 21:35
  • 1
    Possibly \setcounter{tocdepth}{2}?
    – egreg
    Feb 3, 2021 at 21:37
  • @egreg Yep. Was testing. Thanks for catching the mistake.
    – Davislor
    Feb 3, 2021 at 21:40
  • @egreg Also needs the explicit bookmarksdepth option anyway, so I might as well have set it there.
    – Davislor
    Feb 3, 2021 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .