4

Apologies for flooding the site with tikzmark problems ... :)

I know that with \iftikzmark I can test if a given tikzmark exists in general. Is there a way to test if a particular tikzmark exists on the same page?

Use case: I'm trying to design macros that I can insert anywhere in a text that would draw a line between them –- a \linestart and a \linefinish command. However, when the \linefinish command happens to fall on a subsequent page, the line gets drawn to where the \linestart command was on the previous page. I would rather have it instead "do something else", here, just for purpose of illustration, draw me a black square – or, ideally "point across" the page in the direction to where the other mark is sitting.

In the code below, I've tried to construct what I'm envisioning using \iftikzmark but that does not work.

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

Any ideas of how else I might accomplish something like this would be very welcome!


Update: Based on @marmot's suggestion to look at this answer, I've now come up with the following. However, I'm stuck with how to make the lines "point across" the page breaks ...

\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}
10
  • The way you write the questions may indicate that you are about to construct a tikzmar version of something like this answer. The basic trick is that you build in the page number.
    – user121799
    Apr 6 '19 at 16:51
  • @marmot Oh, you mean incorporating the pagenumber into the label for the node. Ha, that's clever.
    – jan
    Apr 6 '19 at 16:53
  • Yes, basically. If you also want the line to also run over page breaks, you may need refcount, like in Gonzalo's answer.
    – user121799
    Apr 6 '19 at 16:56
  • 1
    tikzmark already saves the page number on which it is defined (see tex.stackexchange.com/q/79121/86 for a use of this, and look at next page in the documentation). There isn't a current direct interface to that information; it is stored as \save@pg@<picture id of the tikzmark> so you could do a test on that. I'm not going to attempt to get the expansions right in a comment, but something like \ifnum\csname save@pg@\pgfpictureid\endcsname=\csname save@pg@\csname save@pt@\tmk@label\endcsname\endcsname Apr 6 '19 at 17:10
  • @LoopSpace Hmm, that looks fairly complex. Would you be able to explain where I incorporate that?
    – jan
    Apr 6 '19 at 17:14
6

I can see the point of having a \iftikzmarkonpage conditional, so I've added it to the tikzmark package (download tikzmark.dtx and run tex tikzmark.dtx to get the latest version). This introduces a new conditional, \iftikzmarkoncurrentpage{<tikzmark name>}. That copes with the issue about the lines being drawn on both pages when needed.

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

lines between pages

12
  • That looks like it does what I'm after :) thank you ... and if you incorporate a conditional into the package, even better :) Although ... didn't Gonzalo's answer solve that? Why would his solution be bad?
    – jan
    Apr 6 '19 at 18:28
  • Please feel free to revert my edit. +1
    – user121799
    Apr 6 '19 at 18:29
  • @marmot, I like the gifs ;)
    – jan
    Apr 6 '19 at 18:30
  • 1
    @jan convert -density 300 -delay 144 -loop 0 -alpha remove multipage.pdf animated.gif, see tex.stackexchange.com/a/136919/121799.
    – user121799
    Apr 6 '19 at 18:31
  • 1
    @jan You can use tikz-pagenodes, which has the current page text area node. And I think you are doing it right because in their present form the questions and answers are useful for many, which they are not if you do some highly specialized things. (Will be off now.)
    – user121799
    Apr 6 '19 at 18:37

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.