5

I need to use the current page.center position in a memoir document to center some images on a page. (unless someone has an entirely different suggestion about how to display images like this.)

However, because I am using a trimmed paper size smaller than the stock paper size, TikZ's current page.center node doesn't work as intended.

Texample.net has a solution by redefining the current page bounding box to fit memoir's recto/verso and trimmed layout, but I'm struggling to get it to work.

Several problems I don't understand:

  • It doesn't work on the first page, there must be text and a page break or it'll throw errors

  • The images display on top of one another (as expected), but when I add a \clearpage or a \pagebreak or a \newpage or anything else I can think of, it throws errors.

Here's my 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}

Errors:

Errors from adding \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}]

Errors from removing text and page break at the top:

./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}]

I'm typesetting a pretty complicated book, so while I could make a use-case-specific work around for every time I use these page nodes, my code would get really messy and I constantly have little mistakes (miscalculations, spaces) that build up and throw things off. Seems like if I just understood what is going wrong, this should be the "right" solution.

2
  • Why are you using overlay when you don't (apparently) have any (other) content? This is designed for use when you want to overlay the picture with the main content. But that doesn't seem to be the idea here. (If you want the images to overlap, you might want overlay for one of them. But it seems you don't want that either if you want to clear the page between them?)
    – cfr
    Commented Mar 16, 2015 at 17:52
  • @cfr There will be page numbers, labels, tables, and other content underneath the images on the pages, I took things out to shorten the MWE and because I need the images (and a few tables using the same technique) to stay in place independent from whatever other content lands on the page. The two images should be on separate pages but overlayed over any content on those pages.
    – Ryan
    Commented Mar 16, 2015 at 17:58

1 Answer 1

5

The problem is that you have a bunch of spurious spaces and paragraph breaks in your definitions of macros. There is no point commenting the end of a line to avoid a spurious space if you leave the next line blank! TeX then gets unhappy because you are ending paragraphs before finishing commands which cannot take paragraph breaks.

I didn't check specifically which spaces/line breaks were problematic. I just removed them indiscriminately. If any are actually desired, you can add them back in.

\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}

overlay images

2
  • Linebreaks and spaces, the bane of my TeX existence. Thank you!
    – Ryan
    Commented Mar 16, 2015 at 18:13
  • @Ryan It's not the little things - it's the invisible ones ;).
    – cfr
    Commented Mar 16, 2015 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .