此线程解释了如何对所有页面执行此操作:如何为不同的页面设置彩色边距?
但我想知道是否可以只为某些页面的边距添加颜色?该怎么做?
因此,第 1 到第 x 页没有颜色,然后第 y 页有颜色,然后又没有颜色。
答案1
您可以定义一个pagestyle
并在需要时使用它。
\documentclass[twoside]{article}
\usepackage{tikz} % you know what this does!
\usetikzlibrary{calc}
\usepackage{fancyhdr} % put things headers and footers and we plan misuse it ;)
\usepackage{lipsum} % for sample text
\fancypagestyle{mypage}{%
\fancyhf{}
\fancyhead[LO]{%
\begin{tikzpicture}[overlay,remember picture]
\fill [color=blue] (current page.north west) rectangle
($ (current page.south west) + (1cm,0cm) $);
\end{tikzpicture}
}
\fancyhead[RE]{%
\begin{tikzpicture}[overlay,remember picture]
\fill [color=orange](current page.north east) rectangle
($ (current page.south east) + (-1cm,0cm) $);
\end{tikzpicture}
}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\begin{document}
\pagestyle{mypage} % activate colored margins
\lipsum[1-8]
\clearpage
\pagestyle{plain} % deactivate colored margins
\lipsum[1-8]
\clearpage
\pagestyle{mypage} % activate colored margins
\lipsum[1-8]
\end{document}
答案2
使用链接问题答案的设置,您可以使用条件随意激活或停用边距:
\documentclass{article}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{ifthen}
\usepackage[contents={},opacity=1,scale=1.485]{background}
\newif\ifBgMat
\AddEverypageHook{%
\ifBgMat
\ifthenelse{\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
\else
\fi}
\usepackage{lipsum} % dummy text
\BgMattrue% activate colored margins
\begin{document}
\lipsum[1-8]
\clearpage
\BgMatfalse% deactivate colored margins
\lipsum[1-8]
\clearpage
\BgMattrue% activate colored margins
\lipsum[1-8]
\end{document}
答案3
我喜欢 Gonzalo 的回答,但我更喜欢etoolbox
(而不是ifthen
,已经过时了)和eso-pic
(而不是background
)。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{etoolbox} % provides if-else-conditionals
\usepackage{eso-pic} % provides the \AddToShipoutPictureBG command which adds content to background of every page
\newcommand{\marginLeft}{%
\begin{tikzpicture}[remember picture,overlay]
\fill [magenta!20] ($ (current page.north west) + (-0.3cm,0.3cm) $) rectangle ($ (current page.south west) + (1cm,-0.3cm) $);
\end{tikzpicture}%
}
\newcommand{\marginRight}{%
\begin{tikzpicture}[remember picture,overlay]
\fill [orange!20] ($ (current page.north east) + (0.3cm,0.3cm) $) rectangle($ (current page.south east) + (-1cm,-0.3cm) $);
\end{tikzpicture}%
}
\newtoggle{margins} % default: deactivated
\AddToShipoutPictureBG{% this command adds content to background of every page
\iftoggle{margins}{% if margins-toggle is activated...
\ifnumodd{\thepage}{% if page number is odd
\marginLeft % print margin on the left side
}{% else (if page number is even)
\marginRight % print margin on the right side
}
}{ % else (if margins-toggle is deactivated)
% print nothing
}
}
\usepackage{lipsum} % dummy text
\begin{document}
\toggletrue{margins} % activate colored margins
\lipsum[1-8]
\clearpage
\togglefalse{margins} % deactivate colored margins
\lipsum[1-8]
\clearpage
\toggletrue{margins} % activate colored margins
\lipsum[1-8]
\end{document}