使用 etoolbox 代替 ifthen

使用 etoolbox 代替 ifthen

我试图意识到etoolbox而不是ifthen

我目前所拥有的...

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

答案1

没有功能的解决方案etoolbox,只有传统\newif\ifodd

请不要使用\isodd{\thepage}etc。→\thepage可以扩展为“任何内容”,因此\isodd奇数测试可能会失败。如果您想要名为 foo 的计数器的文字数值,请使用\value{foo}

而且我看了才知道etoolbox,没有\isodd宏,宏就是调用的\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}

在此处输入图片描述

答案2

我很乐意推荐使用etoolbox功能而不是\ifthen。例如,你可以利用\ifnumodd可扩展的事实来简化代码。

注意,\value{page}而不是\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}

在此处输入图片描述

相关内容