I'm trying to use TikZ to make fancier frames. I'm using the clever \tikzmark
thing that Andrew Stacey wrote to put nodes at the top left and bottom right of the body of an environment.
Then I use these nodes to draw funky decorations. I'm sure I'm reinventing the wheel, but I'm learning so it's all good. I know what I want to do is possible because mdframed
and listings
both manage it, but their code is hard for me to read and it's not clear what the relevant part is…
Now, this works fine until the environment spans a page. Then the nodes are on different pages, and when the end code of the environment comes to draw the decorations, it can't find the top left node (because its been shipped out on the last page).
So what I'd like to do is have a different set of behaviours for when the environment is broken across a page. What are the simplest ways to test when a paragraph is broken like this?
Best Answer
You can use
zref
with theabspos
anduser
modules (e.g. load thezref-abspos
andzref-user
packages) and add a\zlabel{labelname}
to the\tikzmark
at the beginning of the paragraph as well one at the end of the paragraph. If you using an environment simply place one atbegin
andend
. Then you can check at the end of the environment if the absolute page numbers of both are identical or not. For this you can use:(Note: Maybe
\zref@extractdefault
is better because you can provide a default value used for the first runs when the label is not set yet.)See also my answer to How to draw text-anchored tikz line below text instead of above? where I do something very similar!
Here a solution I now come up with. It draws the first border half in the begin-macro if the end-macro is not at the same page. Also the case where the environment spans more than two pages, e.g. starts at page 1 and ends at page 3, is covered by extra code.
(The frame is not displayed fully right in the picture because of the low resolution etc.
Click to enlarge)