eso-pic
提供了\AddToShipoutPictureBG
在每一页都添加内容到后台和\AddToShipoutPictureBG*
只添加当前页到后台的功能,\ClearShipoutPicture
清除所有内容,但是没有只\ClearShipoutPicture*
清除当前页的内容的功能。
我想在除章节页之外的每个页面中显示一些内容。使用\AddToShipoutPictureBG{}
章节创建时不起作用,因为之前的内容没有被清除,只是添加了。如果我使用,\ClearShipoutPicture
我需要知道页面何时更改才能再次添加内容。我该怎么做?
我也试过background
,\NoBgThispage
但是不工作我可以使用
\backgroundsetup{contents={
\begin{tikzpicture}
...
\end{tikzpicture}
,angle=0
}}
代替\AddToShipoutPictureBG{...}
和\backgroundsetup{contents={}}
代替\ClearShipoutPicture
。结果几乎是一样的。
梅威瑟:
\documentclass[a4paper,twoside,openright]{report}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{ch1.tex}
\lipsum[1-3]\section{First section ch1}\lipsum[4-10]
\end{filecontents}
\begin{filecontents}{ch2.tex}
\lipsum[11-14]\section{First section ch2}\lipsum[15-25]
\end{filecontents}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{xifthen}
\usepackage{totcount}
\usepackage{eso-pic}
\usepackage{tikz,bm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,positioning}
\regtotcounter{sidecnt} \newcounter{sidecnt}
\colorlet{sidecolor}{red!75}
\colorlet{sidelabelcolor}{blue}
\def\vsidemargin{3.5cm}
\AddToShipoutPictureBG{%
\begin{tikzpicture}[overlay]
\pgfmathsetmacro{\pheight}{(\paperheight-2*\vsidemargin)/28.453}
\pgfmathsetmacro{\myt}{-(\vsidemargin/28.453)-(\thesidecnt-1)/\totvalue{sidecnt}*\pheight}
\pgfmathsetmacro{\myb}{-(\vsidemargin/28.453)-\thesidecnt/\totvalue{sidecnt}*\pheight}
\ifthenelse{\isodd{\value{page}}}{\def\corner{east}\def\sign{-}}{\def\corner{west}\def\sign{+}}
\fill[sidecolor] ($(current page.north \corner)+(0,\myt)$) rectangle ($(current page.north \corner)+(\sign0.75,\myb)$);
\node[font=\normalfont\sffamily\bfseries] at ($(current page.north \corner)+(\sign0.375,\myt-0.5)$) {\color{sidelabelcolor}\thechapter};
\end{tikzpicture}
}
\newcommand\importchapter[2]{
\chapter{#1}
\stepcounter{sidecnt}
%\ClearShipoutPicture* alike
\input{#2}
}
\begin{document}
\importchapter{First chapter}{ch1.tex}
\importchapter{Second chapter}{ch2.tex}
\end{document}
我不希望第 1 页和第 5 页显示背景。
答案1
您可以通过在章节页的背景上添加一个与页面大小相同的白色矩形来覆盖背景图片,而不是真正清除它,从而达到作弊的目的。显然,如果您要打印除白色以外的任何颜色的纸张,则需要匹配颜色。但希望情况并非如此,并且这将有效:
\documentclass[a4paper,twoside,openright]{report}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{ch1.tex}
\lipsum[1-3]\section{First section ch1}\lipsum[4-10]
\end{filecontents}
\begin{filecontents}{ch2.tex}
\lipsum[11-14]\section{First section ch2}\lipsum[15-25]
\end{filecontents}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{xifthen}
\usepackage{totcount}
\usepackage{eso-pic}
\usepackage{tikz,bm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,positioning}
\regtotcounter{sidecnt} \newcounter{sidecnt}
\colorlet{sidecolor}{red!75}
\colorlet{sidelabelcolor}{blue}
\def\vsidemargin{3.5cm}
\AddToShipoutPictureBG{%
\begin{tikzpicture}[overlay]
\pgfmathsetmacro{\pheight}{(\paperheight-2*\vsidemargin)/28.453}
\pgfmathsetmacro{\myt}{-(\vsidemargin/28.453)-(\thesidecnt-1)/\totvalue{sidecnt}*\pheight}
\pgfmathsetmacro{\myb}{-(\vsidemargin/28.453)-\thesidecnt/\totvalue{sidecnt}*\pheight}
\ifthenelse{\isodd{\value{page}}}{\def\corner{east}\def\sign{-}}{\def\corner{west}\def\sign{+}}
\fill[sidecolor] ($(current page.north \corner)+(0,\myt)$) rectangle ($(current page.north \corner)+(\sign0.75,\myb)$);
\node[font=\normalfont\sffamily\bfseries] at ($(current page.north \corner)+(\sign0.375,\myt-0.5)$) {\color{sidelabelcolor}\thechapter};
\end{tikzpicture}}
\newcommand\importchapter[2]{
\chapter{#1}
\stepcounter{sidecnt}
\AddToShipoutPictureBG*{%
\begin{tikzpicture}[overlay,color=white]
\fill (current page.north east) rectangle (current page.south west);
\end{tikzpicture}}
\input{#2}}
\begin{document}
\importchapter{First chapter}{ch1.tex}
\importchapter{Second chapter}{ch2.tex}
\end{document}
答案2
\newtoggle
这是我使用软件包提供的解决方案etoolbox
。@cfr 提供的解决方案很简单,使用软件包提供的命令eso-pic
,这些命令已经使用过了。但是,此解决方案仅涵盖所需部分,保留了后台可能存在的任何其他内容。我认为最好同时发布这两个答案。
\documentclass[a4paper,twoside,openright]{report}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{ch1.tex}
\lipsum[1-3]\section{First section ch1}\lipsum[4-10]
\end{filecontents}
\begin{filecontents}{ch2.tex}
\lipsum[11-14]\section{First section ch2}\lipsum[15-25]
\end{filecontents}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{xifthen}
\usepackage{totcount}
\usepackage{etoolbox} %added
\usepackage{eso-pic}
\usepackage{tikz,bm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,positioning}
\regtotcounter{sidecnt} \newcounter{sidecnt}
\colorlet{sidecolor}{red!75}
\colorlet{sidelabelcolor}{blue}
\def\vsidemargin{3.5cm}
\newtoggle{noSide} %added
\AddToShipoutPictureBG{%
\begin{tikzpicture}[overlay]
\pgfmathsetmacro{\pheight}{(\paperheight-2*\vsidemargin)/28.453}
\pgfmathsetmacro{\myt}{-(\vsidemargin/28.453)-(\thesidecnt-1)/\totvalue{sidecnt}*\pheight}
\pgfmathsetmacro{\myb}{-(\vsidemargin/28.453)-\thesidecnt/\totvalue{sidecnt}*\pheight}
\ifthenelse{\isodd{\value{page}}}{\def\corner{east}\def\sign{-}}{\def\corner{west}\def\sign{+}}
\fill[sidecolor] ($(current page.north \corner)+(0,\myt)$) rectangle ($(current page.north \corner)+(\sign0.75,\myb)$);
\node[font=\normalfont\sffamily\bfseries] at ($(current page.north \corner)+(\sign0.375,\myt-0.5)$) {\color{sidelabelcolor}\thechapter};
\iftoggle{noSide}{ \fill[white] ($(current page.north \corner)+(0,\myt)$) rectangle ($(current page.north \corner)+(\sign0.75,\myb)$); }{} %added
\end{tikzpicture}
\global\togglefalse{noSide} %added
}
\newcommand\importchapter[2]{
\chapter{#1}
\toggletrue{noSide} %added
\stepcounter{sidecnt}
%\ClearShipoutPicture* alike
\input{#2}
}
\begin{document}
\importchapter{First chapter}{ch1.tex}
\importchapter{Second chapter}{ch2.tex}
\end{document}