1

I have an outstanding post about a TiKZ underliner (which has a bounty running for the TikZ 'pro' who find the solution). The only thing I need to solve is to add an xshifter of \@totalleftmargin to the actual drawing to avoid this:

enter image description here

So I tried to hack this in, but I have some issues:

If you run the following MWE (stripped down highlighter) 2 runs, you get the following output. So I have the following questions:

  • Why I am loosing the value of \MARIGN? I at least expect 2 times retrieve 1.8 mm (the last set value).
  • The solution should contain some form of LIFO/FIFO-stacking to pop up the correct value. How can I create this?

Any suggestions?

MARGINPRINT: SET Margin: 0.96571 mm
MARGINPRINT: SET Margin: 1.81557 mm
MARGINPRINT: DO THE ACTUAL DRAWING: RETRIEVE Margin: 0 mm
MARGINPRINT: DO THE ACTUAL DRAWING: RETRIEVE Margin: 0 mm

MWE

\documentclass[twoside,11pt]{book}

\usepackage{tikzpagenodes}
\usepackage{atbegshi}
\usetikzlibrary{backgrounds,calc}
\usepackage{tikz}
\usepackage{zref-abspos}

\makeatletter
\newcommand*{\printlength}[1]{%
  \strip@pt\dimexpr2.54\dimexpr(#1)/72\relax\relax\space mm%
}

\newlength{\LEFTMARGIN}

\newcommand\tikzul[2][]{%
    \setlength{\LEFTMARGIN}{\@totalleftmargin}
    \typeout{MARGINPRINT: SET Margin:
             \printlength{\LEFTMARGIN}}
    \begingroup
    \tikzpagelayer{\@tikzul{}}%
    #2%
    \endtikzpagelayer
    \endgroup
}

\def\@tikzul#1{%
\typeout{MARGINPRINT: DO THE ACTUAL DRAWING: RETRIEVE Margin:
             \printlength{\LEFTMARGIN}}    
    % DO THE ACTUAL DRAWINNG
}

\newenvironment{tikzpagelayer}[2][foreground]{%
    \stepcounter{tikzpagemark}%
    \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{\stoptikzpagelayer}[1]{%
    \tikzpagemark{@#1-end}%
}%

\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
}

\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);%
}

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

\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};
            \begin{pgfonlayer}{background}
            \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
}

\makeatletter
    \begin{document}
    \begin{itemize}
    \item \tikzul[green]{Underline this.}
    \item Test:
        \begin{itemize}
            \item \tikzul[green]{Underline this also.}
        \end{itemize}
    \end{itemize}
    \end{document}
  • unrelated to the question but your tikzul command will add spurious white space because of the missing % at ends of lines – David Carlisle Oct 21 '16 at 20:05
  • \strip@pt\dimexpr0.35146\dimexpr(#1)\relax\relax\space mm will probably give a better value. – egreg Oct 21 '16 at 20:11
3

You want to pass the value not just a reference that can go out of scope:

MARGINPRINT: SET Margin: 0.96571 mm
MARGINPRINT: SET Margin: 1.81557 mm
MARGINPRINT: DO THE ACTUAL DRAWING: RETRIEVE Margin: 0.96571 mm
MARGINPRINT: DO THE ACTUAL DRAWING: RETRIEVE Margin: 1.81557 mm

is produced by

\documentclass[twoside,11pt]{book}

\usepackage{tikzpagenodes}
\usepackage{atbegshi}
\usetikzlibrary{backgrounds,calc}
\usepackage{tikz}
\usepackage{zref-abspos}

\makeatletter
\newcommand*{\printlength}[1]{%
  \strip@pt\dimexpr2.54\dimexpr(#1)/72\relax\relax\space mm%
}

\newlength{\LEFTMARGIN}

\newcommand\tikzul[2][]{%
    \setlength{\LEFTMARGIN}{\@totalleftmargin}
    \typeout{MARGINPRINT: SET Margin:
             \printlength{\LEFTMARGIN}}%%%
    \begingroup
    \edef\tmp{%
      \noexpand\tikzpagelayer{\noexpand\@tikzul{\the\@totalleftmargin}{}}}%
    \tmp
    #2%
    \endtikzpagelayer
    \endgroup
}

\def\@tikzul#1#2{%
\LEFTMARGIN#1\relax
\typeout{MARGINPRINT: DO THE ACTUAL DRAWING: RETRIEVE Margin:
             \printlength{\LEFTMARGIN}}%%%%
    % DO THE ACTUAL DRAWINNG
}

\newenvironment{tikzpagelayer}[2][foreground]{%
    \stepcounter{tikzpagemark}%
    \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{\stoptikzpagelayer}[1]{%
    \tikzpagemark{@#1-end}%
}%

\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
}

\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);%
}

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

\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};
            \begin{pgfonlayer}{background}
            \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
}

\makeatletter
    \begin{document}
    \begin{itemize}
    \item \tikzul[green]{Underline this.}
    \item Test:
        \begin{itemize}
            \item \tikzul[green]{Underline this also.}
        \end{itemize}
    \end{itemize}
    \end{document}

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.