我对以下代码有疑问:
\documentclass[a4paper]{book}
\setlength\headheight{0cm}
\usepackage{xcolor}
\colorlet{headbgcolor}{green!50!blue!70}
\setlength{\topmargin}{-1.5cm}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand\chaptermark[1]{\markboth{\chaptername\ \thechapter:\enskip#1}{}}
\fancyhf{}
\fancyhead[LE]{%
\makebox[0pt][r]{
\colorbox{headbgcolor}{\makebox[\textwidth][r]{\bfseries\textcolor{white}{\thepage}\enskip}}\hspace*{1em}}%
{\itshape\leftmark}\hspace*{1em}\headrulefill%
}
\fancyhead[RO]{%
\mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
\makebox[0pt][l]{%
\hspace*{1em}\colorbox{headbgcolor}{\makebox[\textwidth][l]{\enskip \bfseries\textcolor{white}{\thepage}}}}%
}
\renewcommand\headrulewidth{0pt}
\def\headrulefill{\leaders\hrule width 0pt height 3pt depth -2.8pt \hfill}
\usepackage{blindtext}% dummy text
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[LE]{%
\makebox[0pt][r]{
\colorbox{headbgcolor}{\makebox[\textwidth][r]{\bfseries \textcolor{white}{\thepage}\enskip}}\hspace*{1em}}%
{\itshape\leftmark}\hspace*{1em}\headrulefill%
}
\fancyhead[RO]{%
\mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
\makebox[0pt][l]{%
\hspace*{1em}\colorbox{headbgcolor}{\makebox[\textwidth][l]{\enskip\bfseries\textcolor{white}{\thepage}}}}%
}
}
\usepackage{mathptmx}
\begin{document}
\blinddocument
\end{document}
此代码的目的是在每页的页眉中给出一个填充的水平矩形,其中包含页码(矩形所在的页面边会根据页面是奇数页还是偶数页而变化)。我想知道是否有人可以建议对代码进行一些修改,这样,页码就不再位于矩形内,而是位于五边形内,如图所示
答案1
以下是具有 TikZ 形状的部分解决方案signal
。您必须调整信号长度以达到页面边界。
\documentclass[a4paper]{book}
\usepackage{tikz}
\usetikzlibrary{shapes.symbols}
\setlength\headheight{0cm}
\usepackage{xcolor}
\colorlet{headbgcolor}{green!50!blue!70}
\setlength{\topmargin}{-1.5cm}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand\chaptermark[1]{\markboth{\chaptername\ \thechapter:\enskip#1}{}}
\fancyhf{}
\fancyhead[LE]{%
\makebox[0pt][r]{\tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=east, fill=headbgcolor, text width=\marginparwidth, align=right]{\thepage};
\hspace*{1em}}%
{\itshape\leftmark}\hspace*{1em}\headrulefill%
}
\fancyhead[RO]{%
\mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
\makebox[0pt][l]{%
\hspace*{1em}\tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=west, fill=headbgcolor, text width=\marginparwidth, align=left]{\thepage};}%
}
\renewcommand\headrulewidth{0pt}
\def\headrulefill{\leaders\hrule width 0pt height 3pt depth -2.8pt \hfill}
\usepackage{blindtext}% dummy text
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[LE]{%
\makebox[0pt][r]{
\tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=east, fill=headbgcolor, text width=\marginparwidth, align=right]{\thepage};\hspace*{1em}}%
{\itshape\leftmark}\hspace*{1em}\headrulefill%
}
\fancyhead[RO]{%
\mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
\makebox[0pt][l]{%
\hspace*{1em}\tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=west, fill=headbgcolor, text width=\marginparwidth, align=left]{\thepage};}%
}
}
\usepackage{mathptmx}
\begin{document}
\blinddocument
\end{document}