Combining hyperref's label macro with tikz macro

I would like to mark my labels visually. Trying to expand hyperref's macro \label using the \label@hook gives me error messages and some weird behaviour.

MWE:

\documentclass{scrbook}

\usepackage{blindtext}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{hyperref}

\parindent=0pt

\makeatletter
\newcommand{\labmarred}{\@bsphack\tikz[remember picture,overlay] \fill[red] (0,0.75ex) circle (4pt);\@esphack}
\renewcommand{\labmarred}{\tikz[remember picture,overlay] \fill[red] (0,0.75ex) circle (4pt);}

\AtBeginDocument{\preto{\label@hook}{\labmarred}}
\makeatother

\begin{document}
\chapter{Chap 1}\label{chap:a}
\blindtext

\section{Sec 1}\label{sec:a}
\blindtext

\phantomsection\label{sec:a:a}
\blindtext

\labmarred
\blindtext

\subsection{Subsec 1}\label{subsec:a}
\blindtext

\subsection{Subsec 2}\label{subsec:b}
\blindtext

\subsection{Subsec 3}\label{subsec:c}
\blindtext
\end{document}


There is a spurious indent of the first texxt line on every red dot except the one that has been inserted manually (line 27 by calling \labmarred) and several error messages of the type Bad space factor (0).

Both versions in the above example result in exactly the same as well as using the tikz environment instead of \tikz does.

The showlabels package and the showkeys version are using \marginpar which is often not at the right position.

Any hint where to go?

• You shouldn't fiddle around with \label@hook but use the safer \pretocmd{\label}{\labmarred}{}{}}, in my point of view – user31729 Feb 11 at 14:06
• A tikz drawing is a box, even if it has size zero, if will change the typesetting if you add it at this places. – Ulrike Fischer Feb 11 at 14:11
• @Christian Hupfer can't check at the moment but as far as I remember \pretocmd gives the same result (see comment of @Ulrike Fischer) – AndiW Feb 11 at 14:15
• @AndiW: I can compile your example with my proposition at least, the indent is there anyway, but the compilation does not fail – user31729 Feb 11 at 14:16
• @ChristianHupfer unfortunately here your solution fails at the last \subsection... – AndiW Feb 11 at 19:17

Thanks to the comment of @Ulrike Fischer that \tikz is embedded in a hbox i found the tikzmark-library that provides \pgfmark which is the same as the \tikzmark but without the \hbox. The following code works with some restrictions: From my point of view there is nothing wrong to use \label@hook since the label name is available and as far as I understand the hook-system hooks are designed just for that.

\documentclass{scrbook}

\usepackage{blindtext}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\usepackage{hyperref}

\parindent=0pt
\parskip=1ex

\makeatletter
\newcounter{markcnt}
\pgfmark{dummy\arabic{markcnt}}\marginpar{\tikz[remember picture,overlay] \fill[red] ($(pic cs:dummy\arabic{markcnt})-(0,3ex)$) circle (4pt);}}

%\AtBeginDocument{\pretocmd{\label}{\labmarred}{}{}}
\AtBeginDocument{\preto{\label@hook}{\labmarred}{}{}}
\makeatother

\begin{document}
\chapter{Chap 1}\label{chap:a}
\blindtext

\section{Sec 1}\label{sec:a}
\blindtext

\phantomsection\label{sec:a:a}
\blindtext

\labmarred
\blindtext

\subsection{Subsec 1}\label{subsec:a}
\blindtext

\subsection{Subsec 2}\label{subsec:b}
\blindtext

\subsection{Subsec 3}\label{subsec:c}
\blindtext
\end{document}


Issues:

1. There is an y-shift of about +3ex I could not find the reason for so the \tikz-circle has to be shifted down to be at approximately the right position.

2. Since the circles are drawn in a \marginpar there is still an issue if the \marginpar is moved to the next page - It might be better to put that into the foot or the head of the page.

Anyway - I provide this one as an answer since it seems to be one possible way to go...

Thx to @Christian Hupfer for the critical input and @Ulrike Fischer for the (hidden) hint where to go...

EDIT:

Due to the definitely inaccessible page break system of TeX even this scetchy solution will fail if there is a \label placed at the end of a paragraph which spans over several pages... In short: doesn't work as expected...