菱形框中的页码

菱形框中的页码

我有以下代码,我需要将页码放在页面顶部,结果会出现一个菱形框。如果您能提供任何帮助或向我指出生成它的代码,我将不胜感激。

在此处输入图片描述

我忘记了一点,偶数页的页码应该在左边,奇数页的页码应该在右边。

答案1

一个选项是使用fancyhdrtikzpagenodes包装:

\documentclass{book}
\usepackage[a6paper]{geometry}
\usepackage{fancyhdr}
\usepackage{tikzpagenodes}
\usetikzlibrary{shapes.geometric}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[OC]{\begin{tikzpicture}[remember picture,overlay]
\node[diamond,draw,font=\small\itshape] at (current page header area.south west) (dia) {\thepage};
\draw[double=white] (dia.east) -- (current page header area.south east);
\end{tikzpicture}}
\fancyhead[EC]{\begin{tikzpicture}[remember picture,overlay]
\node[diamond,draw,font=\small\itshape] at (current page header area.south east) (dia) {\thepage};
\draw[double=white] (dia.west) -- (current page header area.south west);
\end{tikzpicture}}
\fancyhead[OR]{\small\nouppercase\leftmark}
\fancyhead[EL]{\small\nouppercase\rightmark}

\begin{document}

\chapter{Test chapter}
\lipsum[2]
\section{Test section}
\lipsum[2]\lipsum[2]\lipsum[2]\lipsum[2]\lipsum[2]

\end{document}

在此处输入图片描述

如果水平线的宽度不相同:

\documentclass{book}
\usepackage[a6paper]{geometry}
\usepackage{fancyhdr}
\usepackage{tikzpagenodes}
\usetikzlibrary{shapes.geometric}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[OC]{\begin{tikzpicture}[remember picture,overlay]
\node[diamond,draw,font=\small\itshape] at (current page header area.south west) (dia) {\thepage};
\draw (dia.3) -- (current page header area.south east|-dia.3);
\draw (dia.357) -- ([xshift=-7pt]current page header area.south east|-dia.357);
\end{tikzpicture}}
\fancyhead[EC]{\begin{tikzpicture}[remember picture,overlay]
\node[diamond,draw,font=\small\itshape] at (current page header area.south east) (dia) {\thepage};
\draw (dia.177) -- (current page header area.south west|-dia.177);
\draw (dia.183) -- ([xshift=7pt]current page header area.south west|-dia.183);
\end{tikzpicture}}
\fancyhead[OR]{\small\nouppercase\leftmark}
\fancyhead[EL]{\small\nouppercase\rightmark}

\begin{document}

\chapter{Test chapter}
\lipsum[2]
\section{Test section}
\lipsum[2]\lipsum[2]\lipsum[2]\lipsum[2]\lipsum[2]

\end{document}

在此处输入图片描述

答案2

借鉴 Gonzalo Medina 的解决方案,但使其摆脱 tikz,并采用不同长度的双线:

\documentclass{book}
\usepackage[a6paper]{geometry}% just for the example
\usepackage{fancyhdr}
\usepackage{stackengine}
\usepackage{graphicx}
\usepackage{lipsum}% just to generate text for the example

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[OC]{%
  \def\stackalignment{c}%
  \topinset{\itshape\thepage}{\scalebox{5}{\(\diamond\)}}{1.5ex}{}%
  \rule{-.2ex}{0ex}%
  \def\stackalignment{l}%
  \stackon[1pt]{\rule[2.8ex]{2.5in}{.1ex}}{\rule{2.55in}{.1ex}}}
\fancyhead[EC]{%
  \def\stackalignment{r}%
  \stackon[1pt]{\rule[2.8ex]{2.5in}{.1ex}}{\rule{2.55in}{.1ex}}%
  \rule{-.2ex}{0ex}%
  \def\stackalignment{c}%
  \topinset{\itshape\thepage}{\scalebox{5}{\(\diamond\)}}{1.5ex}{}}
\begin{document}
\lipsum[2]\lipsum[2]\lipsum[2]\lipsum[2]
\end{document}

在此处输入图片描述

对于偶数页:

在此处输入图片描述

相关内容