![在回忆录类中使用 TikZ 当前页面节点](https://linux22.com/image/307497/%E5%9C%A8%E5%9B%9E%E5%BF%86%E5%BD%95%E7%B1%BB%E4%B8%AD%E4%BD%BF%E7%94%A8%20TikZ%20%E5%BD%93%E5%89%8D%E9%A1%B5%E9%9D%A2%E8%8A%82%E7%82%B9.png)
我需要使用文档current page.center
中的位置memoir
将一些图像置于页面的中心。(除非有人对如何显示这样的图像有完全不同的建议。)
但是,由于我使用的修剪纸张尺寸小于原始纸张尺寸,TikZ
因此current page.center
节点无法按预期工作。
Texample.net 有一个解决方案通过重新定义当前页面边界框来适应回忆录的正面/背面和修剪布局,但我正在努力让它发挥作用。
几个不明白的问题:
它在第一页不起作用,必须有文本和分页符,否则会抛出错误
图像显示在彼此的顶部(如预期的那样),但是当我添加一个
\clearpage
或一个\pagebreak
或一个\newpage
或任何我能想到的其他东西时,它就会引发错误。
这是我的 MWE:
\documentclass[showtrims]{memoir}
\usepackage{tikz}
\usepackage{mwe}
\trimFrame
\setstocksize{11in}{8.5in}
\settrimmedsize{9in}{7in}
\checkandfixthelayout
%%%%%%%%%%%%%%%%%%%%%%%%%%%% Code From Texample.net %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usetikzlibrary{decorations.pathmorphing}
% Special current page bounding box rectangle that adapts to stock paper sizes
% and recto/verso pages
\newcommand\setpagenode{
% The original current page node code can be found in the file
% generic/pgf/modules/pgfmoduleshapes.code.tex
\expandafter\def\csname pgf@sh@ns@current page\endcsname{rectangle}
% Use a few low level Memoir macros to check if we are on an even or odd page.
\strictpagecheck
\checkoddpage
\ifoddpage%
\expandafter\def\csname pgf@sh@np@current page\endcsname{%
% Set the current page.south west coordinate
\def\southwest{\pgfpoint{\stockwidth-\paperwidth-\trimedge}%
{\stockheight-\trimtop-\paperheight}}%
% Set the current page.north east coordinate
\def\northeast{\pgfpoint{\stockwidth-\trimedge}{\stockheight-\trimtop}}%
}
\else
\expandafter\def\csname pgf@sh@np@current page\endcsname{%
\def\southwest{\pgfpoint{\trimedge}{\stockheight-\trimtop-\paperheight}}%
\def\northeast{\pgfpoint{\trimedge+\paperwidth}{\stockheight-\trimtop}}%
}
\fi
\expandafter\def\csname pgf@sh@nt@current page\endcsname{{1}{0}{0}{1}{0pt}{0pt}}
\expandafter\def\csname pgf@sh@pi@current page\endcsname{pgfpageorigin}
}
% Force recalculation of the current page node whenever the overlay option is used.
% If you page is centered on your stock paper it is only necessary to calculate
% the current page rectangle once.
\pgfkeys{/tikz/overlay/.add code={}{\setpagenode}}
%%%%%%%%%%%%%%%%%%%%%%%%% End Code From Texample.net %%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\CenterTallPhoto}[1]{
\begin{tikzpicture}[remember picture, overlay]
\node at (current page.center){\includegraphics[height=\paperheight]{#1}};
\end{tikzpicture}
}
\newcommand{\CenterWidePhoto}[1]{
\begin{tikzpicture}[remember picture, overlay]
\node at (current page.center){\includegraphics[width=\paperwidth]{#1}};
\end{tikzpicture}
}
\begin{document}
If this text is removed, I get errors.
\pagebreak
\CenterTallPhoto{example-image-10x16}
% \clearpage
\CenterWidePhoto{example-image-16x10}
\end{document}
错误:
添加时出错\clearpage
:
./TableQuestion.tex:76: Paragraph ended before \pgfpoint was complete. [ \CenterWidePhoto{example-image-16x10}]
./TableQuestion.tex:76: Missing number, treated as zero. [ \CenterWidePhoto{example-image-16x10}]
./TableQuestion.tex:76: Illegal unit of measure (pt inserted). [ \CenterWidePhoto{example-image-16x10}]
删除顶部的文本和分页符导致的错误:
./TableQuestion.tex:74: Paragraph ended before \pgfpoint was complete. [ \CenterTallPhoto{example-image-10x16}]
./TableQuestion.tex:74: Missing number, treated as zero. [ \CenterTallPhoto{example-image-10x16}]
./TableQuestion.tex:74: Illegal unit of measure (pt inserted). [ \CenterTallPhoto{example-image-10x16}]
./TableQuestion.tex:76: Paragraph ended before \pgfpoint was complete. [ \CenterWidePhoto{example-image-16x10}]
./TableQuestion.tex:76: Missing number, treated as zero. [ \CenterWidePhoto{example-image-16x10}]
./TableQuestion.tex:76: Illegal unit of measure (pt inserted). [ \CenterWidePhoto{example-image-16x10}]
我正在排版一本相当复杂的书,因此虽然每次使用这些页面节点时我都可以针对用例制定解决方法,但我的代码会变得非常混乱,而且我经常会犯一些小错误(计算错误、空格),这些错误会累积起来并导致事情出错。似乎如果我明白出了什么问题,这应该是“正确”的解决方案。
答案1
问题是,宏定义中存在大量虚假空格和段落分隔符。如果下一行留空,则注释行尾以避免虚假空格是没有意义的!TeX 会不高兴,因为您在完成无法分段的命令之前就结束了段落。
我没有具体检查哪些空格/换行符有问题。我只是随意删除了它们。如果确实需要,您可以将它们重新添加。
\documentclass[showtrims]{memoir}
\usepackage{tikz}
\trimFrame
\setstocksize{11in}{8.5in}
\settrimmedsize{9in}{7in}
\checkandfixthelayout
%%%%%%%%%%%%%%%%%%%%%%%%%%%% Code From Texample.net %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usetikzlibrary{decorations.pathmorphing}
\newcommand\setpagenode{%
\expandafter\def\csname pgf@sh@ns@current page\endcsname{rectangle}%
\strictpagecheck%
\checkoddpage%
\ifoddpage%
\expandafter\def\csname pgf@sh@np@current page\endcsname{%
\def\southwest{\pgfpoint{\stockwidth-\paperwidth-\trimedge}%
{\stockheight-\trimtop-\paperheight}}%
\def\northeast{\pgfpoint{\stockwidth-\trimedge}{\stockheight-\trimtop}}%
}%
\else
\expandafter\def\csname pgf@sh@np@current page\endcsname{%
\def\southwest{\pgfpoint{\trimedge}{\stockheight-\trimtop-\paperheight}}%
\def\northeast{\pgfpoint{\trimedge+\paperwidth}{\stockheight-\trimtop}}%
}%
\fi
\expandafter\def\csname pgf@sh@nt@current page\endcsname{{1}{0}{0}{1}{0pt}{0pt}}%
\expandafter\def\csname pgf@sh@pi@current page\endcsname{pgfpageorigin}}
\pgfkeys{/tikz/overlay/.add code={}{\setpagenode}}
%%%%%%%%%%%%%%%%%%%%%%%%% End Code From Texample.net %%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\CenterTallPhoto}[1]{%
\begin{tikzpicture}[remember picture, overlay]
\node at (current page.center){\includegraphics[height=\paperheight]{#1}};
\end{tikzpicture}%
}
\newcommand{\CenterWidePhoto}[1]{%
\begin{tikzpicture}[remember picture, overlay]
\node at (current page.center){\includegraphics[width=\paperwidth]{#1}};
\end{tikzpicture}%
}
\begin{document}
\CenterTallPhoto{example-image-10x16}
\clearpage
\CenterWidePhoto{example-image-16x10}
\end{document}