背景框的实例随着每个章节/函数调用而不断增加

背景框的实例随着每个章节/函数调用而不断增加

我正在使用以下 MWE 创建 PDF,当我编辑创建的 PDF 时,它显示随着每个章节或 \chapframe 调用,框的实例数量不断增加。这也可以通过更改不透明度来实现,并且每个章节框的颜色都变暗。

有什么方法可以解决这个问题,以便每个图形元素只放置一次?

MWE 来自这里

\documentclass{book}
\usepackage[
  scale=1,
  angle=0,
  opacity=1,
  contents={}
]{background}
\usetikzlibrary{calc}
\usepackage{lipsum}

\pagestyle{plain}

% auxiliary counter
\newcounter{chapshift}
\addtocounter{chapshift}{-1}

% the list of colors to be used (add more if needed)
\newcommand\BoxColor{%
  \ifcase\thechapshift blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi}

% the main command; the mandatory argument sets the color of the vertical box
\newcommand\ChapFrame{%
  \AddEverypageHook{%
    \ifodd\value{page}
      \backgroundsetup{contents={%
        \begin{tikzpicture}[overlay,remember picture]
          \node[
            fill=\BoxColor,
            inner sep=0pt,
            rectangle,
            text width=2cm,
            text height=4cm,
            align=center,
            anchor=north east
          ] 
          at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $) 
          {\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
              \raggedright\textcolor{black}{\scshape\leftmark}}}};
        \end{tikzpicture}%
      }%
    }  
    \else
      \backgroundsetup{contents={%
        \begin{tikzpicture}[overlay,remember picture]
        \node[
          fill=\BoxColor,
          inner sep=0pt,
          rectangle,
          text width=2cm,
          text height=4cm,
          align=center,
          anchor=north west
        ] 
        at ($ (current page.north west) + (-0cm,-2*\thechapshift cm) $) 
        {\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
            \raggedright\textcolor{black}{\scshape\leftmark}}}};
        \end{tikzpicture}%
      }%
    }  
    \fi
  \BgMaterial}%
  \stepcounter{chapshift}%
}

% redefinition of \chaptermark to contain only the title
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}} 

\begin{document}

\chapter[intro]{Introduction}
\ChapFrame
\lipsum[1-7]

\chapter{Results}
\ChapFrame
\lipsum[1-7]

\chapter{Discussion}
\ChapFrame
\lipsum[1-7]

\end{document}

堆放的箱子移到一边

相关内容