# Redefine Section to include Hypertarget and tooltip

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,
{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

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[
]{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}
\end{document}


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

\documentclass[
]{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}

The example PDF has been made using scrartcl 2017/04/13 v3.23.
• @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
• @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