3

UPDATE:

The Bounty period is over (AND GUESS WHAT? I gave the credits to the wrong Person (sorry CFR), but thanks Aditya for your contribution. Please upvote CFR answer!!). Find below my current implementation + MWE. If you make improvements to it, please post a reference here as answer/comment This works both for list environments and until page breaks.

Thanks/Contributors:

  • Martin Schrarrer: Original Concept
  • CFR: For his answer with until end of page underliner + warning (see below)
  • David Carlisle: Which helped me with getting Martin/CFR code working on list environments by answering my questions
  • Myself :-): For adding the code to handle list environments (totalleftmargin) an printing the current page in the page break warning.

How it looks like (please not it support highlighting, etc as well, see package/second screenshot below)

enter image description here

MWE

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{tikzpagelayers-xxx}
\begin{document}
Test \tikzul[red]{Test}
\begin{itemize}
\item Test test test test test test test \tikzul[blue]{also working in list environment}
\end{itemize}
\vspace{7.8cm}
Test Test Test Test Test Test TEst Test Test Test Test \tikzul[green]{Demonstrating underline over pagebreak also working until pagebreak.}
\end{document}

tikzpagelayers-xxx.sty

\ProvidesPackage{tikzpagelayers-xxx}[2016/10/20 v1.0 Hacked version of tikzpagelayers, a package to draw with TikZ before or behind text on the page]
\RequirePackage{tikzpagenodes}
\RequirePackage{atbegshi}
\usetikzlibrary{backgrounds,calc}
\RequirePackage{tikz}
\RequirePackage{zref-abspos}

\newcommand\tikzpagemark{}
\DeclareRobustCommand\tikzpagemark[1]{%
    \leavevmode
    \zsavepos{tikzpagemark-#1}%
}


\def\tpl@addto#1#2{%
    \begingroup
    \ifx#1\@undefined
        \global\let#1\empty
    \else
        \ifx#1\relax
            \global\let#1\empty
        \fi
    \fi
    \edef\@tempa{\@thetikzpagemark}%
    \expandafter\g@addto@macro\expandafter#1\expandafter{%
        \expandafter\set@tikzpagemark\expandafter{\@tempa}%
        #2%
    }%
    \endgroup
}

\newenvironment{tikzpagelayer}[2][foreground]{%
    \stepcounter{tikzpagemark}%
    \tikzpagelayeron
    \tikzpagemark{\@thetikzpagemark-begin}%
    \expandafter\tpl@addto\csname tikzpagelayer@#1@page\zref@extract{tikzpagemark-\@thetikzpagemark-begin}{abspage}\endcsname{#2}%
    \edef\endtikzpagelayer{\noexpand\tikzpagemark{\@thetikzpagemark-end}}%
}{}%

