我正在使用book
文档类。我希望章节名称(引言、结果、讨论、...结论...)出现在每一页的右侧小垂直(颜色填充)框中(框大小,假设为 3 厘米)。因此,我希望将章节名称保留在右侧颜色填充框中,而不是页眉或页脚中......有时我们会在 Dairy 中看到这种样式,在许多书籍中也是如此。
对于第一章,这个右侧框将出现在右上角....然后在下一章中,该框将稍微低一些并且会有不同的颜色。
有谁能帮助我吗?
答案1
这是一个可能的解决方案,使用background
包;该\BoxColor
命令使用一个简单的方法\ifcase
来选择要使用的颜色;\chapFrame
在每个\chapter
命令之后使用(这可以自动化)。代码需要运行三次才能稳定下来。
根据评论,彩色框的位置必须交替出现在奇数页和偶数页上;这是必要的修改:
改进版本(background
版本 > 2.0)
此版本使用了 2.0 版中引入的新语法background
,并消除了对包的需求ifthen
:
\documentclass{book}
\usepackage[
scale=1,
angle=0,
opacity=1,
contents={}
]{background}
\usetikzlibrary{calc}
\usepackage{lipsum}
\pagestyle{plain}
% auxiliary counter
\newcounter{chapshift}
\addtocounter{chapshift}{-1}
% the list of colors to be used (add more if needed)
\newcommand\BoxColor{%
\ifcase\thechapshift blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi}
% the main command; the mandatory argument sets the color of the vertical box
\newcommand\ChapFrame{%
\AddEverypageHook{%
\ifodd\value{page}
\backgroundsetup{contents={%
\begin{tikzpicture}[overlay,remember picture]
\node[
fill=\BoxColor,
inner sep=0pt,
rectangle,
text width=2cm,
text height=4cm,
align=center,
anchor=north east
]
at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $)
{\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
\raggedright\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}%
}%
}
\else
\backgroundsetup{contents={%
\begin{tikzpicture}[overlay,remember picture]
\node[
fill=\BoxColor,
inner sep=0pt,
rectangle,
text width=2cm,
text height=4cm,
align=center,
anchor=north west
]
at ($ (current page.north west) + (-0cm,-2*\thechapshift cm) $)
{\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
\raggedright\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}%
}%
}
\fi
\BgMaterial}%
\stepcounter{chapshift}%
}
% redefinition of \chaptermark to contain only the title
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}}
\begin{document}
\chapter[intro]{Introduction}
\ChapFrame
\lipsum[1-7]
\chapter{Results}
\ChapFrame
\lipsum[1-7]
\chapter{Discussion}
\ChapFrame
\lipsum[1-7]
\end{document}
结果:
第一个版本(background
版本 < 2.0)
这是代码的第一个版本;它仍然可以与最新版本的background
包一起使用:
\documentclass{book}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage{ifthen}
\usepackage{lipsum}
\pagestyle{plain}
% background common settings
\SetBgScale{1}
\SetBgAngle{0}
\SetBgOpacity{1}
\SetBgContents{}
% auxiliary counter
\newcounter{chapshift}
\addtocounter{chapshift}{-1}
% the list of colors to be used (add more if needed)
\newcommand\BoxColor{%
\ifcase\thechapshift blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi}
% the main command; the mandatory argument sets the color of the vertical box
\makeatletter
\newcommand\ChapFrame{%
\AddEverypageHook{%
\ifthenelse{\isodd{\thepage}}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\BoxColor,inner sep=0pt,rectangle,text width=2cm,
text height=4cm,align=center,anchor=north east]
at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $)
{\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
\raggedright\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}}%
}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\BoxColor,inner sep=0pt,rectangle,text width=2cm,
text height=4cm,align=center,anchor=north west]
at ($ (current page.north west) + (-0cm,-2*\thechapshift cm) $)
{\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
\raggedright\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}}
}
\bg@material}%
\stepcounter{chapshift}
}
\makeatother
% redefinition of \chaptermark to contain only the title
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}}
\begin{document}
\chapter[intro]{Introduction}
\ChapFrame
\lipsum[1-7]
\chapter{Results}
\ChapFrame
\lipsum[1-7]
\chapter{Discussion}
\ChapFrame
\lipsum[1-7]
\end{document}
生成的文档的图像显示了前几页:
答案2
这个答案是对 Gonzalo 的优秀答案的补充,只是为了展示另一种方法。这里fancyhdr
使用 代替background
。我采用了 Gonzalo 给出的配色方案(感谢他)。
% ----------------------------------------------------------------
% Book Class (This is a LaTeX2e document) ***********************
% ----------------------------------------------------------------
\documentclass[10pt,x11names,svgnames,twoside]{book}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,calc}
\usepackage{lipsum,fancyhdr}
\usepackage[a4paper,left=1.2in,right=1in,top=1in,bottom=1in,headheight=\baselineskip,
headsep=9mm,footskip=13mm,showframe,]{geometry}
% ----------------------------------------------------------------
\newcommand\MyColor{% % This line is from Gonzalo's answer
\ifcase\thechapter blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi} % This line is from Gonzalo's answer
%------------------------------------------
\fancypagestyle{plain}{%
%% Clear all headers and footers
\fancyhf{}
%% Right headers on odd pages
\fancyhead[RO]{%
\rotatebox{90}{
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\MyColor,text=white,
font=\footnotesize,
inner ysep=12pt, inner xsep=20pt,
rounded rectangle,anchor=east,
xshift=-0mm,yshift=-32mm,text width=3cm, text height=0.4cm]
at ($ (current page.north east) + (2.9cm,-0cm) + (-4*\thechapter cm,0cm) $)
{\sffamily\itshape\small\nouppercase{\leftmark}};
\end{tikzpicture}
}
}
%% Left headers on even pages
\fancyhead[LE]{%
\rotatebox{90}{
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\MyColor,text=white,
font=\footnotesize,
inner ysep=12pt, inner xsep=20pt,
rounded rectangle,anchor=east,
xshift=41mm,yshift=-2mm,text width=3cm, text height=0.4cm]
at ($ (current page.north west) + (4cm,0cm) + (-4*\thechapter cm,0cm) $)
{\sffamily\itshape\small\nouppercase{\leftmark}};
\end{tikzpicture}
}
}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyfoot[R]{\thepage}
}
% ----------------------------------------------------------------
\pagestyle{plain}
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}}
\begin{document}
%
\chapter{First chapter}
\lipsum[1-16]
\cleardoublepage
\chapter{Second chapter}
\lipsum[1-16]
\chapter{Third chapter}
\lipsum[1-16]
\chapter{Fourth chapter}
\lipsum[1-16]
%
\end{document}