我尝试使用装饰和背景包在每个页面上放置页面边框。我想将此框架放置在距离页面边框 0.6 厘米的位置,因此我使用 tikz 的 calc 库根据坐标指定每个页面的框架位置。即使文档是用 XeLaTeX 编译的,并且框架放置正确 - 我猜 - 我还是收到错误:
Package pgf Error: No shape named {current page.north west} + {0.6cm,-0.6cm}
以下是 MWE:
\documentclass[a5paper,10pt,openany,oneside]{book}
\usepackage[object=vectorian]{pgfornament}
\usetikzlibrary{positioning,calc}
\usepackage{background}
\usepackage{lipsum}
\def\xoffset{0.6cm}
\def\yoffset{0.6cm}
\def\ornament{61}
\def\ornamentcolor{black}
%Command for inserting the frames
\newcommand{\pageframe}{
\begin{tikzpicture}[remember picture, overlay, inner sep=0pt]
%Necessary coordinates
\coordinate (UR) at ($(current page.north east) + (-\xoffset,-\yoffset)$);
\coordinate (UL) at ($(current page.north west) + (\xoffset,-\yoffset)$);
\coordinate (LR) at ($(current page.south east) + (-\xoffset,\yoffset)$);
\coordinate (LL) at ($(current page.south west) + (\xoffset,\yoffset)$);
\coordinate (CL) at ($(current page.west) + (\xoffset,0)$);
\coordinate (CR) at ($(current page.east) + (-\xoffset,0)$);
%Ornaments located at the corners of the page
\node[anchor=north west] (ULCorner) at (UL){%
\pgfornament[width = 2cm,color = \ornamentcolor ]{\ornament}};%
\node[anchor=north east] (URCorner) at (UR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=v ]{\ornament}};%
\node[anchor=south east] (LRCorner) at (LR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=c ]{\ornament}};%
\node[anchor=south west] (LLCorner) at (LL){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=h ]{\ornament}};%
%Ornaments between the corners: Calculates the distance between the 2 upper corners and uses it for the width of the bar ornament.
\path (CL) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=-3pt] (CLBar) at (CL){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\path (CR) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=0pt] (CRBar) at (CR){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\draw[line width=1.5pt,line cap=round,black]($(CLBar.east) + (0.8pt,0)$) to ($(ULCorner.south west) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.east) + (0.8pt,-1pt)$) to ($(URCorner.south east) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CLBar.west) + (0.8pt,0)$) to ($(LLCorner.north west) + (0.8pt,-1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.west) + (0.8pt,1)$) to ($(LRCorner.north east) + (0.8pt,-1pt)$){};
%Ornaments between he corners: Top and bottom.
\path[] (ULCorner.north east) to [ornament=88, color=black] (URCorner.north west) {};
\path (LRCorner.south west) to [ornament=88,color=black] (LLCorner.south east) {};
\end{tikzpicture}}
\backgroundsetup{scale=1,color=black,opacity=1,contents=\pageframe, angle=0,position={{(current page.north west)} + {(\xoffset,-\yoffset)}},nodeanchor=north west}
\begin{document}
\lipsum[1-5]
\end{document}
我的问题是:我做错了什么?背景包是否接受这种类型的表达式?
答案1
该解决方案使用包可每页节省约 20 kB 并节省一些编译时间xsavebox
。
打开背页。
1页lipsum售价16,345英镑,
2页lipsum的价格为19,831 B,
10 页的 Lipsum 售价为 36,361 B(xelatex
)
编辑:
代码针对文件大小和图形精度进行了进一步优化。角装饰和“条形装饰”被保存下来,xsaveboxes
以便在各个位置重复使用。此外,还对“条形装饰”的线条进行了采样,以便稍后在页面的左右边缘延伸“条形装饰”。
适用于所有引擎/后端,包括dvips
:
\documentclass[a5paper,10pt,openany,oneside]{book}
\usepackage[object=vectorian]{pgfornament}
\usetikzlibrary{positioning,calc}
\usepackage{eso-pic}
\usepackage{xsavebox}
\usepackage{lipsum}
%some size calculations
\def\xoffset{0.6cm}
\def\yoffset{0.6cm}
\def\cornerOrnamentWidth{2cm}
\edef\barOrnamentWidth{\the\dimexpr\paperwidth-\xoffset*2-\cornerOrnamentWidth*2\relax}
\def\cornerOrnament{61}
\def\barOrnament{88}
\def\ornamentcolor{black}
% This saves the whole ornament frame once in an xsavebox.
\AtBeginDocument{%
\xsbox{OrnamentCorner}{\pgfornament[width = \cornerOrnamentWidth, color = \ornamentcolor]{\cornerOrnament}}%
\xsbox{OrnamentBar}{\pgfornament[width=\barOrnamentWidth, color = \ornamentcolor]{\barOrnament}}%
\xsbox{LineSample}{\makebox[2.5cm][r]{\theOrnamentBar}}%
\xsbox{OrnamentBox}{%
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, x=\paperwidth,y=\paperheight]
\useasboundingbox (0,0) rectangle (1,1);
% necessary coordinates
\coordinate (UR) at ($(1,1) + (-\xoffset,-\yoffset)$);
\coordinate (UL) at ($(0,1) + (\xoffset,-\yoffset)$);
\coordinate (LR) at ($(1,0) + (-\xoffset,\yoffset)$);
\coordinate (LL) at ($(0,0) + (\xoffset,\yoffset)$);
\coordinate (CL) at ($(0,0.5) + (\xoffset,0)$);
\coordinate (CR) at ($(1,0.5) + (-\xoffset,0)$);
% ornaments located at page corners
\node[anchor=north west] (ULCorner) at (UL){\theOrnamentCorner};%
\node[anchor=north west,xscale=-1] (URCorner) at (UR) {\theOrnamentCorner};%
\node[anchor=north west, scale=-1] (LRCorner) at (LR){\theOrnamentCorner};%
\node[anchor=north west,yscale=-1] (LLCorner) at (LL){\theOrnamentCorner};%
% ornaments on the left and right sides
\node [rotate=90,anchor=center] at (CL) {\theOrnamentBar};
\node [rotate=90,anchor=east] at (ULCorner.south west) {\theLineSample\theLineSample};
\node [rotate=90,anchor=east,xscale=-1] at (LLCorner.south west) {\theLineSample\theLineSample};
\node [rotate=90,anchor=center,scale=-1] at (CR) {\theOrnamentBar};
\node [rotate=90,anchor=east, yscale=-1] at (URCorner.south west) {\theLineSample\theLineSample};
\node [rotate=90,anchor=east, scale=-1] at (LRCorner.south west) {\theLineSample\theLineSample};
% ornaments at top and bottom page edges
\path (ULCorner.north east) to node {\theOrnamentBar} (URCorner.north east) {};
\path (LRCorner.north east) to node [scale=-1] {\theOrnamentBar} (LLCorner.north east) {};
\end{tikzpicture}%
}%
}
% This adds the frame (a reference, actually) on every page.
\AddToShipoutPictureFG{\theOrnamentBox}%
\begin{document}
% \lipsum[1,2]
\lipsum[1-5]
% \lipsum[1-28]
\end{document}
答案2
似乎适用于所有编译器的解决方案。这里其实真的不需要backgrounds
。/eso-pic
就atbegshi
行。
\documentclass[a5paper,10pt,openany,oneside]{book}
\usepackage[object=vectorian]{pgfornament}
\usetikzlibrary{positioning,calc}
\usepackage{eso-pic}
\usepackage{lipsum}
\def\xoffset{0.6cm}
\def\yoffset{0.6cm}
\def\ornament{61}
\def\ornamentcolor{black}
%Command for inserting the frames
\AddToShipoutPictureBG{%
\begin{tikzpicture}[remember picture, overlay, inner sep=0pt]
%Necessary coordinates
\coordinate (UR) at ($(current page.north east) + (-\xoffset,-\yoffset)$);
\coordinate (UL) at ($(current page.north west) + (\xoffset,-\yoffset)$);
\coordinate (LR) at ($(current page.south east) + (-\xoffset,\yoffset)$);
\coordinate (LL) at ($(current page.south west) + (\xoffset,\yoffset)$);
\coordinate (CL) at ($(current page.west) + (\xoffset,0)$);
\coordinate (CR) at ($(current page.east) + (-\xoffset,0)$);
%Ornaments located at the corners of the page
\node[anchor=north west] (ULCorner) at (UL){%
\pgfornament[width = 2cm,color = \ornamentcolor ]{\ornament}};%
\node[anchor=north east] (URCorner) at (UR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=v ]{\ornament}};%
\node[anchor=south east] (LRCorner) at (LR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=c ]{\ornament}};%
\node[anchor=south west] (LLCorner) at (LL){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=h ]{\ornament}};%
%Ornaments between the corners: Calculates the distance between the 2 upper corners and uses it for the width of the bar ornament.
\path (CL) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=-3pt] (CLBar) at (CL){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\path (CR) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=0pt] (CRBar) at (CR){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\draw[line width=1.5pt,line cap=round,black]($(CLBar.east) + (0.8pt,0)$) to ($(ULCorner.south west) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.east) + (0.8pt,-1pt)$) to ($(URCorner.south east) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CLBar.west) + (0.8pt,0)$) to ($(LLCorner.north west) + (0.8pt,-1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.west) + (0.8pt,1)$) to ($(LRCorner.north east) + (0.8pt,-1pt)$){};
%Ornaments between he corners: Top and bottom.
\path[] (ULCorner.north east) to [ornament=88, color=black] (URCorner.north west) {};
\path (LRCorner.south west) to [ornament=88,color=black] (LLCorner.south east) {};
\end{tikzpicture}}
\begin{document}
\lipsum[1-5]
\end{document}
我已经检查过,这在pdflatex
、xelatex
和 上都能正常工作lualatex
。示例输出如下所示。它确实不是似乎可以正常工作latex -> dvi -> pdf
,但是,这可能是 2018 年可以接受的事情。
旧答案。免责声明:我没有仔细阅读背景手册。快速搜索xelatex
没有找到结果,但可能是我遗漏了一些基本的东西。你无论如何都要用叠加层来定位图片。无论你选择什么,它总是会出现在同一位置position
。(这不是真的!有一些与编译器相关的问题,我在写这篇文章时并没有想到会存在。)
\documentclass[a5paper,10pt,openany,oneside]{book}
\usepackage[object=vectorian]{pgfornament}
\usetikzlibrary{positioning,calc}
\usepackage{background}
\usepackage{lipsum}
\def\xoffset{0.6cm}
\def\yoffset{0.6cm}
\def\ornament{61}
\def\ornamentcolor{black}
%Command for inserting the frames
\newcommand{\pageframe}{
\begin{tikzpicture}[remember picture, overlay, inner sep=0pt]
%Necessary coordinates
\coordinate (UR) at ($(current page.north east) + (-\xoffset,-\yoffset)$);
\coordinate (UL) at ($(current page.north west) + (\xoffset,-\yoffset)$);
\coordinate (LR) at ($(current page.south east) + (-\xoffset,\yoffset)$);
\coordinate (LL) at ($(current page.south west) + (\xoffset,\yoffset)$);
\coordinate (CL) at ($(current page.west) + (\xoffset,0)$);
\coordinate (CR) at ($(current page.east) + (-\xoffset,0)$);
%Ornaments located at the corners of the page
\node[anchor=north west] (ULCorner) at (UL){%
\pgfornament[width = 2cm,color = \ornamentcolor ]{\ornament}};%
\node[anchor=north east] (URCorner) at (UR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=v ]{\ornament}};%
\node[anchor=south east] (LRCorner) at (LR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=c ]{\ornament}};%
\node[anchor=south west] (LLCorner) at (LL){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=h ]{\ornament}};%
%Ornaments between the corners: Calculates the distance between the 2 upper corners and uses it for the width of the bar ornament.
\path (CL) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=-3pt] (CLBar) at (CL){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\path (CR) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=0pt] (CRBar) at (CR){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\draw[line width=1.5pt,line cap=round,black]($(CLBar.east) + (0.8pt,0)$) to ($(ULCorner.south west) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.east) + (0.8pt,-1pt)$) to ($(URCorner.south east) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CLBar.west) + (0.8pt,0)$) to ($(LLCorner.north west) + (0.8pt,-1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.west) + (0.8pt,1)$) to ($(LRCorner.north east) + (0.8pt,-1pt)$){};
%Ornaments between he corners: Top and bottom.
\path[] (ULCorner.north east) to [ornament=88, color=black] (URCorner.north west) {};
\path (LRCorner.south west) to [ornament=88,color=black] (LLCorner.south east) {};
\end{tikzpicture}}
\backgroundsetup{scale=1,color=black,opacity=1,contents=\pageframe, angle=0}
\begin{document}
\lipsum[1-5]
\end{document}
如您所见,装饰物的位置非常漂亮。因为overlay
使用绝对页面坐标的图片并不关心position
您为它们分配的坐标。
附录:这似乎是目前比较罕见的情况之一,在这种情况下,使用哪种编译器很重要。为了简化问题,我从中删除了所有位置信息\backgroundsetup
。
第一个令人惊讶的是,这
angle
似乎很重要。对我来说,这是非常令人惊讶。这个问题似乎与编译器无关。
接下来的惊喜是结果取决于编译器。如果我保留angle=0
上面的内容,只要使用pdflatex
或进行编译,代码就可以正常工作lualatex
。
但是,如果我使用 进行编译
xelatex
,我会重现 Charlie 报告的问题。在这种情况下,查理的回答似乎是可行的方法。如果我用 latex 编译,然后将文件转换dvi
为 pdf,则没有任何装饰。总之,我不认为这个问题已经完全解决了。但我想说,在这种情况下,切换到 可以eso-pic
解决问题。
答案3
根据@Marmot的建议,我删除了覆盖层并记住了 tikzpicture 环境中的图片标志,将位置设置为当前位置.center,并将锚点也设置为中心。这在使用 XeLaTeX 编译时有效
\documentclass[a5paper,10pt,openany,oneside]{book}
\usepackage[object=vectorian]{pgfornament}
\usetikzlibrary{positioning,calc}
\usepackage{background}
\usepackage{lipsum}
\def\xoffset{0.6cm}
\def\yoffset{0.6cm}
\def\ornament{61}
\def\ornamentcolor{black}
%Command for inserting the frames
\newcommand{\pageframe}{
\begin{tikzpicture}[inner sep=0pt]
%Necessary coordinates
\coordinate (UR) at ($(current page.north east) + (-\xoffset,-\yoffset)$);
\coordinate (UL) at ($(current page.north west) + (\xoffset,-\yoffset)$);
\coordinate (LR) at ($(current page.south east) + (-\xoffset,\yoffset)$);
\coordinate (LL) at ($(current page.south west) + (\xoffset,\yoffset)$);
\coordinate (CL) at ($(current page.west) + (\xoffset,0)$);
\coordinate (CR) at ($(current page.east) + (-\xoffset,0)$);
%Ornaments located at the corners of the page
\node[anchor=north west] (ULCorner) at (UL){%
\pgfornament[width = 2cm,color = \ornamentcolor ]{\ornament}};%
\node[anchor=north east] (URCorner) at (UR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=v ]{\ornament}};%
\node[anchor=south east] (LRCorner) at (LR){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=c ]{\ornament}};%
\node[anchor=south west] (LLCorner) at (LL){%
\pgfornament[width = 2cm,color = \ornamentcolor, symmetry=h ]{\ornament}};%
%Ornaments between the corners: Calculates the distance between the 2 upper corners and uses it for the width of the bar ornament.
\path (CL) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=-3pt] (CLBar) at (CL){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\path (CR) let \p1 = ($(URCorner.north west) - (ULCorner.north east)$)
in node[anchor=center, rotate=90, inner sep=0pt] (CRBar) at (CR){\pgfornament[width=\x1 ,color = \ornamentcolor]{88}};%
\draw[line width=1.5pt,line cap=round,black]($(CLBar.east) + (0.8pt,0)$) to ($(ULCorner.south west) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.east) + (0.8pt,-1pt)$) to ($(URCorner.south east) + (0.8pt,1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CLBar.west) + (0.8pt,0)$) to ($(LLCorner.north west) + (0.8pt,-1pt)$){};
\draw[line width=1.5pt,line cap=round,black]($(CRBar.west) + (0.8pt,1)$) to ($(LRCorner.north east) + (0.8pt,-1pt)$){};
%Ornaments between he corners: Top and bottom.
\path[] (ULCorner.north east) to [ornament=88, color=black] (URCorner.north west) {};
\path (LRCorner.south west) to [ornament=88,color=black] (LLCorner.south east) {};
\end{tikzpicture}}
\backgroundsetup{scale=1,color=black,opacity=1,contents=\pageframe, angle=0,
position={current page.center},nodeanchor=center}
\begin{document}
\lipsum[1-5]
\end{document}