在几何布局中居中 tikz 图片

在几何布局中居中 tikz 图片

这个问题涉及几何布局中 tikz 图片的居中。该问题结合了 tex.stackexchange 中回答的两个答案。

第一个答案解释了如何将 tikz 图片定位在页面的中心。但至少在这种情况下,它没有给出正确的结果。

第二个答案在循环中创建 tikz 图片。但它从猜测的值中计算出最重要的坐标(page cs:-0.5045,0.595)

因此,tikz 图片不居中,并且打印两面时,差异显而易见。

问题是:如何将整个 tikz 图片水平和垂直居中在几何布局区域中?我不知道如何计算坐标。

梅威瑟:

\documentclass[12pt]{article}
% ############################## geometry
\usepackage{geometry}
\geometry{
 headsep = 0pt,
 headheight= 0pt, 
  hmarginratio =  1:1,
  vmarginratio = 1:1,
  bindingoffset = 0cm,
  onecolumn,
  a3paper,
  layoutwidth = 220 mm,
  layoutheight = 320 mm,
  layouthoffset=\dimexpr(\paperwidth-\csname Gm@layoutwidth\endcsname)/2\relax,
  layoutvoffset=\dimexpr(\paperheight-\csname Gm@layoutheight\endcsname)/2\relax,
  showcrop
}
\usepackage[icelandic, latin, czech]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}
\definecolor{title}{RGB}{16, 13, 32}
\usepackage{mwe}
\usepackage{XCharter}

% ############################### Document

\newcommand{\czHyphen}{\rule[.45ex]{.2em}{.11ex}}
\newcommand*{\addthinS}{\hskip0.06667em\relax}
\newcommand*{\addthinSS}{\hskip0.00007em\relax}
\def\cropmarkgap{1}% mm

