5

I try to realize this with etoolbox instead of ifthen.

What I have so far...

\documentclass{article}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{etoolbox}
\usepackage[contents={},opacity=1,scale=1.485]{background}

\newtoggle{BgMat}
\settoggle{BgMat}{true}

\AddEverypageHook{%
  \iftoggle{BgMat}{%
    \ifboolexpe{\isodd{\thepage}}%
    {\backgroundsetup{angle=0,position={0.9\textwidth,-
    .7\textheight},%
    contents={\tikz[remember picture,overlay]{ %
    \coordinate (x) at (current page marginpar area.south east|-current page.south east);
    \draw[draw=none,fill=magenta!20]([xshift=-\textwidth]x)rectangle(current page.north west);}}}}%
    {\backgroundsetup{angle=0,position={0.9\textwidth,-
    .7\textheight},%
    contents={\tikz[remember picture,overlay]{ %
    \coordinate (x) at (current page marginpar area.south east|-current page.south east);
    \draw[draw=none,fill=orange!20](x)rectangle(current page.north east);}}}}%
    \BgMaterial
  }{%
  }
}

\usepackage{lipsum} % dummy text

\begin{document}
\lipsum[1-8]

\clearpage
\settoggle{BgMat}{false}% deactivate colored margins
\lipsum[1-8]

\clearpage
\settoggle{BgMat}{true}% activate colored margins
\lipsum[1-8]
\end{document}
  • What’s the problem? – gigabytes Dec 31 '17 at 10:17
  • @gigabytes: It does not compile, that seems the problem, but the O.P. forgot to mention it ;-) – user31729 Dec 31 '17 at 10:33
  • From a quick view point I don't see any need for etoolbox here at all (Well, I used it also some years ago before I started to learn about \newif etc. – user31729 Dec 31 '17 at 10:34
  • Especially, \thepage is not necessarily a literal number (think of roman figures!), so \isodd might fail – user31729 Dec 31 '17 at 10:37
  • 1
    A side note: There is no \isodd macro in etoolbox. You meant \ifnumodd, must likely – user31729 Dec 31 '17 at 10:55
4

A solution without etoolbox features, just traditional \newif and \ifodd.

Please do not use \isodd{\thepage} etc. → \thepage can expand to 'anything', so \isodd as test for odd numbers might fail. If you want the literal numeric value of a counter named, say foo, use \value{foo}

And I learned from reading etoolbox, that there is no \isodd macro, the macro is called \ifnumodd.

\documentclass{article}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\usepackage[contents={},opacity=1,scale=1.485]{background}


\newif\ifBGMat

\BGMattrue

\AddEverypageHook{%
  \ifBGMat
  \ifodd\value{page} \backgroundsetup{angle=0,position={0.9\textwidth,-.7\textheight},
    contents={\tikz[remember picture,overlay]{ %
        \coordinate (x) at (current page marginpar area.south east|-current page.south east);
        \draw[draw=none,fill=magenta!20]([xshift=-\textwidth]x)rectangle(current page.north west);}}}
  \else
  \backgroundsetup{angle=0,position={0.9\textwidth,-.7\textheight},
    contents={\tikz[remember picture,overlay]{ %
        \coordinate (x) at (current page marginpar area.south east|-current page.south east);
        \draw[draw=none,fill=orange!20](x)rectangle(current page.north east);}}}
  \fi
  \fi
  \BgMaterial
 }



\usepackage{lipsum} % dummy text

\begin{document}
\lipsum[1-8]

\clearpage
\BGMatfalse
\lipsum[1-8]

\clearpage
\BGMattrue
\lipsum[1-8]
\end{document}

enter image description here

4

I gladly recommend using etoolbox features rather than \ifthen. For instance, you can simplify your code using the fact that \ifnumodd is expandable.

Note \value{page} instead of \thepage.

\documentclass{article}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{etoolbox}
\usepackage[contents={},opacity=1,scale=1.485]{background}

\usepackage{lipsum} % dummy text

\newtoggle{BgMat}
\settoggle{BgMat}{true}

\AddEverypageHook{%
  \iftoggle{BgMat}
   {% BgMat true
    \backgroundsetup{
      angle=0,
      position={0.9\textwidth,-0.7\textheight},
      contents={\ifnumodd{\value{page}}{\BgPictureOdd}{\BgPictureEven}},
    }%
    \BgMaterial
   }
   {% BgMat false
   }%
}

\newcommand{\BgPictureOdd}{%
  \tikz[remember picture,overlay]{%
    \coordinate (x) at (current page marginpar area.south east|-current page.south east);
    \draw[draw=none,fill=magenta!20]([xshift=-\textwidth]x)rectangle(current page.north west);}%
}
\newcommand{\BgPictureEven}{%
  \tikz[remember picture,overlay]{%
    \coordinate (x) at (current page marginpar area.south east|-current page.south east);
    \draw[draw=none,fill=orange!20](x)rectangle(current page.north east);}%
}


\begin{document}
\lipsum[1-8]

\clearpage
\settoggle{BgMat}{false}% deactivate colored margins
\lipsum[1-8]

\clearpage
\settoggle{BgMat}{true}% activate colored margins
\lipsum[1-8]
\end{document}

enter image description here

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.