测试 tikzmark 是否存在于同一页面上

测试 tikzmark 是否存在于同一页面上

抱歉,网站充斥着 tikzmark 问题... :)

我知道\iftikzmark我可以测试给定内容是否tikzmark存在。有没有办法tikzmark在同一页面上测试特定内容是否存在?

使用案例:我正在尝试设计宏,可以将其插入文本中的任何位置,并在它们之间画一条线——一个\linestart和一个\linefinish命令。但是,当\linefinish命令恰好落在后续页面上时,该线将被绘制到命令\linestart在上一页的位置。我宁愿让它“做其他事情”,在这里,只是为了说明的目的,画一个黑色的方块——或者,理想情况下“指向”页面上另一个标记所在的方向。

在下面的代码中,我尝试构建我所设想的内容,\iftikzmark但没有成功。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newcounter{tikzmkstart}\setcounter{tikzmkstart}{0} % 
\newcounter{tikzmkfinish}\setcounter{tikzmkfinish}{0} % 

\newcommand{\linestart}{%
\stepcounter{tikzmkfinish}\iftikzmark{b\thetikzmkfinish}{% tikzmark exists on same page
\stepcounter{tikzmkstart}\tikzmarknode{a\thetikzmkstart}{\vphantom{x}}\addtocounter{tikzmkfinish}{-1}}
{% tikzmark doesn't exist on page
\begin{tikzpicture} \draw [line width=4] (0,0) -- (.1,0); \end{tikzpicture}\addtocounter{tikzmkfinish}{-1}}}% black square if linefinish on next page

\newcommand{\linefinish}{%
\iftikzmark{a\thetikzmkstart}{% tikzmark exists on same page
\stepcounter{tikzmkfinish}\tikzmarknode{b\thetikzmkfinish}{\vphantom{x}}\begin{tikzpicture}[remember picture]  \draw[overlay] (a\thetikzmkstart.west) -- (b\thetikzmkfinish.west); \end{tikzpicture}}
{% tikzmark doesn't exist on page
\begin{tikzpicture} \draw [line width=4] (0,0) -- (.1,0); \end{tikzpicture}}}

\begin{document} 
Text \linestart text text 

Text text text 
%\newpage

Text text \linefinish text
\end{document}

我非常欢迎任何有关如何完成此类事情的其他想法!


更新:根据@marmot 的建议答案,我现在想出了以下办法。但是,我不知道如何让这些线“指向”分页符...

\documentclass{article}
\usepackage{refcount}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcounter{tmp}
\newcommand\tikzmark[1]{%
  \tikz[overlay,remember picture] \node (#1) {};}

\newcommand\linestart{%
  \stepcounter{tmp}%
  \tikzmark{a}\label{a\thetmp}%
  \ifnum\getpagerefnumber{a\thetmp}=\getpagerefnumber{b\thetmp} \else
  \begin{tikzpicture}[overlay, remember picture]
    \draw [thick]     
      (a.west) -- (b.west); 
  \end{tikzpicture}%
  \fi%
}


\newcommand\linefinish{%
\tikzmark{b}\label{b\thetmp}
  \ifnum\getpagerefnumber{a\thetmp}=\getpagerefnumber{b\thetmp}
  \begin{tikzpicture}[overlay, remember picture]
    \draw [thick]
      (a.west) -- (b.west); 
  \end{tikzpicture}%
  \else
  \begin{tikzpicture}[overlay, remember picture]
    \draw [thick]
      (a.west) -- (b.west); 
  \end{tikzpicture}%
  \fi
}

\newcommand\Squ[1]{\linestart#1\linefinish}

\begin{document}

Text \linestart text text

Text text text

Text text \linefinish text

Text \linestart text text

Text text text
\newpage

Text text \linefinish text

\end{document}

答案1

我明白有条件的意义\iftikzmarkonpage,所以我把它添加到tikzmark包(下载tikzmark.dtx并运行tex tikzmark.dtx以获取最新版本)。这引入了一个新的条件。\iftikzmarkoncurrentpage{<tikzmark name>}这解决了在需要时在两个页面上绘制线条的问题。

\documentclass{article}
\usepackage[a6paper]{geometry}
%\url{https://tex.stackexchange.com/q/483547/86}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{tikzmark}
\newcounter{tikzmarklines}\setcounter{tikzmarklines}{0} % 

\tikzset{
  tikzmark prefix=prefix-,
  tikzmark suffix=-suffix
}

\newcommand{\linestart}{%
  \stepcounter{tikzmarklines}%
  \tikzmark{a\thetikzmarklines}%
  \iftikzmarkoncurrentpage{b\thetikzmarklines}%
  \else
  \begin{tikzpicture}[remember picture,overlay,next page=below]%
  \clip (current page text area.south west) rectangle (current page text area.north east);
  \draw[line width=4] (pic cs:a\thetikzmarklines) -- (pic cs:b\thetikzmarklines);
  \end{tikzpicture}%
  \fi
}

\newcommand{\linefinish}{%
  \tikzmark{b\thetikzmarklines}%
  \begin{tikzpicture}[remember picture,overlay,next page=below]%
  \clip (current page text area.south west) rectangle (current page text area.north east);
  \draw[line width=1] (pic cs:a\thetikzmarklines) -- (pic cs:b\thetikzmarklines);
  \end{tikzpicture}%
  }

\begin{document} 
Text \linestart text text 

Text text text 

Text text \linefinish text

\newpage

Text text text 

Text \linestart text text 

Text text text 
\newpage

Text text text

Text text \linefinish text
\end{document}

页间线

相关内容