3

I would like section and subsection to automatically create a hypertarget (for external url linking into the pdf) and a pdftooltip, that displays the url (so the url is discoverable)

\section{\pdftooltip{Common Pressed Meats}{\#Common-Pressed-Meats}}\hypertarget{Common-Pressed-Meats}{}
\hypertarget{Canned-Pressed-Spam}{\subsection{\pdftooltip{Canned \& Pressed Spam}{\#Canned-Pressed-Spam}}}

Either of these lines work for me, my question us how to redefine \section and \subsection so that \section{Common Pressed Meats} will output one of the above, and to do the conversion from heading text (with symbols) to valid url without symbols, [Damn they almost work - pdfbookmark ia "Common Pressed Meats#Common-Pressed-Meats"]

Here is MWE for pdflatex

\documentclass[english]{scrartcl}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=1,
 breaklinks=true,pdfborder={0 0 1},backref=section,colorlinks=false]
 {hyperref}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{pdfcomment}

\makeatother

\begin{document}
\section{\pdftooltip{Common Pressed Meats}{\#Common-Pressed-Meats}}\hypertarget{Common-Pressed-Meats}{}

The hypertarget is Common-Pressed-Meats or from the internet http://www.spam.com\#Common-Pressed-Meats

\hypertarget{Canned-Pressed-Spam}{\subsection{\pdftooltip{Canned \& Pressed Spam}{\#Canned-Pressed-Spam}}}

Invalid and special URL chars (i.e. most symbols) in the heading text
must be removed from the hypertarget and replaced with dash -

\hypertarget{Other-Pressed-Meats}{\section{\pdftooltip{Other Pressed Meats}{\#Other-Pressed-Meats}}}

The tooltip is over the heading text, and shows the hypertarget preceded
by \#
\end{document}

What am I trying to do and Why?

I have documentation, in pdf, on the net. It is detailed and typically 5-50 pages long. When dealing with technical queries part of the answer is in the documentation. I need to be able to send a link that goes directly to the part of the document that explains my point. The link (url from internet, or Named-Destination in pdf terminology) needs to be human meaningful, and my answer should still be useful in 3 months time when the document has been revised (which rules out "section.2.1.3". Me and any other users need to quickly an easily know what the url is when reading the pdf. This is why have tootip. Otherwise the named destinations are too difficult to be useful Note that I am not interested in using the hypertarget internally within the latex/pdf

  • Imho such automatic labels are seldom useful -- when you rename the section every link will break. But beside this check tex.stackexchange.com/questions/376450/… – Ulrike Fischer Sep 2 '17 at 10:39
  • take a look to my answer here: tex.stackexchange.com/a/380116/120578 it redefines all kinds of sections for almost all classes. You may add your commands above evrey \oldsection call all bellow it and you may also include it inside an hyperlink. I will try to understand what you want to do and if possible I will write an answer. – koleygr Sep 2 '17 at 10:44
  • I agree that automatic labels are not ideal, but what hyperref currently does is this: section.8 , subsection.8.1 , page.11 , which is probably as utterly useless as it is possible to acheive – Henry Crun Sep 2 '17 at 10:54
  • hyperref already adds automatic hyper targets. It does not use a symbolic name for them but a section-leve-prefix, e.g., subsection followed by a dot and the section number, e.g., subsection.3.6 in the hyperref manual – Schweinebacke Sep 2 '17 at 10:57
  • The anchor for a section is section.1 etc. the number 1 is replaced by the total section number (unless the section number is reset). The same is almost true for any other structure level counter,, where the higher levels are appended, i.e. subsection.1.1 – user31729 Sep 2 '17 at 11:02
4

If you want more symbolic anchor names, you can use the extended optional argument feature of KOMA-Script (see option headings=optiontotocandhead in the examples below) and add a new key to generate the hyper targets:

\documentclass[
  headings=optiontotocandhead% Switch on extended optional arguments for headings
]{scrartcl}[2017/04/13]% Need at least KOMA-Script v3.23
\usepackage{hyperref}
\usepackage{lipsum}

\DefineFamilyKey[.section]{KOMAarg}{hypertarget}{%
  \hypertarget{#1}{}%
}

\begin{document}
\section[hypertarget={First.Section.Name}]{First Section Name}
\lipsum[1]
\section[hypertarget={Example.Section}]{Example Section}
\lipsum[2]
\section[hypertarget={Final.Section}]{Final Section}
See \hyperlink{First.Section.Name}{the first section} or
\hyperlink{Example.Section}{the second section} or
\hyperlink{Final.Section}{this final section}.
\end{document}

To also add a tool tip you could try something like:

\documentclass[
  headings=optiontotocandhead% Switch on extended optional arguments for headings
]{scrartcl}[2017/04/13]% Need at least KOMA-Script v3.23
\usepackage{hyperref}
\usepackage{pdfcomment}
\usepackage{lipsum}

\FamilyStringKey[.section]{KOMAarg}{hypertarget}{\currenthypertarget}
\newcommand*{\currenthypertarget}{}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
  \ifx\currenthypertarget\@empty
    \def\currenthypertarget{#1.\csname the#1\endcsname}%
  \fi  
  \hypertarget{\currenthypertarget}{}\@hangfrom{\hskip
    #2#3}{\pdftooltip{#4}{\#\currenthypertarget}}%
  \global\let\currenthypertarget\@empty
}
\makeatother

\begin{document}
\section[hypertarget={First.Section.Name}]{First Section Name}
\lipsum[1]
\section[hypertarget={Example.Section}]{Example Section}
\lipsum[2]
\section[hypertarget={Final.Section}]{Final Section}
See \hyperlink{First.Section.Name}{the first section} or
\hyperlink{Example.Section}{the second section} or
\hyperlink{Final.Section}{this final section}.
\end{document}

With Adobe Reader this second example gives, e.g.,

Bookmarks+Heading+Tooltip

The example PDF has been made using scrartcl 2017/04/13 v3.23.

  • SumatraPDF shows the tooltips (damn good). and this lets you list named destinations in pdfs. pdfresizer.com/list-named-destinations – Henry Crun Sep 2 '17 at 12:01
  • The second one makes the named destination and tooltip, but it is "section.2" and the tooltip is "#section.2". Unfortunately the pdfbookmark is "hypertarget=Example.Section" – Henry Crun Sep 2 '17 at 12:14
  • @HenryCrun The bookmarks are from the mandatory argument or from option tocentry. But don't forget headings=optiontotocandhead. – Schweinebacke Sep 2 '17 at 13:55
  • Yes that was it, found it in the morning, when I could see again! Could we chat about the second part of my problem, which is to automatically generate the targetname from the sectionname? I assume this line needs replacing: \def\currenthypertarget{#1.\csname the#1\endcsname} with something that calls the compact function in tex.stackexchange.com/questions/376450/ – – Henry Crun Sep 4 '17 at 7:41
  • @HenryCrun I don't think, that this makes sense, because then you would again need to change external links to the document whenever the (headings of the) document changes and you wouldn't need the tool tips. So the argument against the automatic numeric labels of hyperref wouldn't fit any longer and the whole thing wouldn't make sense. Nevertheless you only need to replace the default case \def\currenthypertarget{#1.\csname the#1\endcsname} by something that makes the default target from the (extended) title found in argument 4. – Schweinebacke Sep 4 '17 at 8:47

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.