堆叠长度并弹出

堆叠长度并弹出

我有一个优秀的发布有关 TiKZ 底线的帖子(为找到解决方案的 TikZ“专业人士”提供悬赏)\@totalleftmargin。我唯一需要解决的是在实际绘图中添加一个 xshifter以避免这种情况:

在此处输入图片描述

因此我尝试破解它,但遇到了一些问题:

如果您运行以下 MWE(精简版荧光笔)2 次,您将获得以下输出。所以我有以下问题:

  • 为什么我会丢失 \MARIGN 的值?我至少希望 2 次检索 1.8 毫米(最后设定的值)。
  • 解决方案应包含某种形式的 LIFO/FIFO 堆叠,以弹出正确的值。我该如何创建它?

有什么建议么?

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

平均能量损失

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

答案1

您想要传递的值而不仅仅是超出范围的引用:

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

由制作

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

相关内容