将页码放在 Tikz 形状中

将页码放在 Tikz 形状中

我正在用波斯语写一本书(其方向是从右到左),我希望页码像下面的代码一样显示:

 \documentclass[12pt,twoside]{book}
    \usepackage{tikz}
    \usepackage{lipsum}
    \usepackage{fancyhdr}
    %\usepackage{xepersian} % If this enables problem occurs
    %\settextfont{Adobe Arabic} % Or \settextfont{Yas}
    \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=east] 
        \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}
    \chapter{تست ۱}
     \lipsum[1-5]
     \section{تست ۲}
        \lipsum[1-5]

    \end{document}

代码在没有xepersian包的情况下编译完美,但使用此包后,偶数页码不合适。有人知道这个吗?

编辑
我需要的: 理想身材 第二个答案中的代码产生了这样的结果: 编辑答案

答案1

由于从左到右变为从右到左,Tik对东和西的解释不同。因此,您需要重新配置 tikzpicture 部分中的锚点。

我稍微简化了您的代码,eastwest在适当的地方进行了相应的切换,以便现在的代码适合从右到左的排版:

\documentclass[12pt,twoside]{book}
    \usepackage{tikz}
    \usepackage{lipsum}
    \usepackage{fancyhdr}
    \usepackage{xepersian} 
    \settextfont{Amiri} % I didn't have the other fonts

    \fancypagestyle{myfancy}{%   
      \fancyhf{}%
      \fancyfoot[OL]{%
        \begin{tikzpicture}[overlay,baseline={(0,0)}] 
        \node[
          rectangle,
          outer sep=0pt,
          text=white,
          fill=black!30,
          draw=black!20,
          anchor=east,
        ]
          (pageno)
          {\thepage};
        \draw[
          fill=black!30,
          draw=black!20,
        ]
          ([xshift=\oddsidemargin+1in]pageno.north east)
            rectangle  
          (pageno.south east);
        \end{tikzpicture}%  
        }%
      \fancyfoot[ER]{%
        \begin{tikzpicture}[overlay,baseline={(0,0)}] 
        \node[
          rectangle,
          outer sep=0pt,
          text=white,
          fill=black!30,
          draw=black!20,
          anchor=west,
        ]
          (pageno)
          {\thepage};
        \draw[
          fill=black!30,
          draw=black!20,
        ]
          ([xshift=-\oddsidemargin-1in]pageno.north west)
            rectangle  
          (pageno.south west);
        \end{tikzpicture}%  
        }%
        \renewcommand{\headrulewidth}{0pt}%
        \renewcommand{\footrulewidth}{0pt}%
    }

\begin{document}
    \pagestyle{myfancy}

    \chapter{تست ۱}
        \lipsum[1-5]

    \section{تست ۲}
        \lipsum[1-5]

\end{document}

当然,这意味着如果您xepersian再次注释该包,该解决方案将再次不起作用。

在此处输入图片描述


编辑

偶数页和奇数页已更改:

\documentclass[12pt,twoside]{book}
    \usepackage{tikz}
    \usepackage{lipsum}
    \usepackage{fancyhdr}
    \usepackage{xepersian} % If this enables problem occurs
    \settextfont{Amiri} % Or \settextfont{Yas}

    \fancypagestyle{myfancy}{%   
      \fancyhf{}%
      \fancyfoot[OR]{%
        \begin{tikzpicture}[overlay,baseline={(0,0)}] 
        \node[
          rectangle,
          outer sep=0pt,
          text=white,
          fill=black!30,
          draw=black!20,
          anchor=west,
        ]
          (pageno)
          {\thepage};
        \draw[
          fill=black!30,
          draw=black!20,
        ]
          ([xshift=-\paperwidth+\textwidth-\oddsidemargin]pageno.north west)
            rectangle  
          (pageno.south west);
        \end{tikzpicture}%  
        }%
      \fancyfoot[EL]{%
        \begin{tikzpicture}[overlay,baseline={(0,0)}] 
        \node[
          rectangle,
          outer sep=0pt,
          text=white,
          fill=black!30,
          draw=black!20,
          anchor=east,
        ]
          (pageno)
          {\thepage};
        \draw[
          fill=black!30,
          draw=black!20,
        ]
          ([xshift=\paperwidth-\textwidth+\oddsidemargin]pageno.north east)
            rectangle  
          (pageno.south east);
        \end{tikzpicture}%  
        }%
        \renewcommand{\headrulewidth}{0pt}%
        \renewcommand{\footrulewidth}{0pt}%
    }

\begin{document}
    \pagestyle{myfancy}

    \chapter{تست ۱}
        \lipsum[1-5]

    \section{تست ۲}
        \lipsum[1-5]

\end{document}

第二次编辑

不知何故,所使用的软件包的不同版本在解释东方和西方方面表现不同。上述答案目前适用于我用于测试的 Overleaf。

以下内容应适用于最新的软件包(截至 2019 年 2 月 5 日):

\documentclass[12pt,twoside]{book}
    \usepackage{tikz}
    \usepackage{lipsum}
    \usepackage{fancyhdr}
    \usepackage{xepersian} % If this enables problem occurs
    \settextfont{Amiri} % Or \settextfont{Yas}

    \fancypagestyle{myfancy}{%   
      \fancyhf{}%
      \fancyfoot[OR]{%
        \begin{tikzpicture}[overlay,baseline={(0,0)}] 
        \node[
          rectangle,
          outer sep=0pt,
          text=white,
          fill=black!30,
          draw=black!20,
          anchor=west
        ] at (-0.5\textwidth,0)        
          (pageno)
          {\thepage};
        \draw[
          fill=black!30,
          draw=black!20,
          anchor=east
        ]
          ([xshift=-\paperwidth+\textwidth-\oddsidemargin]pageno.north west)
            rectangle  
          (pageno.south west);
        \end{tikzpicture}%  
        }%
      \fancyfoot[EL]{%
        \begin{tikzpicture}[overlay,baseline={(0,0)}] 
        \node[
          rectangle,
          outer sep=0pt,
          text=white,
          fill=black!30,
          draw=black!20,
          anchor=east
        ] at (\textwidth,0)
          (pageno)
          {\thepage};
        \draw[
          fill=black!30,
          draw=black!20,
        ]
          ([xshift=\paperwidth-\textwidth+\oddsidemargin]pageno.north east)
            rectangle  
          (pageno.south east);
        \end{tikzpicture}%  
        }%
      \renewcommand{\headrulewidth}{0pt}%
      \renewcommand{\footrulewidth}{0pt}%
    }

\begin{document}
    \pagestyle{myfancy}

    \chapter{تست ۱}
        \lipsum[1-5]

    \section{تست ۲}
        \lipsum[1-7]

\end{document}

相关内容