2

I need to set the bookmark with character count limitations. For headings with more than 100 characters, only the first 100 characters should be shown, followed by an ellipsis (...). For headings with less than 100 characters, the full content should be displayed in the bookmark. (Compiling the file using lualatex-dev.exe)

MWE is below

\documentclass{article}


\usepackage{bookmark}

\usepackage{hyperref}
\begin{document}

\section{More than 100 character, Recent advances towards point-of-care devices for fungal detection: Emphasizing the role of plasmonic nanomaterials in current and future technologies}

The first section of this chapter consists of a brief and informal overview of the topics addressed in the book, in which only terms and notions familiar from traditional grammar and/or introductory courses in morphosyntax are used. Section 1.2 discusses the position taken by the author about some concepts generally considered basic for the analysis of clause structure and its semantic correlates, and the terms used to refer to them. Section 1.3 presents the framework within which transitivity, valency,

\section{Less than 100 character Section heading}

The first section of this chapter consists of a brief and informal overview of the topics addressed in the book, in which only terms and notions familiar from traditional grammar and/or introductory courses in morphosyntax are used. Section 1.2 discusses the position taken by the author about some concepts generally considered basic for the analysis of clause structure and its semantic correlates, and the terms used to refer to them. Section 1.3 presents the framework within which transitivity, valency,


\end{document}

My expected output is below:

enter image description here

5
  • do you really want to just cut it off or you could use \texorpdfstring{a short bookmark}{the original long TeX section title} Commented Jul 9 at 13:35
  • @DavidCarlisle, Is it possible to automate based on the character count instead of adding \texorpdfstring{}{}? Commented Jul 9 at 14:07
  • 1
    yes sure, especially in luatex but if you have the time for hand tuning you get a much more readable bookmark panne by choosing the bookmarks than having it simply kut off mid-word. somy comment was just to suggest that alternative Commented Jul 9 at 14:12
  • I assume you don't want to use the optional argument to \section because it would also shorten the entry in the ToC?
    – cfr
    Commented Jul 9 at 14:13
  • The danger with automated solutions is that if the title is 101 (or 100?) characters long, you end up with the last character being replaced by three characters.
    – Teepeemm
    Commented Jul 9 at 15:05

1 Answer 1

2

You can preprocess the title.

\documentclass{article}
\usepackage{hyperref}
\usepackage{bookmark}

\NewCommandCopy{\latexsection}{\section}
\RenewDocumentCommand{\section}{sO{#3}m}{%
  \IfBooleanTF{#1}{\latexsection*{#3}}{%
    \shortenbookmark{\latexsection}{#2}{#3}%
  }%
}
%%% do similarly for other section levels

%%% the main command
\ExplSyntaxOn
\tl_new:N \l__murugan_bookmark_tl
\NewDocumentCommand{\shortenbookmark}{mmm}
 {
  \tl_set:Nn \l__murugan_bookmark_tl {#2}
  \tl_replace_all:Nnn \l__murugan_bookmark_tl { ~ } { \c_space_tl }
  \int_compare:nT { \tl_count:N \l__murugan_bookmark_tl > 100 }
   {
    \tl_set:Ne \l__murugan_bookmark_tl { \tl_range:Nnn \l__murugan_bookmark_tl {1} {100} … }
   }
  #1[\texorpdfstring{#2}{\l__murugan_bookmark_tl}]{#3}
 }
\ExplSyntaxOff

\begin{document}

\section{More than 100 character, Recent advances towards point-of-care 
devices for fungal detection: Emphasizing the role of plasmonic nanomaterials 
in current and future technologies}

The first section of this chapter consists of a brief and informal overview 
of the topics addressed in the book, in which only terms and notions familiar 
from traditional grammar and/or introductory courses in morphosyntax are used. 
Section 1.2 discusses the position taken by the author about some concepts 
generally considered basic for the analysis of clause structure and its 
semantic correlates, and the terms used to refer to them. Section 1.3 
presents the framework within which transitivity, valency.

\section{Less than 100 character Section heading}

The first section of this chapter consists of a brief and informal overview 
of the topics addressed in the book, in which only terms and notions familiar 
from traditional grammar and/or introductory courses in morphosyntax are used. 
Section 1.2 discusses the position taken by the author about some concepts 
generally considered basic for the analysis of clause structure and its 
semantic correlates, and the terms used to refer to them. Section 1.3 
presents the framework within which transitivity, valency,

\end{document}

enter image description here

If you also need subsections, add

\NewCommandCopy{\latexsubsection}{\subsection}
\RenewDocumentCommand{\subsection}{sO{#3}m}{%
  \IfBooleanTF{#1}{\latexsubsection*{#3}}{%
    \shortenbookmark{\latexsubsection}{#2}{#3}%
  }%
}

and similarly for other section levels.

3
  • +1 A question: aacros are not expanded right? For how many will \textbf{abc} count? Shouldn't the count rather be a hack inside hyperref internals so that all its parsing has happened already?
    – user691586
    Commented Jul 9 at 18:07
  • @user691586 One could add \text_purify:n to get rid of \textbf (and similar commands), which don't make sense in bookmarks.
    – egreg
    Commented Jul 9 at 19:27
  • ok, and my \textbf was ill-chosen for a section title usually rendered in bold face anyhow.
    – user691586
    Commented Jul 9 at 20:00

You must log in to answer this question.

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