我有一个 .tex 文档,我用它来通过 pdflatex 生成 .pdf 文件,它工作正常,但我想知道为什么为了正确显示所有 tikz 图像,我总是必须调用 pdflatex 两次。这种奇怪的行为有什么原因吗?
pdflatex myfile.tex .
pdflatex myfile.tex .
pdfTeX, Version 3.14159265-2.6-1.40.17
下面是一个代码示例:
\documentclass[english,12pt,a4paper]{article}
\usepackage[left=48pt,right=46pt]{geometry}
\usepackage[latin9]{inputenc}
\usepackage{imakeidx}
\usepackage{graphicx}
\usepackage{epigraph}
\usepackage{babel}
\usepackage{tikz}% loads also graphicx, xcolor
\usepackage{lmodern}
\usepackage{scrbase}
\usepackage{scrlayer}[31/10/2013]
\usepackage{hyperref}
\hypersetup{pdftex,colorlinks=true,allcolors=blue}
\usepackage{hypcap}
\newkomafont{myheadfoot}{\color{white}\bfseries\sffamily}
\newkomafont{myhead}{\Huge}
\newkomafont{mypagenumber}{\normalsize}
\newkomafont{mysepline}{\color{blue}}
\newcommand\headimageheight{2cm}
\newcommand\footimageheight{.5cm}
\newcommand\seplineheight{.2cm}
\newcommand\headtexthoffset{1cm}
\renewcommand\epigraphflush{flushright}
\renewcommand\epigraphsize{\normalsize}
\setlength\epigraphwidth{0.7\textwidth}
\renewcommand\sectionmark[1]{\markright{#1}}
\newcommand\titleinhead{}
\newcommand\mytitle[1]{\title{#1}\renewcommand\titleinhead{#1}}
\newsavebox\headimage
\sbox\headimage{%
\tikz{
\clip(0,0)rectangle(\paperwidth,-\headimageheight);
\node[inner sep=0pt,outer sep=0pt,anchor=north west]{%
\includegraphics[width=\paperwidth]{myHeaderPicture}};
}%
\llap{\usekomafont{mysepline}\rule[-\seplineheight]{\paperwidth}{\seplineheight}}%
}
\newsavebox\footimage
\sbox\footimage{%
\tikz{
\clip(0,0)rectangle(\paperwidth,\footimageheight);
\node[inner sep=0pt,outer sep=0pt,anchor=south west]{%
\includegraphics[width=\paperwidth]{myFooterPicture}};
}%
\llap{\usekomafont{mysepline}\rule[\footimageheight]{\paperwidth}{\seplineheight}}%
}
\DeclareNewLayer[
background,
area={0pt}{0pt}{\paperwidth}{\headimageheight},
contents={%
\parbox[c][\layerheight]{\layerwidth}{%
\vspace*{\stretch{2}}
\hspace*{\headtexthoffset}%
\usekomafont{myheadfoot}\usekomafont{myhead}\smash{\MakeUppercase{%
\ifodd\value{page}\titleinhead\else\rightmark\fi%
}}%
\vspace*{\stretch{1}}}%
%}%
}
]{headtext}
\DeclareNewLayer[
clone=headtext,
%% addheight=\seplineheight,
contents={\usebox\headimage}
]{headimage}
\DeclareNewLayer[
background,
align=b,
area={0pt}{\paperheight}{\paperwidth}{\footimageheight},
contents={%
\parbox[c][\layerheight][c]{\layerwidth}{\centering\usekomafont{myheadfoot}\usekomafont{mypagenumber}\thepage}%
}
]{foottext}
\DeclareNewLayer[
clone=foottext,
%% addheight=\seplineheight,
contents={\parbox{\layerwidth}{\usebox\footimage}}
]{footimage}
\DeclarePageStyleByLayers{headfootimage}{%
headimage,headtext,%
footimage,foottext%
}
\definecolor{titlepagecolor}{cmyk}{1,.60,0,.40}
\definecolor{namecolor}{cmyk}{1,.50,0,.10}
\newcommand\titlepagedecoration{%
\begin{tikzpicture}[remember picture,overlay,shorten >= -10pt]
\coordinate (aux1) at ([yshift=-15pt]current page.north east);
\coordinate (aux2) at ([yshift=-410pt]current page.north east);
\coordinate (aux3) at ([xshift=-4.5cm]current page.north east);
\coordinate (aux4) at ([yshift=-150pt]current page.north east);
\begin{scope}[titlepagecolor!40,line width=12pt,rounded corners=12pt]
\draw
(aux1) -- coordinate (a)
++(225:5) --
++(-45:5.1) coordinate (b);
\draw[shorten <= -10pt]
(aux3) --
(a) --
(aux1);
\draw[opacity=0.6,titlepagecolor,shorten <= -10pt]
(b) --
++(225:2.2) --
++(-45:2.2);
\end{scope}
\draw[titlepagecolor,line width=8pt,rounded corners=8pt,shorten <= -10pt]
(aux4) --
++(225:0.8) --
++(-45:0.8);
\begin{scope}[titlepagecolor!70,line width=6pt,rounded corners=8pt]
\draw[shorten <= -10pt]
(aux2) --
++(225:3) coordinate[pos=0.45] (c) --
++(-45:3.1);
\draw
(aux2) --
(c) --
++(135:2.5) --
++(45:2.5) --
++(-45:2.5) coordinate[pos=0.3] (d);
\draw
(d) -- +(45:1);
\end{scope}
\end{tikzpicture}%
}
\makeindex
\begin{document}
% Title Page
\begin{titlepage}
\newgeometry{left=6.5cm} % defines the geometry for the titlepage
\pagecolor{titlepagecolor}
\noindent
\includegraphics[width=10cm]{myLogoImage}\\[-1em]
\color{white}
\makebox[0pt][l]{\rule{1.3\textwidth}{1pt}}
\par
\noindent
\textbf{\textsf{MyName}} \textcolor{namecolor}{\textsf{MyFamilyName}}
% \noindent
\titlepagedecoration
\vfill
\noindent
{\huge \textsf{My Title}}
\vskip\baselineskip
\noindent
\end{titlepage}
% Title Page End
\restoregeometry % restores the geometry
\nopagecolor% Use this to restore the color pages to white
\end{document}
答案1
TeX 的基本块是水平盒和垂直盒。你写或画的任何东西都会被放在某个垂直盒中的某个水平盒里。只有在 TeX 完成换行和分页后,每个盒子才会被分配一个位置。而且只有在你完成当前的图片/水平盒/垂直盒后,TeX 才会进行换行/分页。所以存在因果依赖关系,你必须编译两次。
上述论证隐含着一个假设,即当前 Ti 的位置钾Z 图片很重要。通常情况下,人们喜欢写
\tikz[remember picture,overlay](0,0)--(current page.center);
那么 Ti钾Z 所做的就是计算相(current page.center)
对于当地的协调。
当我们只想使用绝对坐标。我们知道脚注和页眉页脚是不需要二次编译的,第一次编译的时候就已经放到正确的位置了。
所以你的问题的答案实际上是
不,没有理由。
我们可以完成第一次编译所做的工作。
例如:
\documentclass{article}
\usepackage{tikz}
\pgfsyspdfmark{pgfid1}{8799518}{45527859}
\begin{document}
\newbox\mybox
\setbox\mybox=\hbox{%
\tikz[remember picture,overlay]{
\draw++(current page.north east)+(-.1,-.1)--+(-1,-1)
node[below left]{this is the top right corenr};
}%
}
\pagestyle{headings}
\markright{\box\mybox}
\leavevmode
\end{document}
请注意,我仍在使用[remember picture,overlay]
。并且我添加了猜测图片位置\pgfsyspdfmark{pgfid1}{8799518}{45527859}
。这表示页眉基线左端点的位置,可以手工计算。
具体来说,8799518
是缩放点的 x 坐标,即
一英寸++ .
\hoffset
5+\marginparwidth
.\marginparsep
是45527859
缩放点的 y 坐标,即
\paperheight
- 一英寸 -\voffset
-\topmargin
-\headheight
。
看https://en.wikibooks.org/wiki/LaTeX/Page_Layout
一种奇特的方式
这是特定于编译器的。
基本思想是除了盒子之外,pdfTeX 还提供了一种访问绝对坐标的方法。其中samplepdf.tex
有一个例子:
% pdfTeX keeps the origin point of coordinates in the lower-left corner of the % page (native PDF space origin). \pdfliteral used without `direct' keyword % moves this position temporarily to the point user probably expect; current % position on the page. That is why the rectangle above appears in-line. % Drawing anything relative to the page origin is a bit problematic. In most % cases, one can't simply say `\pdfliteral direct {q <paiting operators> Q}'. % That is because q..Q operators can't appear inside BT..ET blocks. % % Starting from pdfTeX 1.30.0, another \pdfliteral modifier can be used instead % of `direct'. If the keyword is `page', pdfTeX closes internal text block with `ET' % before putting the literal content; like with `direct', without translation. \feature 1.30.0 {\cs\pdfliteral{ \tt page} keyword} ...and now look at the lower-left corner of the page.\par \pdfliteral page {q 1 j 0 1 0 rg 0 0 1 RG 0 0 10 10 re B Q} \pdfliteral page {q -1 0 0 1 612 0 cm .5 g}\hfill Do you like that?\pdfliteral page {Q} \endfeature
但我们这些人不会说 PDF 的语言,只会说 PGF/Ti钾Z 确实如此。因此,我们可以以某种方式重写 PGF 的系统层并获得:
\documentclass{article}
\usepackage{tikz}
\begin{document}
(You have to say something in order to setup font)
(Use {\tt\string\leavevmode} for text-free graphics)
(See the next page to see what happened on this page)
\makeatletter
\def\myliteralstring{}
\def\pgfsys@invoke#1{\g@addto@macro\myliteralstring{#1}}
\def\pgftext#1{
\pgfsys@invoke{
q
}
\pgftransformshift{\pgfqpoint{\pgf@path@lastx}{\pgf@path@lasty}}\pgflowlevelsynccm
\pgfsys@invoke{
4 Tw BT [(#1)] TJ ET
Q
}
}
\def\tikz@fig ode#1;{
\endgroup
\pgftext{#1}
}
\tikz[line width=1]{
\draw[<-,red](.1,.1)--(.9,.9);
\path(1,1)node{this is our origin};
\draw[<-]++(8.5in,11in)+(-.1,-.1)--+(-.9,-.9);
\path++(8.5in,11in)+(-5,-1)node{this the opposite corner};
\draw[<-]++(8.5in/2,11in/2)circle(1);
\pgftext{this is the center of the page}
}
\pdfliteral page{
q
\myliteralstring
Q
}
\clearpage
\myliteralstring
\end{document}
这是有问题的。但总比没有好。
旧答案
其实有一个非常原始的方法可以避免双重编译。它实际上避免使用 Ti钾Z。希望你喜欢它。
\documentclass{article}
\begin{document}
\leavevmode
\pdfliteral page {
q 1 0.6 0 0.4 k 1 0.6 0 0.4 K n 0 0 597.50787 845.04684 re f Q
0 g 0 G
0 g 0 G
0 g 0 G
1 g 1 G
q
1 0 0 1 184.252 729.405 cm
[]0 d 0 J 0.996 w 0 0 m 418.252 0 l S
Q
1 0 0 1 317.796 714.461 cm
q
1 G
1 g
0.3985 w
q
q
0.4 0.24 0 0.16 k 0.4 0.24 0 0.16 K
11.95532 w
277.48364 112.48627 m
185.71669 20.71877 l
181.0475 16.04987 181.0475 8.48029 185.71669 3.81137 c
286.53308 -97.00525 l
S
q
Q
q
Q
142.25674 133.86983 m
218.21884 70.0652 l
223.27463 65.81834 231.1581 66.16046 235.8273 70.82938 c
284.5284 119.53102 l
S
q
/[email protected] gs
/[email protected] gs
1 0.6 0 0.4 k 1 0.6 0 0.4 K
286.53308 -82.91574 m
243.84503 -125.60405 l
239.17584 -130.27295 239.17584 -137.84253 243.84503 -142.51144 c
286.53308 -185.19974 l
S
0.4 0.24 0 0.16 k 0.4 0.24 0 0.16 K
Q
1 g 1 G
Q
q
1 0.6 0 0.4 k 1 0.6 0 0.4 K
7.97021 w
284.5287 -14.96634 m
267.08464 -32.41034 l
263.97192 -35.52295 263.97192 -40.56934 267.08464 -43.68193 c
284.5287 -61.12593 l
S
1 g 1 G
Q
q
0.7 0.42 0 0.28 k 0.7 0.42 0 0.28 K
5.97766 w
284.5284 -273.99832 m
222.98709 -335.54 l
219.8743 -338.65259 219.8743 -343.69899 222.98709 -346.81158 c
286.53278 -410.35764 l
S
q
Q
277.48364 -281.04308 m
256.06015 -302.46681 l
252.94743 -305.5794 247.90076 -305.5794 244.78795 -302.46681 c
205.94989 -263.62802 l
202.83708 -260.51541 202.83708 -255.46902 205.94989 -252.35641 c
244.78795 -213.51764 l
247.90076 -210.40503 252.94748 -210.40503 256.06027 -213.51764 c
307.57925 -265.03697 l
S
q
Q
265.45738 -222.9151 m
292.54681 -195.82611 l
S
1 g 1 G
Q
Q
n
Q
1 0 0 1 -317.796 -714.461 cm
}
\end{document}