这是我的 MWE:
\documentclass{book}
\usepackage{lipsum}
\usepackage{fmtcount}
\FCloadlang{ngerman}
\usepackage[many]{tcolorbox}
\usepackage{calc}
\usetikzlibrary{positioning,shapes.misc,calc}
\usepackage[scale=1,angle=0,opacity=1]{background}
\usepackage{ifthen}
\usepackage{xcolor}
\definecolor{mp}{RGB}{240,240,240}
\SetBgScale{1}
\SetBgAngle{0}
\SetBgOpacity{1}
\SetBgContents{}
\newcounter{partshift}
\addtocounter{partshift}{-1}
\newcommand{\maxpart}{9}
\makeatletter
\newcommand{\parttitle}{}
\def\@part[#1]#2{%
\renewcommand{\parttitle}{#1}\pf
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\addcontentsline{toc}{part}{\bfseries\color{gray}\Ordinalstring{part}~\partname:\hspace{0.5em}#1}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\Ordinalstring{part}\nobreakspace\partname
\par
\vskip 20\p@
\fi
\bfseries\color{gray}\fontsize{28}{30}\selectfont #2\par}%
\@endpart}
\newcommand\pf{%
\AddEverypageHook{%
\ifthenelse{\isodd{\thepage}}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[
fill=mp,
inner sep=0pt,
rounded rectangle,
text width=5mm,
text height=\paperheight/\maxpart,
align=center,
anchor=north east] at
($ (current page.north east) + (1mm,-\thepartshift*\paperheight/\maxpart) $){};
\end{tikzpicture}}%
}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[
fill=mp,
inner sep=0pt,
rounded rectangle,
text width=5mm,
text height=\paperheight/\maxpart,
align=center,
anchor=north west] at
($ (current page.north west) + (-1mm,-\thepartshift*\paperheight/\maxpart) $){};
\end{tikzpicture}}
}
\bg@material}%
\stepcounter{partshift}
}
\makeatother
\begin{document}
\part{A}
\chapter{a1}
\part{B}
\chapter{b1}
\lipsum[1-7]
\part{C}
\chapter{c1}
\lipsum[1-7]
\end{document}
我想让它看起来是正确的
我怎样才能做到这一点?
答案1
您可以使用
rectangle,
rounded corners=2ex, %% adjust 2ex
得到它。我还增加了最小宽度和坐标,如下所示
minimum width=1cm,
.
.
anchor=north east] at
($ (current page.north east) + (5mm,-\thepartshift*\paperheight/\maxpart) $){};
注意5mm
和-5mm
(在偶数页)。您可以相应地调整这些值。
\documentclass{book}
\usepackage{lipsum}
\usepackage{fmtcount}
\FCloadlang{ngerman}
\usepackage[many]{tcolorbox}
\usepackage{calc}
\usetikzlibrary{positioning,shapes.misc,calc}
\usepackage[scale=1,angle=0,opacity=1]{background}
\usepackage{ifthen}
\usepackage{xcolor}
\definecolor{mp}{RGB}{240,240,240}
\SetBgScale{1}
\SetBgAngle{0}
\SetBgOpacity{1}
\SetBgContents{}
\newcounter{partshift}
\addtocounter{partshift}{-1}
\newcommand{\maxpart}{9}
\makeatletter
\newcommand{\parttitle}{}
\def\@part[#1]#2{%
\renewcommand{\parttitle}{#1}\pf
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\addcontentsline{toc}{part}{\bfseries\color{gray}\Ordinalstring{part}~\partname:\hspace{0.5em}#1}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\Ordinalstring{part}\nobreakspace\partname
\par
\vskip 20\p@
\fi
\bfseries\color{gray}\fontsize{28}{30}\selectfont #2\par}%
\@endpart}
\newcommand\pf{%
\AddEverypageHook{%
\ifthenelse{\isodd{\thepage}}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[
fill=mp,
inner sep=0pt,
rectangle,
rounded corners=2ex,
text width=5mm,
minimum width=1cm,
text height=\paperheight/\maxpart,
align=center,
anchor=north east] at
($ (current page.north east) + (5mm,-\thepartshift*\paperheight/\maxpart) $){};
\end{tikzpicture}}%
}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[
fill=mp,
inner sep=0pt,
rectangle,
rounded corners=2ex,
text width=5mm,
minimum width=1cm,
text height=\paperheight/\maxpart,
align=center,
anchor=north west] at
($ (current page.north west) + (-5mm,-\thepartshift*\paperheight/\maxpart) $){};
\end{tikzpicture}}
}
\bg@material}%
\stepcounter{partshift}
}
\makeatother
\begin{document}
\part{A}
\chapter{a1}
\part{B}
\chapter{b1}
\lipsum[1-7]
\part{C}
\chapter{c1}
\lipsum[1-7]
\end{document}
答案2
这是一个不需要猜测长度并绘制所需形状的解决方案(其他答案只是绘制了一个完整的矩形,但其中一半超出了页面)。此解决方案还使用了background
包的新语法:
\documentclass{book}
\usepackage{lipsum}
\usepackage{fmtcount}
\FCloadlang{ngerman}
\usepackage[many]{tcolorbox}
\usepackage{calc}
\usetikzlibrary{positioning,shapes.misc,calc}
\usetikzlibrary{chains,qrr.shapes.openrectangle}
\usepackage[scale=1,angle=0,opacity=1,contents={}]{background}
\usepackage{xcolor}
\definecolor{mp}{RGB}{240,240,240}
\newcounter{partshift}
\addtocounter{partshift}{-1}
\newcommand{\maxpart}{9}
\makeatletter
\newcommand{\parttitle}{}
\def\@part[#1]#2{%
\renewcommand{\parttitle}{#1}\pf
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\addcontentsline{toc}{part}{\bfseries\color{gray}\Ordinalstring{part}~\partname:\hspace{0.5em}#1}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\Ordinalstring{part}\nobreakspace\partname
\par
\vskip 20\p@
\fi
\bfseries\color{gray}\fontsize{28}{30}\selectfont #2\par}%
\@endpart}
\newcommand\pf{%
\AddEverypageHook{%
\ifodd\value{page}\relax
\backgroundsetup{contents={%
\begin{tikzpicture}[overlay,remember picture]
\node[
shape=open rectangle,
rounded corners=20pt,
open rectangle fill=mp,
open rectangle sides=nws,
align=center,
inner sep=0pt,
text width=10mm,
text height=\paperheight/\maxpart,
anchor=north east,
] at
( $ (current page.north east) + (1mm,-\thepartshift*\paperheight/\maxpart) $){};
\end{tikzpicture}}}%
\else
\backgroundsetup{contents={%
\begin{tikzpicture}[overlay,remember picture]
\node[
shape=open rectangle,
rounded corners=20pt,
open rectangle fill=mp,
open rectangle sides=ens,
text width=10mm,
text height=\paperheight/\maxpart,
align=center,
anchor=north west
] at
($ (current page.north west) + (-1mm,-\thepartshift*\paperheight/\maxpart) $){};
\end{tikzpicture}}}
\fi
\BgMaterial}%
\stepcounter{partshift}
}
\makeatother
\begin{document}
\part{A}
\chapter{a1}
\part{B}
\chapter{b1}
\lipsum[1-7]
\part{C}
\chapter{c1}
\lipsum[1-7]
\end{document}
代码使用了以下库:
作者奎伯比尔贝尔。
按照链接下载文件并将其保存在 TeX 可以找到的地方(当前工作目录是最简单的地方)。