如何将页码放在附在页面边缘的 tikz \node 内?

如何将页码放在附在页面边缘的 tikz \node 内?

我的尝试

\documentclass[12pt,twoside]{book}
   %------Tikz------------------------------------------------
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{
positioning, shapes,
shapes,
arrows,
calc,
trees,
decorations.pathmorphing, decorations.pathreplacing, decorations.markings}
    \usepackage{lipsum}

%---------------headers and footers---------------
\usepackage{fancyhdr,blindtext,tikz}
\fancypagestyle{myfancy}{%   
    \fancyhf{}%
    \fancyfoot[ER,OL]{\tikz[baseline={(0,0)},anchor=center] \node [rectangle,
        text=white, fill = black!30, draw = black!20, width = 5cm]{\thepage};}%
    \renewcommand{\headrulewidth}{0pt}%
    \renewcommand{\footrulewidth}{0pt}%
}%
%% without the following the chapter pages won't have fancy page style
%\makeatletter
%\let\ps@plain\ps@myfancy
%\makeatother
%

\begin{document}
    \pagestyle{myfancy}

    \lipsum[1-35]

\end{document}

我需要这样的东西: 在此处输入图片描述

答案1

一种选择:

\documentclass[12pt,twoside]{book}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{fancyhdr}

\fancypagestyle{myfancy}{%   
  \fancyhf{}%
  \fancyfoot[OL]{%
    \begin{tikzpicture}[overlay,baseline={(0,0)},anchor=center] 
    \node[
      rectangle,
      outer sep=0pt,
      text=white,
      fill = black!30,
      draw = black!20,
      anchor=west,
      ]
      (pageno)
      {\phantom\thepage};
    \draw[
      fill = black!30,
      draw = black!20,
    ]
      ([xshift=-\oddsidemargin-1in]pageno.north west)
        rectangle  
      (pageno.south east);
    \node[
    anchor=east,
    font=\color{white}
    ]
      at (pageno.east)
      {\thepage};  
    \end{tikzpicture}%  
    }%
  \fancyfoot[ER]{%
    \begin{tikzpicture}[overlay,baseline={(0,0)},anchor=center] 
    \node[
      rectangle,
      outer sep=0pt,
      text=white,
      anchor=east,
      ]
      (pageno)
      {\phantom{\thepage}};
    \draw[
      fill = black!30,
      draw = black!20,
    ]
      ([xshift=\paperwidth-\textwidth-\oddsidemargin-1in]pageno.north west)
        rectangle  
      (pageno.south west);
    \node[
    anchor=west,
    font=\color{white}
    ]
      at (pageno.west)
      {\thepage};  
    \end{tikzpicture}%  
    }%
    \renewcommand{\headrulewidth}{0pt}%
    \renewcommand{\footrulewidth}{0pt}%
}%
%% without the following the chapter pages won't have fancy page style
%\makeatletter
%\let\ps@plain\ps@myfancy
%\makeatother
%

\begin{document}
    \pagestyle{myfancy}

    \lipsum[1-35]

\end{document}

结果:

在此处输入图片描述

一些放大的图像:

在此处输入图片描述

在此处输入图片描述

答案2

这是一个使用current page(需要两次编译的特殊节点)的简短解决方案。

(注意:我更改了您的示例,将页码放在外面。)

\documentclass[12pt,twoside]{book}
\usepackage{tikz}
\usepackage{lipsum}

%---------------headers and footers---------------
\usepackage{fancyhdr}
\tikzset{
  page num/.style n args={2}{
    text=white,fill=black!30,text width=5cm,font=\bfseries,
    node contents=\thepage, at={(0,0 -| current page.#1)},
    align=flush #2,anchor=base #1,
  },
  page num OR/.style={page num={east}{left}},
  page num EL/.style={page num={west}{right}},
}
\fancypagestyle{myfancy}{
    \fancyhf{}
    \fancyfoot[OR]{\tikz[remember picture,overlay]\node[page num OR];}
    \fancyfoot[EL]{\tikz[remember picture,overlay]\node[page num EL];}
    \renewcommand{\headrulewidth}{0pt}%
    \renewcommand{\footrulewidth}{0pt}%
}
\pagestyle{myfancy}

\begin{document}
\lipsum[1-35]
\end{document}

相关内容