\newcommand{\starttikzpagelayer}[3][foreground]{%
    \begingroup
    \tikzpagelayeron
    \edef\@thetikzpagemark{@#2}%
    \tikzpagemark{\@thetikzpagemark-begin}%
    \expandafter\tpl@addto\csname tikzpagelayer@#1@page\zref@extract{tikzpagemark-\@thetikzpagemark-begin}{abspage}\endcsname{#3}%
    \endgroup
}
\newcommand{\stoptikzpagelayer}[1]{%
    \tikzpagemark{@#1-end}%
}%

\newcommand\tikzul[2][]{%
    \begingroup
    \edef\tmp{%
    \noexpand\tikzpagelayer{\noexpand\@tikzul{\the\@totalleftmargin}{tplyshift=-.8\dp\strutbox,#1}}}%
    \tmp
    #2%
    \endtikzpagelayer
    \endgroup
}


\newcommand\tikzhl[2][]{%
    \begingroup
    \tikzpagelayer[background]{\@tikzul{tplyshift=.5\ht\strutbox-.5\dp\strutbox,line width=\baselineskip,yellow,#1}}%
    #2%
    \endtikzpagelayer
    \endgroup
}

\renewcommand\tikzhl[2][]{%
    \begingroup
    \tikzpagelayer[background]{\draw [line width=.5cm,#1] (tplbegin) -- (tplend);}%
    #2%
    \endtikzpagelayer
    \endgroup
}

\newcommand\tikzshl[2][]{%
    \begingroup
    \tikzpagelayer[background]{\shade [line width=.5cm,#1] (tplbegin) ++(0,1ex) rectangle (tplend);}%
    #2%
    \endtikzpagelayer
    \endgroup
}


\def\tikzul@loop#1#2#3{%
    #3%
    \ifdim\dimexpr#1>1.1\baselineskip
        #2%
        \expandafter\tikzul@loop\expandafter{\the\dimexpr#1-\baselineskip\relax}{#2}%
    \fi
}



\tikzset{tplyshift/.code={\def\@tplyshift{#1}}}

\def\tikzullinewidth{\linewidth}

\def\@tikzul#1#2{%
  \ifnum\tplfirstpage=\tpllastpage
      \draw [#2] let \p1 = (tplbegin), \p2 = (tplend), \p3 = ([xshift=#1]current page text area.south west), \p4 = (current page text area.north east) in
          \ifdim\y1=\y2
              (\x1,\y1+\@tplyshift) -- (\x2,\y2+\@tplyshift)
          \else
              (\x1,\y1+\@tplyshift) -- (\x4,\y1+\@tplyshift)
              (\x3,\y1+\@tplyshift)
              \tikzul@loop{\y1-\y2}{%
                  ++(0,-\baselineskip) -- +(\tikzullinewidth,0)
              }{}
              (\x3,\y2+\@tplyshift) -- (\x2,\y2+\@tplyshift)
          \fi
      ;
  % add code to add stuff to end of current page
  \else
      \draw [#2] let \p1 = (tplbegin), \p2 = (current page text area.south east), \p3 = ([xshift=#1]current page text area.south west), \p4 = (current page text area.north east) in
          \ifdim\y1=\y2
              (\x1,\y1+\@tplyshift) -- (\x2,\y2+\@tplyshift)
          \else
              (\x1,\y1+\@tplyshift) -- (\x4,\y1+\@tplyshift)
              (\x3,\y1+\@tplyshift)
              \tikzul@loop{\y1-\y2}{%
                  ++(0,-\baselineskip) -- +(\tikzullinewidth,0)
              }{}
              (\x3,\y2+\@tplyshift) -- (\x2,\y2+\@tplyshift)
          \fi
      ;
      \PackageWarning{tikzpagelayers-xxx}{Annotation not continued over page break. Current page \tplfirstpage }%
  \fi
}

\newcommand\tikzso[1][]{%
    \tikzul[tplyshift=.6ex,#1]%
}

\newcommand\tikzhi[1][]{%
    \tikzul[on background layer,tplyshift=.5\ht\strutbox-.5\dp\strutbox,line width=\baselineskip,yellow,#1]%
}

\newenvironment{tpframebox*}[2][]{%
    \par
    \begingroup
    \tikzset{#1}\pgfmathsetmacro\tpframebox@sep{2\fboxsep+\pgflinewidth}
    \edef\@tempa{\endgroup\def\noexpand\tpframebox@sep{\tpframebox@sep pt}}%
    \@tempa
    \vspace{\tpframebox@sep}%
    \setlength{\hsize}{#2}%
   %\addtolength{\hsize}{-\tpframebox@sep}%
   %\addtolength{\hsize}{-\tpframebox@sep}%
    \linewidth\hsize
    \leftskip=\tpframebox@sep
    \rightskip=\tpframebox@sep
    \tikzpagelayer{\@tpframebox@s{#1}}%
}{%
    \endtikzpagelayer
    \par\vspace{\tpframebox@sep}%
}

\newenvironment{tpframebox}[1][]{%
    \par
    \begingroup
    \tikzset{#1}\pgfmathsetmacro\tpframebox@sep{2\fboxsep+\pgflinewidth}
    \vspace{\tpframebox@sep pt}%
    \edef\@tempa{\endgroup\def\noexpand\tpframebox@sep{\tpframebox@sep}}%
    \@tempa
    \tikzpagelayer{\@tpframebox{#1}}%
}{%
    \endtikzpagelayer
    \par\vspace{\tpframebox@sep pt}%
}

\newcommand\newtpfenvironment[6][foreground]{%
    \@namedef{@tpfenv@#2@layer}{#1}%
    \@namedef{@tpfenv@#2@single}{#3}%
    \@namedef{@tpfenv@#2@first}{#4}%
    \@namedef{@tpfenv@#2@middle}{#5}%
    \@namedef{@tpfenv@#2@last}{#6}%
    \newenvironment{#2}{%
        \tikzpagelayer[#1]{%
          \ifnum\tplfirstpage=\tpllastpage
              \csname @tpfenv@#2@single\endcsname
          \else
              \csname @tpfenv@#2@first\endcsname
              \expandafter\tplonlastpage\expandafter{\csname @tpfenv@#2@last\endcsname}%
              \expandafter\tplonmiddlepage\expandafter{\csname @tpfenv@#2@middle\endcsname}%
          \fi
        }%
    }{%
        \endtikzpagelayer
    }%
}

\newtpfenvironment{tpltest}{\draw (tplbegin) -- (tplend);}{}{}{}

\def\@tpframebox#1{%
    \begin{scope}[#1]
    \ifnum\tplfirstpage=\tpllastpage
        \path [#1] let \p1= (tplbegin), \p2 = (tplend), \p3 = (current page text area.north west), \p4 = (current page text area.south east) in
            (\x3-\fboxsep-.5\pgflinewidth,\y1+\ht\strutbox+\fboxsep+.5\pgflinewidth) rectangle (\x4+\fboxsep+.5\pgflinewidth,\y2-\dp\strutbox-\fboxsep-.5\pgflinewidth)
        ;
    \else
        \path [#1] let \p1= (tplbegin), \p3 = (current page text area.north west), \p4 = (current page text area.south east) in
            (\x3-\fboxsep-.5\pgflinewidth,\y4-\dp\strutbox-\fboxsep) --  (\x3-\fboxsep-.5\pgflinewidth,\y1+\ht\strutbox+\fboxsep+.5\pgflinewidth) -| (\x4+\fboxsep+.5\pgflinewidth,\y4-\dp\strutbox-\fboxsep)
        ;
        \tplonpage{\tpllastpage}{%
            \begin{scope}[#1]
            \path [#1] let \p1 = (tplend), \p3 = (current page text area.north west), \p4 = (current page text area.south east) in
                (\x3-\fboxsep-.5\pgflinewidth,\y3+.5\pgflinewidth) --
                (\x3-\fboxsep-.5\pgflinewidth,\y1-\dp\strutbox-\fboxsep-.5\pgflinewidth) -|
                (\x4+\fboxsep+.5\pgflinewidth,\y3+.5\pgflinewidth)
            ;
            \end{scope}%
        }%
        \tplonmiddlepages{%
            \begin{scope}[#1]
            \path [#1,draw=none] let \p1 = (current page text area.north west), \p2 = (current page text area.south east) in
                (\x1-\fboxsep-.5\pgflinewidth,\y1+.5\pgflinewidth) --
                (\x1-\fboxsep-.5\pgflinewidth,\y2-.5\pgflinewidth) --
                (\x2+\fboxsep+.5\pgflinewidth,\y2-.5\pgflinewidth) --
                (\x2+\fboxsep+.5\pgflinewidth,\y1+.5\pgflinewidth) -- cycle
            ;
            \path [#1] let \p1 = (current page text area.north west), \p2 = (current page text area.south east) in
                (\x1-\fboxsep-.5\pgflinewidth,\y1+.5\pgflinewidth) --
                (\x1-\fboxsep-.5\pgflinewidth,\y2-.5\pgflinewidth)
                (\x2+\fboxsep+.5\pgflinewidth,\y2-.5\pgflinewidth) --
                (\x2+\fboxsep+.5\pgflinewidth,\y1+.5\pgflinewidth)
            ;
            \end{scope}%
        }%
    \fi
    \end{scope}
}
\tikzset{tpframebox/.style={}}


\def\@tpframebox@s#1{%
    \begin{scope}[#1]
    \ifnum\tplfirstpage=\tpllastpage
        \draw [#1] let \p1= (tplbegin), \p2 = (tplend), \p3 = (current page text area.north west), \p4 = (current page text area.south east) in
            (\x3+.5\pgflinewidth,\y1+\ht\strutbox+\fboxsep+.5\pgflinewidth) rectangle (\x4-.5\pgflinewidth,\y2-\dp\strutbox-\fboxsep-.5\pgflinewidth)
        ;
    \else
        \draw [#1] let \p1= (tplbegin), \p3 = (current page text area.north west), \p4 = (current page text area.south east) in
            (\x3-\fboxsep-.5\pgflinewidth,\y4-\dp\strutbox-\fboxsep) --  (\x3-\fboxsep-.5\pgflinewidth,\y1+\ht\strutbox+\fboxsep+.5\pgflinewidth) -| (\x4+\fboxsep+.5\pgflinewidth,\y4-\dp\strutbox-\fboxsep)
        ;
    \fi
    \end{scope}
}
\tikzset{tpframebox/.style={}}

\def\set@tikzpagemark#1{%
    \def\@thetikzpagemark{#1}%
    \edef\tplfirstpage{\zref@extract{tikzpagemark-#1-begin}{abspage}}%
    \edef\tpllastpage{\zref@extract{tikzpagemark-#1-end}{abspage}}%
    \coordinate (tplbegin) at ([shift={(\zposx{tikzpagemark-#1-begin}sp,\zposy{tikzpagemark-#1-begin}sp)}]current page.south west);%
    \coordinate (tplend)   at ([shift={(\zposx{tikzpagemark-#1-end}sp,\zposy{tikzpagemark-#1-end}sp)}]current page.south west);%
}

\newcommand\tplonpage[2][foreground]{%
    \begingroup
    \pgfmathtruncatemacro\@tplpage{#2}%
    \expandafter\endgroup
    \expandafter\tpl@addto\csname tikzpagelayer@#1@page\@tplpage\endcsname
}

\newcommand\tplonnextpage[1][foreground]{%
    \tplonpage[#1]{\@tplnextpage}%
}

\newcommand\tplonlastpage[1][foreground]{%
    \tplonpage[#1]{\tpllastpage}%
}

\newcommand\tplonmiddlepages[2][foreground]{%
    \begingroup
    \@tempcnta=\tplfirstpage\relax
    \loop
        \advance\@tempcnta\@ne
      \ifnum\@tempcnta<\tpllastpage
        \tplonpage[#1]{\number\@tempcnta}{#2}%
    \repeat
    \endgroup
}

% During the page `abspage' is one to low
\def\@tplnextpage{\c@abspage+2}
% In the shipout routine it is correct
\def\@@tplnextpage{\c@abspage+1}

\newcounter{tikzpagemark}
\def\@thetikzpagemark{\number\c@tikzpagemark}

\def\@tikzpagelayeron{%
   % \AtBeginShipoutNext{\tikzpagelayer@atbeginshipout}%
   % \global\let\tikzpagelayeron\relax
}
\AtBeginShipout{\tikzpagelayer@atbeginshipout}%
\let\tikzpagelayeron\@tikzpagelayeron
\let\tikzpagelayer@background\empty
\let\tikzpagelayer@foreground\empty

\def\tikzpagelayer@atbeginshipout{%
    \setbox\AtBeginShipoutBox\hbox{%
        \color@setgroup
        \let\@tplnextpage\@@tplnextpage
        \begin{tikzpicture}[remember picture]%
            \path [use as bounding box,every node/.style={},every rectangle node/.style={}]
                node [inner sep=0pt,outer sep=0pt] (current page box) {\box\AtBeginShipoutBox};
         %  \fill [line width=10pt,blue,opacity=.5] (current page box.north west) rectangle (current page box.south east);
            \begin{pgfonlayer}{background}
            \begin{scope}%
                \csname tikzpagelayer@background@page\number\c@abspage\endcsname
                \tikzpagelayer@background
            \end{scope}%
            \end{pgfonlayer}%
            \begin{scope}%
                \csname tikzpagelayer@foreground@page\number\c@abspage\endcsname
                \tikzpagelayer@foreground
            \end{scope}%
        \end{tikzpicture}%
        \color@endgroup
    }%
    \global\let\tikzpagelayer@background\empty
    \global\let\tikzpagelayer@foreground\empty
    \global\let\tikzpagelayeron\@tikzpagelayeron
}

START ORIGINAL QUESTION Message

I am looking for a TikZ expert which can give some advice on the following. As I am relatively new to TikZ and this looks like an advanced topic, help would be appreciated.

Context

I am using an excellent featurerich TikZ Highlighter/underline code snippet original developed and posted by Martin Schrarrer. Different other 'forks' are published e.g. this one.

Problem

The original posting/code discuss pagebreaks (see also screenshot below), however it looks not properly implemented. If this TikZ snippet is used at pagebreaks, compilation (second run) will fail with:

! Package tikz Error: Giving up on this path. Did you forget a semicolon?.

As my document is now growing, and this snippet is used more-and-more, more failures occur. Until now I handled this by introducing extra linebreaks or stop/restart this code at page breaks. This become unmanageable.

Minimal Working Example to demonstrate failure

I have added a MWE below. The preamble is untouched (original Martin's code). It breaks at the second compile phase.

Analysis

Ulrike did post below some remarks that the code does contain some bugs. As reported by Ulrike the Martin's original MWE page break only works in his MWE situation (because of special circumstances).

What I am looking for

Which changes are needed to avoid it crashes plus in prefered order:

  • Is working on two sided book pagebreaks (left/right margin different)
  • Does continue over pagebreaks, but handles two side book document not correctly (preferable log this as well)
  • Does not continue over pagebreaks but stops at the end of the page where it did start. Write a warning to log.
  • Does not do anything. In case of page breaks in between, no TikZ output appears only a log statement with pagenumber, etc.

enter image description here

MWE producing (on second run) Package tikz Error: Giving up on this path. Did you forget a semicolon?

\documentclass[twoside,11pt]{book}

\usepackage{zref-abspage}
\usepackage{zref-user}
\usepackage{tikz}
\usepackage{atbegshi}
\usetikzlibrary{calc,decorations.pathmorphing}

\makeatletter
\newcommand{\currentsidemargin}{%
  \ifodd\zref@extract{textarea-\thetextarea}{abspage}%
    \oddsidemargin%
  \else%
    \evensidemargin%
  \fi%
}

\newcounter{textarea}
\newcommand{\settextarea}{%
   \stepcounter{textarea}%
   \zlabel{textarea-\thetextarea}%
   \begin{tikzpicture}[overlay,remember picture]
    % Helper nodes
    \path (current page.north west) ++(\hoffset, -\voffset)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\paperwidth, minimum height=\paperheight]
        (pagearea) {};
    \path (pagearea.north west) ++(1in+\currentsidemargin,-1in-\topmargin-\headheight-\headsep)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\textwidth, minimum height=\textheight]
        (textarea) {};
  \end{tikzpicture}%
}


\usepackage{lipsum}
\newcommand\xlipsum[1][]{{\let\par\relax\lipsum*[#1]}}

\tikzset{tikzul/.style={yshift=-.75\dp\strutbox}}

\newcounter{tikzul}%
\newcommand\tikzul[1][]{%
    \begingroup
    \global\tikzullinewidth\linewidth
    \def\tikzulsetting{[#1]}%
    \stepcounter{tikzul}%
    \settextarea
    \zlabel{tikzul-begin-\thetikzul}%
    \tikz[overlay,remember picture,tikzul] \coordinate (tikzul-\thetikzul) at (0,0);% Modified \tikzmark macro
    \ifnum\zref@extract{tikzul-begin-\thetikzul}{abspage}=\zref@extract{tikzul-end-\thetikzul}{abspage}
    \else
        \AtBeginShipoutNext{\tikzul@endpage{#1}}%
    \fi
    \bgroup
    \def\par{\ifhmode\unskip\fi\egroup\par\@ifnextchar\noindent{\noindent\tikzul[#1]}{\tikzul[#1]\bgroup}}%
    \aftergroup\endtikzul
    \let\@let@token=%
}

\newlength\tikzullinewidth

\def\tikzul@endpage#1{%
\setbox\AtBeginShipoutBox\hbox{%
\box\AtBeginShipoutBox
\hbox{%
\begin{tikzpicture}[overlay,remember picture,tikzul]
\draw[#1]
    let \p1 = (tikzul-\thetikzul), \p2 = ([xshift=\tikzullinewidth+\@totalleftmargin]textarea.south west) in
    \ifdim\dimexpr\y1-\y2<.5\baselineskip
        (\x1,\y1) -- (\x2,\y1)
    \else
        let \p3 = ([xshift=\@totalleftmargin]textarea.west) in
        (\x1,\y1) -- +(\tikzullinewidth-\x1+\x3,0)
        % (\x3,\y2) -- (\x2,\y2)
        (\x3,\y1)
       \myloop{\y1-\y2+.5\baselineskip}{%
           ++(0,-\baselineskip) -- +(\tikzullinewidth,0)
       }%
    \fi
;
\end{tikzpicture}%
}}%
}%

\def\endtikzul{%
    \zlabel{tikzul-end-\thetikzul}%
    \ifnum\zref@extract{tikzul-begin-\thetikzul}{abspage}=\zref@extract{tikzul-end-\thetikzul}{abspage}
    \begin{tikzpicture}[overlay,remember picture,tikzul]
        \expandafter\draw\tikzulsetting
            let \p1 = (tikzul-\thetikzul), \p2 = (0,0) in
            \ifdim\y1=\y2
                (\x1,\y1) -- (\x2,\y2)
            \else
                let \p3 = ([xshift=\@totalleftmargin]textarea.west), \p4 = ([xshift=-\rightmargin]textarea.east) in
                (\x1,\y1) -- +(\tikzullinewidth-\x1+\x3,0)
                (\x3,\y2) -- (\x2,\y2)
                (\x3,\y1)
                \myloop{\y1-\y2}{%
                    ++(0,-\baselineskip) -- +(\tikzullinewidth,0)
                }%
            \fi
        ;
    \end{tikzpicture}%
    \else
    \settextarea
    \begin{tikzpicture}[overlay,remember picture,tikzul]
        \expandafter\draw\tikzulsetting
            let \p1 = ([xshift=\@totalleftmargin,yshift=-.5\baselineskip]textarea.north west), \p2 = (0,0) in
            \ifdim\dimexpr\y1-\y2<.5\baselineskip
                (\x1,\y2) -- (\x2,\y2)
            \else
                let \p3 = ([xshift=\@totalleftmargin]textarea.west), \p4 = ([xshift=-\rightmargin]textarea.east) in
                (\x3,\y2) -- (\x2,\y2)
                (\x3,\y2)
                \myloop{\y1-\y2}{%
                    ++(0,+\baselineskip) -- +(\tikzullinewidth,0)
                }
            \fi
        ;
    \end{tikzpicture}%
    \fi
    \endgroup
}

\def\myloop#1#2#3{%
    #3%
    \ifdim\dimexpr#1>1.1\baselineskip
        #2%
        \expandafter\myloop\expandafter{\the\dimexpr#1-\baselineskip\relax}{#2}%
    \fi
}

\makeatother

\begin{document}
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[green]{text text text text text text text text text
text text text text text text text text text text text
text text text text 
text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
\end{document}
  • 1
    Imho the code is buggy. When page breaks are involved it advances \pgf@picture@serial@count without creating a suitable pgfid. In the original code it only works by chance as the following uses of \tikzul hides that the count was off temporarly. – Ulrike Fischer Oct 18 '16 at 9:11
  • Is there an easy way to get this fixed? – robert Oct 18 '16 at 9:56
  • 1
    I didn't find one, I can see that the value is wrong. But as quite a lot things happens during shipout and there is a lot of code, it is difficult to see what happens exactly. – Ulrike Fischer Oct 18 '16 at 10:02
  • I did suggest that since the original .sty file has no licence statement, that it ought not be reposted here and similarly for my patched version. (Strictly speaking, even downloading and using it is probably illegal - never mind patching it - but I'm assuming it wouldn't be on GIT Hub in that case.) – cfr Oct 20 '16 at 22:41
  • Have you looked into \hl{} of the soul package? It is not Tikz, but at least it does highlight correctly over page breaks. You have to make sure that special commands are inside a \mbox{} or you are using \soulregister\command7 (e.g. \soulregister\cite7) for all special commands that may appear in the preamble as described in How to make hl highlighting to automatically place incompatible commands in \mbox? – Matthias Arras Oct 22 '16 at 15:49
5

This possibly partially satisfies the criteria for your third choice. Hopefully, somebody can come up with something better - at least something which definitely fully satisfies the criteria for your third choice, for example.

I started by downloading Martin Scharrer's code from https://bitbucket.org/martin_scharrer/tikzpagelayers.

Out-of-the-box, I could only get 1 of the 3 test files to work as I think they are meant to. Moreover, using the package with your example didn't annotate anything. It did compile, which is possibly an improvement but not much consolation.

So I fiddled a little.

I am not clear what the licence for the code is at all. Hence, I don't want to post a modified version of the package here. However, I hope that posting a patch will not violate anything.

If you think it does - especially if you are Martin - or if you are Martin and you simply object for any reason at all or no reason whatsoever, please let me know and I will delete this answer.

Here's a patch, then. It should be applied in the usual way.

--- tikzpagelayers.sty  2011-12-23 16:23:20.000000000 +0000
+++ tikzpagelayers-xxx.sty  2016-10-20 02:13:46.161497923 +0100
@@ -1,5 +1,5 @@

-\ProvidesPackage{tikzpagelayers}[2011/09/15 v1.0 Draw with TikZ before or behind text on the page]
+\ProvidesPackage{tikzpagelayers-xxx}[2016/10/20 v1.0 Hacked version of tikzpagelayers, a package to draw with TikZ before or behind text on the page]
 \RequirePackage{tikzpagenodes}
 \RequirePackage{atbegshi}
 \usetikzlibrary{backgrounds,calc}
@@ -112,6 +112,21 @@
               (\x3,\y2+\@tplyshift) -- (\x2,\y2+\@tplyshift)
           \fi
       ;
+  % add code to add stuff to end of current page
+  \else
+      \draw [#1] let \p1 = (tplbegin), \p2 = (current page text area.south east), \p3 = (current page text area.south west), \p4 = (current page text area.north east) in
+          \ifdim\y1=\y2
+              (\x1,\y1+\@tplyshift) -- (\x2,\y2+\@tplyshift)
+          \else
+              (\x1,\y1+\@tplyshift) -- (\x4,\y1+\@tplyshift)
+              (\x3,\y1+\@tplyshift)
+              \tikzul@loop{\y1-\y2}{%
+                  ++(0,-\baselineskip) -- +(\tikzullinewidth,0)
+              }{}
+              (\x3,\y2+\@tplyshift) -- (\x2,\y2+\@tplyshift)
+          \fi
+      ;
+      \PackageWarning{tikzpagelayers-xxx: Annotation not continued over page break!!}%
   \fi
 }

Then the following document

\documentclass[twoside,11pt]{book}
\usepackage{tikzpagelayers-xxx}

\begin{document}
text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[red]{text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text}
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[green]{text text text text text text text text text
text text text text text text text text text text text
text text text text
text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
\end{document}

produces underlining in red for the first instance, which is fine, and underlining to the end of the page in green for the second, which is less fine.

partial underlining

A warning is written for the second instance

\AtBeginShipoutBox=\box40


Package tikzpagelayers-xxx: Annotation not continued over page break!! Warning:
  on input line 188.
  • I hope somebody comes up with a real answer to this question .... – cfr Oct 20 '16 at 1:35
  • @robert I'm hoping that somebody might use the link to the GIT code and fix it properly - maybe even Martin :-). – cfr Oct 20 '16 at 14:45
  • cfr: Your downloaded bitbucket version has one big drawback: it does not keep increased paragraph spacing into account. See the top of my openingspost. Is there an easy way to fix? – robert Oct 20 '16 at 19:33
  • @robert Sorry. This is well beyond my ken. You need somebody who understands this stuff ;). – cfr Oct 20 '16 at 23:27
2
+200

The question that you link to was inspired by an old blog post of mine on ConTeXt bar mechanism. For what it is worth, ConTeXt bar mechanism works correctly across page breaks. Here is an example which uses a slightly modified version of Martin's example:

\setuppagenumbering[alternative=doublesided]
\setuplayout[cutspace=2in,backspace=1in,width=fit]
\setupbar [underbar][rulethickness=1pt]
\definebar[redbar]  [underbar][color=red]
\definebar[greenbar][underbar][color=green]

\definebar[highlight]
          [
            order=background,
            rulethickness=2.5,
            offset=1.25,
            continue=yes,
            color=yellow,
          ]
\starttext
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text \redbar{text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum.
\highlight{Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.}
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
test

\startnarrower
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text \redbar{text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
\stopnarrower

\startnarrower
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
\startnarrower
text text text text text text text text text text text
text text \greenbar{text text text text text text text text text
text text text text text text text text text text text
text text text text 
text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
\stopnarrower
text text text text text text text text text text text
text text text text text text text text text text text
\stopnarrower


{\switchtobodyfont[big]
new text text text text text text text text text text text
text text \redbar{first text text text text text text text text
teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt
teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt
teXt teXt teXt last} teXt teXt teXt teXt teXt teXt teXt
text text text text text text text text text text text
text text text text text text text text text text text

text text text text text text text text text text text
text text text text text text text text text text text
text text \redbar{text text text text text text text text text
text text\par\noindent text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
\endgraf}

\startitemize[packed]
    \item \underbar{test test test} test
    \item test \underbar{test test test} test
    \item aa \underbar{test test test} test
    \item b \underbar{test test 
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
        test} test
\stopitemize

text text text text text text text text text text text
text text text text text text text text text text text
text text \redbar{text text text text text text text text text
text text

 text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text

text text text text text text text text text text text
text text \redbar{text text text text text text text text text
text text

text text text text text text text text text
\par text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text

\stoptext

which gives enter image description here

  • @cfr: Oops...thanks! I was trying to get the image right and must have pressed back instead of edit. I deleted the other post. – Aditya Oct 23 '16 at 9:00

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.