![当前最先进的 TikZ Underliner](https://linux22.com/image/342652/%E5%BD%93%E5%89%8D%E6%9C%80%E5%85%88%E8%BF%9B%E7%9A%84%20TikZ%20Underliner.png)
更新:
赏金期已结束(猜猜怎么着?我把功劳归功于错误的人(对不起 CFR),但感谢 Aditya 的贡献。请为 CFR 的答案点赞!!)。下面找到我当前的实现 + MWE。如果您对其进行了改进,请在此处发布参考作为答案/评论这适用于列表环境和直到分页符。
致谢/贡献者:
- 马丁·施拉雷: 原创概念
- 疾病控制与预防中心:他的回答直到页面末尾都有下划线+警告(见下文)
- 大卫·卡莱尔:这帮助我让 Martin/CFR 代码在列表环境中运行回答我的问题
- 我:-): 用于添加代码来处理列表环境(totalleftmargin)并在分页符警告中打印当前页面。
它看起来像什么(请注意,它也支持突出显示等,请参见下面的包装/第二张屏幕截图)
平均能量损失
\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
}
开始原始问题消息
我正在寻找一位 TikZ 专家,可以就以下问题提供一些建议。由于我对 TikZ 还比较陌生,而且这看起来是一个高级主题,因此非常希望得到帮助。
语境
我正在使用功能丰富的 TikZ Highlighter/下划线代码片段原创开发由 Martin Schrarrer 发布。发布了其他不同的“分叉”,例如这个。
问题
原始帖子/代码讨论了分页符(另见下面的屏幕截图),但它看起来没有正确实现。如果此 TikZ 代码片段在分页符处使用,则编译(第二次运行)将失败和:
! Package tikz Error: Giving up on this path. Did you forget a semicolon?.
随着我的文档越来越长,并且此代码片段的使用越来越多,因此出现了更多故障。到目前为止,我通过引入额外的换行符或在分页符处停止/重新启动此代码来处理此问题。这变得难以管理。
演示失败的最小工作示例
我在下面添加了一个 MWE。序言未受影响(原始 Martin 代码)。它在第二个编译阶段中断。
分析
Ulrike 确实在下面发布了一些评论,指出代码确实包含一些错误。正如 Ulrike 所报告的,Martin 最初的 MWE 分页符仅在他的 MWE 情况下有效(因为特殊情况)。
我正在寻找
需要进行哪些更改以避免崩溃,并按优先顺序进行:
- 正在处理双面书籍分页符(左/右边距不同)
- 确实会继续分页,但无法正确处理两面书籍文档(最好也记录下来)
- 不会跨越分页符继续,而是在页面末尾停止。将警告写入日志。
- 不执行任何操作。如果中间有分页符,则不会出现 TikZ 输出,仅会出现带有页码等的日志语句。
MWE 生成(第二次运行)包 tikz 错误:放弃此路径。您忘记了分号吗?
\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
这可能部分满足了你的第三选择的标准。希望有人能想出更好的办法 - 至少是完全满足你的第三选择标准的方法。
我首先从以下网址下载了 Martin Scharrer 的代码:https://bitbucket.org/martin_scharrer/tikzpagelayers。
开箱即用,我只能让 3 个测试文件中的 1 个按预期工作。此外,使用带有示例的包没有注释任何内容。它确实编译了,这可能是一个改进,但并没有太多安慰。
所以我稍微摆弄了一下。
我完全不清楚代码的许可证是什么。因此,我不想在这里发布该软件包的修改版本。但是,我希望发布补丁不会违反任何规定。
如果您认为确实如此 - 特别是如果您是马丁 - 或者如果您是马丁并且您只是出于任何原因或毫无理由地反对,请告诉我,我将删除这个答案。
那么,这是一个补丁。它应该以通常的方式应用。
--- 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
}
然后是以下文件
\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}
第一次使用红色下划线,效果很好,第二次使用绿色下划线到页面末尾,效果不太好。
第二次将受到警告
\AtBeginShipoutBox=\box40
Package tikzpagelayers-xxx: Annotation not continued over page break!! Warning:
on input line 188.
答案2
您链接到的问题受到我关于 ConTeXt 条形机制的一篇旧博客文章的启发。值得一提的是,ConTeXt 条形机制在分页符之间正常工作。以下是使用 Martin 示例的略微修改版本的示例:
\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