\makeatletter
\def\Gm@cropmark(#1,#2,#3,#4){% #1 = x direction, #2 = y direction, #3 & #4 no longet used
  \begin{picture}(0,0)
    \setlength\unitlength{1truemm}%
    \linethickness{0.25pt}%
    \put(\the\numexpr #1*\cropmarkgap\relax,0){\line(#1,0){\the\numexpr 20-\cropmarkgap}}%
    \put(0,\the\numexpr #2*\cropmarkgap\relax){\line(0,#2){\the\numexpr 20-\cropmarkgap}}%
  \end{picture}}%
\makeatother

\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

\usepackage{eso-pic}
\usepackage{tikzpagenodes}
%\AddToShipoutPicture{\drawbackground}
\newcommand{\shiftleft}{\hspace*{-0.55\dimexpr\csname Gm@layoutwidth\endcsname-\textwidth\relax}}
\newcommand{\shiftup}{\vspace*{-0.13\dimexpr\csname Gm@layoutheight\endcsname-\textwidth\relax}}


%%% Define "Array" interface
\makeatletter
    \newcounter{imgs}
    \setcounter{imgs}{0}
    %#1 is the image
    %#2 is the title
    %#3 is the color
    \newcommand{\addimg}[3]{%
        \stepcounter{imgs}%
        \@namedef{imgimage\theimgs}{#1}%
        \@namedef{imgtitle\theimgs}{#2}%
        \@namedef{imgcolor\theimgs}{#3}}
    \newcommand{\getimage}[1]{\expandafter\@nameuse\expandafter{imgimage#1}}%
    \newcommand{\gettitle}[1]{\expandafter\@nameuse\expandafter{imgtitle#1}}%
    \newcommand{\getcolor}[1]{\expandafter\@nameuse\expandafter{imgcolor#1}}%
\makeatother

%%% Define Cards
\addimg{example-image}{one}{blue}%
\addimg{example-image}{two}{green}%
\addimg{example-image}{three}{red}%
\addimg{example-image}{four}{yellow}%
\addimg{example-image}{five}{orange}%
\addimg{example-image}{six}{yellow}%
\addimg{example-image}{seven}{red}%
\addimg{example-image}{eight}{brown}%
\addimg{example-image}{nine}{green}%
\addimg{example-image}{ten}{blue}%
\addimg{example-image}{eleven}{green}%
\addimg{example-image}{twelve}{red}%
\addimg{example-image}{thirteen}{yellow}%
\addimg{example-image}{fourteen}{orange}%
\addimg{example-image}{fifteen}{yellow}%
\addimg{example-image}{sixteen}{red}%
\addimg{example-image}{seventeen}{brown}%
\addimg{example-image}{eighteen}{green}%
\addimg{example-image}{nineteen}{blue}%
\addimg{example-image}{twenty}{green}%
\addimg{example-image}{twenty one}{red}%
\addimg{example-image}{twenty two}{yellow}%
\addimg{example-image}{twenty three}{orange}%
\addimg{example-image}{twenty four}{yellow}%
\addimg{example-image}{twenty five}{red}%
\addimg{example-image}{twenty six}{brown}%
\addimg{example-image}{twenty seven}{green}%
\addimg{example-image}{twenty eight}{blue}%
\addimg{example-image}{twenty nine}{green}%
\addimg{example-image}{thirty}{red}%
\addimg{example-image}{thirty one}{yellow}%
\addimg{example-image}{thirty two}{orange}%


%%% Global Setup
\newcommand\xspacing{71pt}%<== space between the images
\newcommand\yspacing{71pt}%<== vertical space between rows
\newcommand\imgperrow{4}%<== number of images per row
\tikzset{p_title/.style={text centered, minimum height=0.6cm, minimum width=5cm, font=\bfseries}}
\tikzset{p_title_line/.style={ultra thin, color=violet}}

%%% Define primary for loop
\newcommand{\forloop}[2]{%
    \foreach [count=\i] \x in {#1,...,#2}%<==loop for each image in the array
    {
        \edef\gonode{\noexpand\node[inner sep=0pt] (B) at (A) {\noexpand\includegraphics[width=5cm]{\getimage{\x}}};}%<==Edit to expand the file name
         \gonode%\node [inner sep=0pt] (B) at (A) {\includegraphics[width=5cm]{\getimage{\x}}};%
        \draw [black, ultra thick] ($(B.north west)$) rectangle ($(B.south east)$);%
        \node [p_title] (AA) at ($(B.north west)+(2.5,-0.30)$) {\gettitle{\x}};%
        \draw [p_title_line, color=\getcolor{\x}](AA.south west) -- (AA.south east);%
        \pgfmathparse{Mod(\i,\imgperrow)==0?1:0};%
        \ifnum\pgfmathresult>0
            \coordinate (left) at ([yshift=-\yspacing]left);
            \path let \p1=(left),\p2=(B.south) in coordinate (A) at (\x1,\y2-\yspacing);
        \else   
            \coordinate (A) at ([xshift=\xspacing]B.east);%
        \fi
    }}
%%% Define the for loop for 2 images per row on third page
\newcommand{\forlooptwo}[2]{%
    \begingroup
    \def\imgperrow{2}
    \let\originalxspacing\xspacing
    \def\xspacing{5*\originalxspacing}
    \forloop{#1}{#2}\endgroup}

\begin{document}
    \pagestyle{empty}
    %first page
    \begin{tikzpicture}[remember picture, overlay]
        \coordinate (A) at (page cs:-0.5045,0.595);
        \coordinate (left) at (A);
        \forloop{1}{24}
    \end{tikzpicture}

    %second page
    \clearpage
    \begin{tikzpicture}[remember picture, overlay]
        \coordinate (A) at (page cs:-0.5045,0.595);
        \coordinate (left) at (A);
        \forloop{25}{32}
        \forloop{1}{16}
    \end{tikzpicture}

    %third page
    \clearpage
    \begin{tikzpicture}[remember picture, overlay]
        \coordinate (A) at (page cs:-0.5045,0.595);
        \coordinate (left) at (A);
        \forloop{17}{20}
        \forlooptwo{21}{28}
        \forloop{29}{32}
    \end{tikzpicture}
\end{document}

答案1

incgraph该包允许将内容置于页面中央。

因此,我补充说

\usepackage{incgraph}

并将图片封装到inctext环境中。

\igrset{currentpaper}
\begin{inctext}
\begin{tikzpicture}
    \coordinate (A) at (page cs:-0.5045,0.595);
    \coordinate (left) at (A);
    \forloop{1}{24}
\end{tikzpicture}
\end{inctext}

整个代码如下:

\documentclass[12pt]{article}
% ############################## geometry
\usepackage{geometry}
\geometry{
 headsep = 0pt,
 headheight= 0pt,
  hmarginratio =  1:1,
  vmarginratio = 1:1,
  bindingoffset = 0cm,
  onecolumn,
  a3paper,
  layoutwidth = 220 mm,
  layoutheight = 320 mm,
  layouthoffset=\dimexpr(\paperwidth-\csname Gm@layoutwidth\endcsname)/2\relax,
  layoutvoffset=\dimexpr(\paperheight-\csname Gm@layoutheight\endcsname)/2\relax,
  showcrop
}
\usepackage[icelandic, latin, czech]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}
\definecolor{title}{RGB}{16, 13, 32}
\usepackage{mwe}
\usepackage{XCharter}
\usepackage{incgraph}

% ############################### Document

\newcommand{\czHyphen}{\rule[.45ex]{.2em}{.11ex}}
\newcommand*{\addthinS}{\hskip0.06667em\relax}
\newcommand*{\addthinSS}{\hskip0.00007em\relax}
\def\cropmarkgap{1}% mm

\makeatletter
\def\Gm@cropmark(#1,#2,#3,#4){% #1 = x direction, #2 = y direction, #3 & #4 no longet used
  \begin{picture}(0,0)
    \setlength\unitlength{1truemm}%
    \linethickness{0.25pt}%
    \put(\the\numexpr #1*\cropmarkgap\relax,0){\line(#1,0){\the\numexpr 20-\cropmarkgap}}%
    \put(0,\the\numexpr #2*\cropmarkgap\relax){\line(0,#2){\the\numexpr 20-\cropmarkgap}}%
  \end{picture}}%
\makeatother

\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

\usepackage{eso-pic}
\usepackage{tikzpagenodes}
%\AddToShipoutPicture{\drawbackground}
\newcommand{\shiftleft}{\hspace*{-0.55\dimexpr\csname Gm@layoutwidth\endcsname-\textwidth\relax}}
\newcommand{\shiftup}{\vspace*{-0.13\dimexpr\csname Gm@layoutheight\endcsname-\textwidth\relax}}


%%% Define "Array" interface
\makeatletter
    \newcounter{imgs}
    \setcounter{imgs}{0}
    %#1 is the image
    %#2 is the title
    %#3 is the color
    \newcommand{\addimg}[3]{%
        \stepcounter{imgs}%
        \@namedef{imgimage\theimgs}{#1}%
        \@namedef{imgtitle\theimgs}{#2}%
        \@namedef{imgcolor\theimgs}{#3}}
    \newcommand{\getimage}[1]{\expandafter\@nameuse\expandafter{imgimage#1}}%
    \newcommand{\gettitle}[1]{\expandafter\@nameuse\expandafter{imgtitle#1}}%
    \newcommand{\getcolor}[1]{\expandafter\@nameuse\expandafter{imgcolor#1}}%
\makeatother

%%% Define Cards
\addimg{example-image}{one}{blue}%
\addimg{example-image}{two}{green}%
\addimg{example-image}{three}{red}%
\addimg{example-image}{four}{yellow}%
\addimg{example-image}{five}{orange}%
\addimg{example-image}{six}{yellow}%
\addimg{example-image}{seven}{red}%
\addimg{example-image}{eight}{brown}%
\addimg{example-image}{nine}{green}%
\addimg{example-image}{ten}{blue}%
\addimg{example-image}{eleven}{green}%
\addimg{example-image}{twelve}{red}%
\addimg{example-image}{thirteen}{yellow}%
\addimg{example-image}{fourteen}{orange}%
\addimg{example-image}{fifteen}{yellow}%
\addimg{example-image}{sixteen}{red}%
\addimg{example-image}{seventeen}{brown}%
\addimg{example-image}{eighteen}{green}%
\addimg{example-image}{nineteen}{blue}%
\addimg{example-image}{twenty}{green}%
\addimg{example-image}{twenty one}{red}%
\addimg{example-image}{twenty two}{yellow}%
\addimg{example-image}{twenty three}{orange}%
\addimg{example-image}{twenty four}{yellow}%
\addimg{example-image}{twenty five}{red}%
\addimg{example-image}{twenty six}{brown}%
\addimg{example-image}{twenty seven}{green}%
\addimg{example-image}{twenty eight}{blue}%
\addimg{example-image}{twenty nine}{green}%
\addimg{example-image}{thirty}{red}%
\addimg{example-image}{thirty one}{yellow}%
\addimg{example-image}{thirty two}{orange}%


%%% Global Setup
\newcommand\xspacing{71pt}%<== space between the images
\newcommand\yspacing{71pt}%<== vertical space between rows
\newcommand\imgperrow{4}%<== number of images per row
\tikzset{p_title/.style={text centered, minimum height=0.6cm, minimum width=5cm, font=\bfseries}}
\tikzset{p_title_line/.style={ultra thin, color=violet}}

%%% Define primary for loop
\newcommand{\forloop}[2]{%
    \foreach [count=\i] \x in {#1,...,#2}%<==loop for each image in the array
    {
        \edef\gonode{\noexpand\node[inner sep=0pt] (B) at (A) {\noexpand\includegraphics[width=5cm]{\getimage{\x}}};}%<==Edit to expand the file name
         \gonode%\node [inner sep=0pt] (B) at (A) {\includegraphics[width=5cm]{\getimage{\x}}};%
        \draw [black, ultra thick] ($(B.north west)$) rectangle ($(B.south east)$);%
        \node [p_title] (AA) at ($(B.north west)+(2.5,-0.30)$) {\gettitle{\x}};%
        \draw [p_title_line, color=\getcolor{\x}](AA.south west) -- (AA.south east);%
        \pgfmathparse{Mod(\i,\imgperrow)==0?1:0};%
        \ifnum\pgfmathresult>0
            \coordinate (left) at ([yshift=-\yspacing]left);
            \path let \p1=(left),\p2=(B.south) in coordinate (A) at (\x1,\y2-\yspacing);
        \else
            \coordinate (A) at ([xshift=\xspacing]B.east);%
        \fi
    }}
%%% Define the for loop for 2 images per row on third page
\newcommand{\forlooptwo}[2]{%
    \begingroup
    \def\imgperrow{2}
    \let\originalxspacing\xspacing
    \def\xspacing{5*\originalxspacing}
    \forloop{#1}{#2}\endgroup}

\begin{document}
    \pagestyle{empty}
    %first page
    \igrset{currentpaper}
    \begin{inctext}
    \begin{tikzpicture}
        \coordinate (A) at (page cs:-0.5045,0.595);
        \coordinate (left) at (A);
        \forloop{1}{24}
    \end{tikzpicture}
    \end{inctext}

    %second page
    \clearpage
    \begin{inctext}
    \begin{tikzpicture}
        \coordinate (A) at (page cs:-0.5045,0.595);
        \coordinate (left) at (A);
        \forloop{25}{32}
        \forloop{1}{16}
    \end{tikzpicture}
    \end{inctext}

    %third page
    \clearpage
    \begin{inctext}
    \begin{tikzpicture}
        \coordinate (A) at (page cs:-0.5045,0.595);
        \coordinate (left) at (A);
        \forloop{17}{20}
        \forlooptwo{21}{28}
        \forloop{29}{32}
    \end{tikzpicture}
    \end{inctext}
\end{document}

相关内容