如何将 fancyhdr 标题中的 Tikz 图片与文档边缘对齐?

如何将 fancyhdr 标题中的 Tikz 图片与文档边缘对齐?

我正在准备一个模板,我应该在边缘添加一个带有页码的书签式图标。这在 tikz 上工作正常,但是图标的边缘与文档边缘不对齐。如果只是将普通页码放在页眉中,那么它就会与边缘对齐。有人能给我提示如何解决这个问题吗?谢谢!

\documentclass{book}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{fancyhdr}
\usepackage{geometry}
\geometry{a4paper,
    inner=2.5cm, outer=2.0cm,top=3cm,bottom=2.5cm,
    includehead=false, % Kopfzeile außerhalb des Textkörper, also im Rand
    includefoot=false,
    headheight = \baselineskip,
    headsep = 25mm,
    showframe}

\renewcommand{\headrulewidth}{2pt}

\newcommand{\bmshapel}[1]{%
    \begin{tikzpicture}
    \fill[fill=black]
    (0.4,0) node {}
    -- (1,0) node {}
    -- (1,2) node {}
    -- (0,2) node {}
    -- (0,0.4) node {};
    \node[text=white, align=center] at (0.5, 0.7){\textsl{\textbf{\MakeUppercase{#1}}}};
    \end{tikzpicture}
}
\newcommand{\bmshaper}[1]{%
    \begin{tikzpicture}
    \fill[fill=black]
    (0.6,0) node {}
    -- (1,0.4) node {}
    -- (1,2) node {}
    -- (0,2) node {}
    -- (0,0) node {};
    \node[text=white, align=center] at (0.5, 0.7){\textsl{\textbf{\MakeUppercase{#1}}}};
    \end{tikzpicture}
}

\fancypagestyle{reg}{%
    \fancyhf{}%
    \fancyhead[OL]{\bmshapel{\thepage}}%
    \fancyhead[OR]{\bmshaper{\thepage}}}%

\pagestyle{reg}

\begin{document}

\mainmatter

\lipsum[1-3]


\end{document}

答案1

删除空节点,并在%后面添加\end{tikzpicture}

\documentclass{book}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{fancyhdr}
\usepackage{geometry}
\geometry{a4paper,
    inner=2.5cm, outer=2.0cm,top=3cm,bottom=2.5cm,
    includehead=false, % Kopfzeile außerhalb des Textkörper, also im Rand
    includefoot=false,
    headheight = \baselineskip,
    headsep = 25mm,
    showframe}

\renewcommand{\headrulewidth}{2pt}

\newcommand{\bmshapel}[1]{%
    \begin{tikzpicture}
    \fill[fill=black]
    (0.4,0)
    -- (1,0)
    -- (1,2)
    -- (0,2)
    -- (0,0.4);
    \node[text=white, align=center] at (0.5, 0.7){\textsl{\textbf{\MakeUppercase{#1}}}};
    \end{tikzpicture}%
}
\newcommand{\bmshaper}[1]{%
    \begin{tikzpicture}
    \fill[fill=black]
    (0.6,0)
    -- (1,0.4)
    -- (1,2)
    -- (0,2)
    -- (0,0);
    \node[text=white, align=center] at (0.5, 0.7){\textsl{\textbf{\MakeUppercase{#1}}}};
    \end{tikzpicture}%
}

\fancypagestyle{reg}{%
    \fancyhf{}%
    \fancyhead[OL]{\bmshapel{\thepage}}%
    \fancyhead[OR]{\bmshaper{\thepage}}}%

\pagestyle{reg}

\begin{document}

\mainmatter

\lipsum[1-3]


\end{document}

在此处输入图片描述

相关内容