装饰命令移动文本

装饰命令移动文本

我在使用装饰命令时遇到了一个小问题,该命令会稍微移动页面上的文本。这是我尝试修复的代码的精简版本:

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[nomath]{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{microtype}
\usepackage[bottom]{footmisc}
\usepackage{mathtools}
\usepackage[dvipsnames,table]{xcolor}
\usepackage[pagestyles,medium]{titlesec}

% Main header :
\newpagestyle{principal}{
    \sethead[\thepage][][\itshape\small\MakeUppercase{\chaptername\ \thechapter. \chaptertitle}]{\itshape\small\MakeUppercase{\thesection\ \sectiontitle}}{}{\thepage}
    \headrule
}

% Add ornementations to corners :
\usepackage[object=vectorian]{pgfornament}

\newcommand*{\pageornament}[2]{
\begin{tikzpicture}[remember picture,overlay]
\color{#2}%
    \node[anchor=north west,xshift=0.5in+2mm,yshift=-2mm] at (current page.north west){% 
    \pgfornament[width=2cm]{#1}};
    \node[anchor=north east,xshift=-2mm,yshift=-2mm] at (current page.north east){%
    \pgfornament[width=2cm,symmetry=v]{#1}};
    \node[anchor=south west,xshift=0.5in+2mm,yshift=2mm] at (current page.south west){% 
    \pgfornament[width=2cm,symmetry=h]{#1}};
    \node[anchor=south east,xshift=-2mm,yshift=2mm] at (current page.south east){% 
    \pgfornament[width=2cm,symmetry=c]{#1}};
\end{tikzpicture}
}

\begin{document}

\pagestyle{principal}

\chapter{Chapter title}

\pageornament{41}{gray}

Some funny line\footnote{Some weird footnote text.}

\newpage

Another line

\end{document}

以下是此代码所做操作的预览(我们需要两次编译才能看到结果):

在此处输入图片描述

如果你停用装饰命令\pageornament{41}{灰色}(就在章节标题之后),文本会移回其正确位置。为什么装饰命令会移动文本块?我该如何防止这种情况发生?

答案1

使用\AddToHook宏扩展宏以在页面左上角(0,0)\shipout输出零大小的内容。picture

% pagedecoprob.tex  Try for decorative page margins

\documentclass{book} \usepackage{geometry}  %%%% SE 568730  or
%\documentclass{memoir}  %%%% SE 568730

\usepackage{comment}
\usepackage{lipsum}

\begin{document}

% seems like picture can now cope with actual values
% inset from page edges, change this to suit
\newlength{\linset} \setlength{\linset}{\baselineskip} 
% x coordinate of RHS 
\newlength{\rloc} \setlength{\rloc}{\paperwidth} \addtolength{\rloc}{-\linset}
% (-) y coordinate of bottom location
\newlength{\bloc} \setlength{\bloc}{\paperheight} \addtolength{\bloc}{-\linset}

% the decorative elements and their placements
\newcommand{\corners}{% 
  \put(\linset,-\linset){\makebox(0,0)[tl]{linset, -linset}}  % top left
  \put(\rloc,-\linset){\makebox(0,0)[tr]{rloc, -linset}}      % top right
  \put(\linset,-\bloc){\makebox(0,0)[bl]{linset, -bloc}}      % bottom left
  \put(\rloc,-\bloc){\makebox(0,0)[br]{rloc, -bloc}}          % bottom right
}

% use LaTeX arrows as the decorative elements
\renewcommand{\corners}{%
  \put(\linset,-\linset){\makebox(0,0)[tl]{\Huge$\nearrow$}}  % top left
  \put(\rloc,-\linset){\makebox(0,0)[tr]{\Huge$\nwarrow$}}    % top right
  \put(\linset,-\bloc){\makebox(0,0)[bl]{\Huge$\searrow$}}    % bottom left
  \put(\rloc,-\bloc){\makebox(0,0)[br]{\Huge$\swarrow$}}      % bottom right
}

% put decorative elements on this and future pages 
\AddToHook{shipout/background}[mypic]{\corners}

\chapter{First}
\textbf{Added To Hook}
\lipsum[1-12]

\textbf{Removed from Hook}

\RemoveFromHook{shipout/background}[mypic]

\lipsum[1-10]

\textbf{Added to Hook}

\AddToHook{shipout/background}[mypic]{\corners}

\lipsum[1-10]
  
\end{document}

感谢@ulrikefischer 向我展示\...Hook代码(回答如何在每一页上放置(或不放置)与页眉和页脚无关的内容?)。

在此处输入图片描述

相关内容