This is the continuation of the question Drawing tikz line in the margin for multiple pages
The code provided by John Kormylo allows to draw a margin decoration in front of a portion of text surrounded by an environment defined in the code. It uses everypage package and seems to work well. Moreover, the environment is breakable, which is very interesting because it can be nested with package which provide breakable boxes. For instance, the decoration can extend on several pages and in the same time, can include breakable boxes (theorem, paragraphs, citations...).
As I mentionned it in the comments, I'd like to use several different types of decoration for different purposes. I tried to duplicate the code, but even when changing all the names (included counters and function chekmarkpage), it doesn't work. In fact, it draws both decorations at the same time. Plus, some environments are ignored...
Here is the code I am using. If anyone could help me to get the things work, I would appreciate :).
\documentclass[a4paper,12pt]{book}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc,tikzmark}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{everypage}
\makeatletter
\newcommand{\checkmarkpageredenv}[4]% #1 = tikzmark label, #2 = less, #3 = equal, #4 = greater
{\@ifundefined{save@pt@#1}{#2}{%
\edef\markid{\csname save@pt@#1\endcsname}%
\edef\markpage{\csname save@pg@\markid\endcsname}%
\ifnum\thepage<\markpage\relax #2%
\else
\ifnum\thepage=\markpage\relax #3%
\else #4%
\fi
\fi}%
}
\makeatother
\newcounter{redenvid}
\newcounter{redenvdone}
\newenvironment{redenv}{\par\tikzmark{begin\theredenvid}\ignorespaces}%
{\par\tikzmark{end\theredenvid}\stepcounter{redenvid}\ignorespaces}
\newcommand{\drawredenv}{\checkmarkpageredenv{begin\theredenvdone}{}%
{\begin{tikzpicture}[remember picture,overlay]
\path ({pic cs:begin\theredenvdone}-| current page text area.west)
++(0pt,\ht\strutbox) coordinate(A);
\checkmarkpageredenv{end\theredenvdone}%
{\path (current page text area.south west) ++(0pt,-\dp\strutbox)
coordinate(B);}%
{\path ({pic cs:end\theredenvdone}-| current page text area.west)
++(0pt,\ht\strutbox) coordinate(B);}%
{}% this should not happen
\fill[red] ($(A) + (-.1cm,0pt)$) rectangle ($(B) + (-.9cm,0pt)$);
\end{tikzpicture}}%
{\begin{tikzpicture}[remember picture,overlay]
\coordinate (A) at (current page text area.north west);
\checkmarkpageredenv{end\theredenvdone}%
{\path (current page text area.south west) ++(0pt,-\dp\strutbox)
coordinate(B);}%
{\path ({pic cs:end\theredenvdone}-| current page text area.west)
++(0pt,\ht\strutbox) coordinate(B);}%
{}% this should not happen
\fill[red] ($(A) + (-.1cm,0pt)$) rectangle ($(B) + (-.9cm,0pt)$);
\end{tikzpicture}}%
\checkmarkpageredenv{end\theredenvdone}{}%
{\stepcounter{redenvdone}\drawredenv}%
{}% this should not happen
}
\AddEverypageHook{\drawredenv}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\newcommand{\checkmarkpageblueenv}[4]% #1 = tikzmark label, #2 = less, #3 = equal, #4 = greater
{\@ifundefined{save@pt@#1}{#2}{%
\edef\markid{\csname save@pt@#1\endcsname}%
\edef\markpage{\csname save@pg@\markid\endcsname}%
\ifnum\thepage<\markpage\relax #2%
\else
\ifnum\thepage=\markpage\relax #3%
\else #4%
\fi
\fi}%
}
\makeatother
\newcounter{blueenvid}
\newcounter{blueenvdone}
\newenvironment{blueenv}{\par\tikzmark{begin\theblueenvid}\ignorespaces}%
{\par\tikzmark{end\theblueenvid}\stepcounter{blueenvid}\ignorespaces}
\newcommand{\drawblueenv}{\checkmarkpageblueenv{begin\theblueenvdone}{}%
{\begin{tikzpicture}[remember picture,overlay]
\path ({pic cs:begin\theblueenvdone}-| current page text area.west)
++(0pt,\ht\strutbox) coordinate(C);
\checkmarkpageblueenv{end\theblueenvdone}%
{\path (current page text area.south west) ++(0pt,-\dp\strutbox)
coordinate(D);}%
{\path ({pic cs:end\theblueenvdone}-| current page text area.west)
++(0pt,\ht\strutbox) coordinate(D);}%
{}% this should not happen
\fill[blue] ($(C) + (-1.0cm,0pt)$) rectangle ($(D) + (-1.8cm,0pt)$);
\end{tikzpicture}}%
{\begin{tikzpicture}[remember picture,overlay]
\coordinate (C) at (current page text area.north west);
\checkmarkpageblueenv{end\theblueenvdone}%
{\path (current page text area.south west) ++(0pt,-\dp\strutbox)
coordinate(D);}%
{\path ({pic cs:end\theblueenvdone}-| current page text area.west)
++(0pt,\ht\strutbox) coordinate(D);}%
{}% this should not happen
\fill[blue] ($(C) + (-1.0cm,0pt)$) rectangle ($(D) + (-1.8cm,0pt)$);
\end{tikzpicture}}%
\checkmarkpageblueenv{end\theblueenvdone}{}%
{\stepcounter{blueenvdone}\drawblueenv}%
{}% this should not happen
}
\AddEverypageHook{\drawblueenv}
\begin{document}
\newpage
\thispagestyle{empty}
Next line begins the rule for BLUE ENV:
\begin{blueenv}
\lipsum[2]
\end{blueenv}
Line above ends the rule for BLUE ENV.
Two on one page for RED ENV:
\begin{redenv}
\lipsum[3]
\end{redenv}
One on three pages for BLUE ENV:
\begin{blueenv}
\lipsum[4-12]
\end{blueenv}
That's all, folks.
\end{document}