2

I place several images on one page using Tikz. I use geometry layout. I would like to find out exact coordinate of the left top corner of the layout page (not the real page). I have found out coordinate script, but I am not sure if it is necessary. Any suggestions are welcome. Note: The aim is to centre final Tikz picture (consisting of several images and labels) on the layout page.

\documentclass[12pt]{extbook}
% ############################## geometry
\usepackage%[showframe]
           {geometry}
\geometry
  { hmarginratio =  1:1
  , vmarginratio = 1:1
  , bindingoffset = 0cm
  , onecolumn
  , a4paper
  , layoutwidth = 50 mm
  , layoutheight = 180 mm
  , layouthoffset=\dimexpr(\paperwidth-\csname Gm@layoutwidth\endcsname)/2\relax
   , layoutvoffset=\dimexpr(\paperheight-\csname Gm@layoutheight\endcsname)/2\relax
  , showcrop
  }
\usepackage{fancyhdr}
\usepackage{mwe}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\def\parsecomma#1,#2\endparsecomma{\def\page@x{#1}\def\page@y{#2}}
\tikzdeclarecoordinatesystem{page}{
    \parsecomma#1\endparsecomma
    \pgfpointanchor{current page}{north east}
    % Save the upper right corner
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    % save the lower left corner
    \pgfpointanchor{current page}{south west}
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    % Transform to the correct placement
    \pgfmathparse{(\pgf@xc-\pgf@xb)/2.*\page@x+(\pgf@xc+\pgf@xb)/2.}
    \expandafter\pgf@x\expandafter=\pgfmathresult pt
    \pgfmathparse{(\pgf@yc-\pgf@yb)/2.*\page@y+(\pgf@yc+\pgf@yb)/2.}
    \expandafter\pgf@y\expandafter=\pgfmathresult pt
}
\makeatother

% ############################### Document
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[remember picture, overlay]
 \node[inner sep=0pt] (A) at (page cs:0,0.5)
    {\includegraphics[width=5cm]{example-image}};
 \draw[black, thick] ($(A.north west)$)  rectangle ($(A.south east)$);
 \node[inner sep=0pt] (B) at ([yshift=-71pt]A.south)
    {\includegraphics[width=5cm]{example-image}};
 \draw[black,thick] ($(B.north west)$)  rectangle ($(B.south east)$);
\end{tikzpicture}
\end{document}
chejnik
  • 1,441
  • 2
  • 23
  • 42
  • I am not sure if it is by chance, but in real document with more complicated tikzpicture, the picture is centered vertically and horizontally in layout page. – chejnik Feb 18 '16 at 14:36

1 Answers1

4

Here is another way to locate the center of the layout area. (Whether or not it is simpler...)

\documentclass[12pt]{extbook}
    % ############################## geometry
    \usepackage%[showframe]
               {geometry}

\makeatletter% rename layout variables
  \def\layoutwidth{\Gm@layoutwidth}
  \def\layoutheight{\Gm@layoutheight}
  \def\layouthoffset{\Gm@layouthoffset}
  \def\layoutvoffset{\Gm@layoutvoffset}
\makeatother

    \geometry
      { hmarginratio =  1:1
      , vmarginratio = 1:1
      , bindingoffset = 0cm
      , onecolumn
      , a4paper
      , layoutwidth = 50 mm
      , layoutheight = 180 mm
      , layouthoffset=\dimexpr(\paperwidth-\layoutwidth)/2\relax
       , layoutvoffset=\dimexpr(\paperheight-\layoutheight)/2\relax
      , showcrop
      }

    \usepackage{fancyhdr}
    \usepackage{mwe}
    \usepackage{graphicx}
    \usepackage{tikz}
    \usetikzlibrary{calc}

    \newsavebox{\tempbox}

    % ############################### Document
    \begin{document}
    \thispagestyle{empty}

    \savebox{\tempbox}{%
    \begin{tikzpicture}
     \node[inner sep=0pt] (A) at (0,0.5)
        {\includegraphics[width=5cm]{example-image}};
     \draw[black, thick] ($(A.north west)$)  rectangle ($(A.south east)$);
     \node[inner sep=0pt] (B) at ([yshift=-71pt]A.south)
        {\includegraphics[width=5cm]{example-image}};
     \draw[black,thick] ($(B.north west)$)  rectangle ($(B.south east)$);
    \end{tikzpicture}}

    \begin{tikzpicture}[remember picture, overlay]
    \path (current page.north west) ++(\layouthoffset,-\layoutvoffset)
      ++(0.5\layoutwidth,-0.5\layoutheight) coordinate(Center);
    \node[inner sep=0pt] at (Center) {\usebox{\tempbox}};
    \end{tikzpicture}
  \end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Please can you explain how it works? Many thanks – chejnik Feb 19 '16 at 06:58
  • 1
    On page 7 of the geometry manual is a diagram of how layout is handled. The values for layouthoffset etc, are stored in \Gm@layouthoff etc. Since @ is used to make it difficult for users to break things, \makeatletter or \csname have to be used to write these names. The savebox is a safe way to insert a tikzpicture inside another tikzpicture. – John Kormylo Feb 19 '16 at 15:24
  • I see. So you insert previously created whole picture using \savebox inside new tikz picture (well, nothing in it) with coordinate counted from layout variables. Many thanks for explanation. – chejnik Feb 20 '16 at 06:27
  • Are the coordinate (0,0.5) in savebox doing something? Or the coordinates can have whatever value, because the picture is centered anyway? – chejnik Mar 03 '16 at 14:16
  • Sorry, they are not needed. I just didn't notice that at the time. – John Kormylo Mar 03 '16 at 15:40
  • Thank you. I am still curious because when I change these coordinates it is not centered. Can the cause lay in naming of the nodes? Please when you have a time can you have a look at my complete code? – chejnik Mar 03 '16 at 18:31
  • Add [draw] to the node at (Center) to check if you have extra white space. (not a githup user.) – John Kormylo Mar 03 '16 at 18:55
  • Ok, now changing of those coordinates still centers the whole image. The wrong behaviour, it seems, was caused by using [remember picture, overlay] option also for the included (centered) image. – chejnik Mar 03 '16 at 19:40
  • 1
    Yup. An overlay image has no size, and the location depends on where the cursor was at the time. – John Kormylo Mar 03 '16 at 22:00