新页面出现一些奇怪的行为(感谢 David Carlisle 指出这不仅仅是\clearpage
)\hypersetup
这会破坏后续文档的能力。
我在新页面之后生成元数据,因此我需要以某种方式存储这些数据以供下次运行。
详情请见下面的演示。
代码
\documentclass{article}
\usepackage{fontspec} % typeset with xelatex
\usepackage{hyperref}
\newcommand\insertproducer{placeholder}
\hypersetup{pdfproducer=Tester Schmoe (set in preamble)}
\AtEndDocument{%
\renewcommand\insertproducer{Tester Schmidt (set in body just before end)}
\hypersetup{pdfproducer=\insertproducer}
}%
\begin{document}
\null
%\clearpage % <-- uncommenting this results in \hypersetup failure
\end{document}
pdfinfo test.pdf
输出(注释\clearpage
)
预期输出。
Creator: LaTeX with hyperref package
Producer: Tester Schmidt (set in body just before end)
CreationDate: Tue Jun 7 12:13:48 2016
Tagged: no
Pages: 1
Encrypted: no
Page size: 612 x 792 pts (letter)
File size: 2149 bytes
Optimized: no
PDF version: 1.5
pdfinfo test.pdf
输出(未注释\clearpage
)
意外的输出。
Creator: LaTeX with hyperref package
Producer: Tester Schmoe (set in preamble)
CreationDate: Tue Jun 7 12:16:44 2016
Tagged: no
Pages: 1
Encrypted: no
Page size: 612 x 792 pts (letter)
File size: 2138 bytes
Optimized: no
PDF version: 1.5
答案1
您可以通过多种方式简化您的设置。
\documentclass{article}
\usepackage{hyperref}
\hypersetup{pdfproducer=Tester Schmitty (set in preamble)}
\AtBeginDocument{%
\hypersetup{
pdfproducer = \myproducer,
}
}
\makeatletter
\newcommand*\myhypersetuptoaux[1]{% command to tell LaTeX to save the value for the next run
\AtEndDocument{%
\immediate\write\@auxout{\string\my@producer{#1}}%
}%
}
\newcommand*{\my@producer}[1]{\protected@xdef\myproducer{#1}}
\newcommand*{\myproducer}{% initial value
This baby needs extra TLC, run it again.%
}
\makeatother
\begin{document}
\null
\clearpage
\myhypersetuptoaux{Tester Schmoe}
\end{document}
pdfinfo
这是我第一次跑步后得到的
Title:
Subject:
Keywords:
Author:
Creator: LaTeX with hyperref package
Producer: This baby needs extra TLC, run it again.
CreationDate: Thu Jun 9 09:17:16 2016
ModDate: Thu Jun 9 09:17:16 2016
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 612 x 792 pts (letter) (rotated 0 degrees)
File size: 8416 bytes
Optimized: no
PDF version: 1.5
这是我第二次运行后得到的结果:
Title:
Subject:
Keywords:
Author:
Creator: LaTeX with hyperref package
Producer: Tester Schmoe
CreationDate: Thu Jun 9 09:20:39 2016
ModDate: Thu Jun 9 09:20:39 2016
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 612 x 792 pts (letter) (rotated 0 degrees)
File size: 8389 bytes
Optimized: no
PDF version: 1.5
答案2
AUX 文件解决方案
这可能会被简化,也可能存在缺点。我刚刚开始尝试写入/读取 AUX 文件。
需要特别注意的是,必须在文档早期设置某些事项:
\hypersetup{
colorlinks=true,
linkcolor=red,
urlcolor=red,
hyperfootnotes=false,
hypertexnames,
bookmarks=true % Causes clash if hyperref parameters loaded before bookmark, because bookmark loads hyperref without any parameters
}
- 这会将元数据写入辅助文件,如下所示:
例如
\mysetproducer{Tester Schmoe}
- 下一次运行将立即解析 aux
\begin{document}
并执行:
例如
\mysetproducer{Tester Schmoe}
这反过来又创建了一个my@pdfproducer
包含Tester Schmoe
例如
\expandafter\xdef\csname my@pdfproducer\endcsname{#1}}
- 如果宏存在,用户级命令
\myproducer
将获取该值(仅在创建辅助文件后 - 即至少运行一次后)
例如
\expandafter\ifx\csname my@pdfproducer\endcsname\relax This baby needs extra TLC, run it again.\else
\csname my@pdfproducer\endcsname\fi}
完整代码
\documentclass{article}
\usepackage{fontspec} % typeset with xelatex
\usepackage{hyperref}
\usepackage{atveryend}
\hypersetup{pdfproducer=Tester Schmitty (set in preamble)}
\AtBeginDocument{%
\hypersetup{
pdfproducer = \myproducer{}
}
}
\makeatletter
\newcommand*\myhypersetuptoaux[1]{% command to tell LaTeX to save the value for the next run
%\AfterLastShipout{%
\immediate\write\@auxout{%
%\protected@write\@mainaux{}{%
\string\mysetproducer{#1}%
%}%
}%
%}%
}
\makeatother
\makeatletter
\newcommand*{\mysetproducer}[1]{% called by the aux file and creates command my@producer that expands to input of myhypersetuptoaux
\expandafter\xdef\csname my@pdfproducer\endcsname{#1}}
\makeatother
\makeatletter
\newcommand*{\myproducer}{% user-level command to retrieve value of my@pdfproducer
\expandafter\ifx\csname my@pdfproducer\endcsname\relax This baby needs extra TLC, run it again.\else
\csname my@pdfproducer\endcsname\fi}
% \@ifundefined{stored@#1}{???}{\csname stored@#1\endcsname}%
\makeatother
\begin{document}
\null
\clearpage
\myhypersetuptoaux{Tester Schmoe}
\end{document}