我在整本书中都使用了背景图片。我使用的代码源自这个问题和答案(感谢@GonzaloMedina):“双面报告外边距的垂直边距规则“
我的问题是,我想弄清楚如何将彩色背景框移到每章(7-10 章)页面边缘的新位置。此外,我想将章节号放在彩色框内。这可能吗?
\documentclass[a4paper,openany]{book}
\usepackage[contents={},opacity=0.5,scale=1,angle=90,hshift=-2.5cm]{background}
\usepackage{lipsum}
\newcommand*\VerBar[2]{%
{\color{#1}\rule{#2}{65pt}}}
\newif\ifBgUse
\AddEverypageHook{%
\ifBgUse%
\ifodd\value{page}
\backgroundsetup{position={current page.north east},vshift=32.5pt,%
contents={\VerBar{blue}{5cm}}}%
\else
\backgroundsetup{position={current page.north west},vshift=-32.5pt,%
contents={\VerBar{blue}{5cm}}}%
\fi%
\BgMaterial%
\fi}
\begin{document}
\chapter{Test chapter one}
\lipsum[1-9]\clearpage
\BgUsetrue% activate background material
\chapter{Test chapter two}
\lipsum[1-9]\clearpage
\BgUsefalse% deactivate background material
\chapter{Test chapter two}
\lipsum[1-9]
\end{document}
答案1
您可以简单地引入适当的水平移动(因为材料之前已经旋转过),例如基于章节计数器:
\documentclass[a4paper,openany]{book}
\usepackage[contents={},opacity=0.5,scale=1,angle=90]{background}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{etoolbox}
\newcounter{mychap}
\pretocmd{\chapter}{\stepcounter{mychap}}{}{}
\newcommand*\VerBar[2]{%
{\color{#1}\rule{#2}{65pt}}%
\llap{\rotatebox[origin=c]{-90}{%
\hspace*{-2.5cm}\raisebox{-2.5cm}[0pt][0pt]{%
\Huge\color{white}\bfseries\thechapter}%
}%
}%
}
\newif\ifBgUse
\newlength\LabelSize
\setlength\LabelSize{2.5cm}
\AddEverypageHook{%
\ifBgUse%
\ifodd\value{page}
\backgroundsetup{
position={current page.north west},
vshift=-32.5pt,
hshift=-\value{mychap}*\LabelSize,
contents={\VerBar{blue}{5cm}}%
}%
\else
\backgroundsetup{
position={current page.north east},
vshift=32.5pt,
hshift=-\value{mychap}*\LabelSize,
contents={\VerBar{blue}{5cm}}%
}%
\fi%
\BgMaterial%
\fi}
\begin{document}
\chapter{Test chapter one}
\lipsum[1-9]\clearpage
\BgUsetrue% activate background material
\chapter{Test chapter two}
\lipsum[1-9]
\chapter{Test chapter three}
\lipsum[1-9]
\chapter{Test chapter four}
\lipsum[1-9]\clearpage
\appendix
\chapter{Test appendix one}
\lipsum[1-9]\clearpage
\BgUsefalse% deactivate background material
\chapter{Test appendix two}
\lipsum[1-9]
\end{document}
答案2
以下是我的做法:
\documentclass[a4paper,openany]{book}
\usepackage[contents={},opacity=0.5,scale=1,angle=90]{background}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{ifthen}
\newcommand*\VerBar[2]{%
\begin{tikzpicture}
\fill[#1] (0,0)--(-5,0)--(-5,2)--(0,2)--cycle;
\draw[white] (-2.5,1)node{\rotatebox{-90}{\Huge \thechapter}};
\end{tikzpicture}
}
\newlength{\shift}
\setlength{\shift}{2.5cm}
\newif\ifBgUse
\AddEverypageHook{%
\ifBgUse%
\ifodd\value{page}
\backgroundsetup{position={current page.north west},vshift=-1cm,%
contents={\VerBar{blue}{5cm}}}%
\else
\backgroundsetup{position={current page.north east},vshift=1cm,%
contents={\VerBar{blue}{5cm}}}%
\fi%
\BgMaterial%
\fi}
\BgUsefalse
\newcommand{\mychapter}[1]{\ifthenelse{\equal{#1}{*}}%
{\mycommandStar}%
{\mycommandNoStar{#1}}%
}
\newcommand{\mycommandStar}[1]{\BgUsetrue
\backgroundsetup{hshift=\shift}
\chapter{#1}
\addtolength{\shift}{-5cm}}
\newcommand{\mycommandNoStar}[1]{\BgUsefalse%
\chapter{#1}}
\begin{document}
\mychapter{Test chapter one}
\lipsum[1-9]\clearpage
\mychapter*{Test chapter two}
\lipsum[1-9]\clearpage
\mychapter*{Test chapter three}
\lipsum[1-9]
\end{document}
为了在框中显示章节编号,我使用tikz
而不是\rule
。
为了根据章节扫描方框,我定义了一个新的长度(移位),每次开始新章节时,该长度都会减少(向下)5 厘米,并且为BgUse
真。为此,我刚刚定义了一个新命令,\mychapter
其工作方式与此完全相同\chapter
(并设置BgUse
为 false),并且此命令的星号版本\mychapter*
将变为BgUse
true 并在页面侧面添加扫描方框。它也调用章节命令。
以下是第 2 章和第 3 章的输出(第一页):