2

I want to put cards on pages, e.g. 4 cards per page of which I have a lot. These cards should be arranged as a e.g. 2x2-grid filling up the text area which will be set to the printable of the printer. My first try was the following MWE:

\documentclass{article}
\usepackage[margin=1cm]{geometry}

\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tikzpagenodes}

\newlength{\cardxlen}
\newlength{\cardylen}

\newcommand{\numberofxcards}{2}
\newcommand{\numberofycards}{2}
\pgfmathsetlength{\cardxlen}{\textwidth/\numberofxcards}
\pgfmathsetlength{\cardylen}{\textheight/\numberofycards}

\begin{document}
\foreach\myindex in {1,2,...,10}{%
\pgfmathsetmacro{\cardxnum}{Mod(\myindex-1,\numberofxcards)}
\pgfmathsetmacro{\cardynum}{floor(divide(\myindex-1,\numberofxcards))}
\begin{tikzpicture}[remember picture,overlay]
\draw[fill=gray]($(current page text area.north west)+(\cardxnum*\cardxlen,-\cardynum*\cardylen)$)rectangle++(\cardxlen,-\cardylen);
\end{tikzpicture}
\pgfmathsetmacro{\currentrest}{int(Mod(\myindex,\numberofxcards*\numberofycards))}
\ifnumequal{\currentrest}{0}{\clearpage}{}
}

\end{document}

If I compile this several times, the first page will be printed correctly but on the following pages, the cards are set to low (the vertical position seeming to be calculated as the last vertical position on the preceding page). But as I understand it, at the time the next 'tikzpicture' begins, 'TeX' is already at the next page.

How can I make 'TikZ' calculate the position on the real new page? Or is this just a bug?

2
  • Probably your division is not precise enough so things are pushed to next page.
    – percusse
    Sep 26, 2017 at 21:54
  • @percusse That doesn't seem to bother. Just adding \currentrest to the end of the \foreach-loop will print 0 1 2 3, just as it should.
    – Toscho
    Sep 26, 2017 at 22:13

2 Answers 2

3

I think you have to use

\pgfmathsetmacro{\cardynum}
  {Mod(floor(divide(\myindex-1,\numberofxcards)),\numberofycards)}

for the calculation of \cardynum.

enter image description here

enter image description here

Code:

\documentclass{article}
\usepackage[margin=1cm]{geometry}

\usepackage{etoolbox}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\newlength{\cardxlen}
\newlength{\cardylen}

\newcommand{\numberofxcards}{2}
\newcommand{\numberofycards}{2}
\pgfmathsetlength{\cardxlen}{\textwidth/\numberofxcards}
\pgfmathsetlength{\cardylen}{\textheight/\numberofycards}

\begin{document}
\foreach\myindex in {1,2,...,10}{%
  \pgfmathsetmacro{\cardxnum}
    {Mod(\myindex-1,\numberofxcards)}
  \pgfmathsetmacro{\cardynum}
    {Mod(floor(divide(\myindex-1,\numberofxcards)),\numberofycards)}
  \begin{tikzpicture}[remember picture,overlay]
    \draw[fill=gray]
      ($(current page text area.north west)+(\cardxnum*\cardxlen,-\cardynum*\cardylen)$)
      rectangle
      ++(\cardxlen,-\cardylen)
    ;
  \end{tikzpicture}
  \pgfmathsetmacro{\currentrest}{int(Mod(\myindex,\numberofxcards*\numberofycards))}
  \ifnumequal{\currentrest}{0}{\clearpage}{}
}
\end{document}
1
  • Thank you, I am so dumb. When I added that part I first thought about the necessety of calculating modulo the number of rows. Then I thought the critical part was getting TeX to the next page. After that I obviously forgot about the first thought.
    – Toscho
    Sep 26, 2017 at 22:34
4

You can also arrange your cards within a tcbraster or tcbitemize environment from tcolorbox package and let it do all the work for you. Just say, number of rows and columns and the package will compute cards size. You just have to list all your cards and the raster will distribute on pages.

An example:

\documentclass{article}
\usepackage[margin=1cm]{geometry}

\usepackage[most]{tcolorbox}

\begin{document}
\begin{tcbitemize}[%
    raster columns=2,
    raster rows=2,
    raster height=\textheight,
    enhanced,
    sharp corners,
    colframe=black,
    colback=gray!30,
    watermark text=\thetcbrasternum,
    raster equal skip=0pt,
    raster row skip=0pt,
    raster column skip=0pt
]
\tcbitem 
\tcbitem 
\tcbitem 
\tcbitem 
\tcbitem
\tcbitem
\tcbitem
\tcbitem
\tcbitem
\tcbitem
\tcbitem
\end{tcbitemize}
\end{document}

enter image description here

1
  • Thank you very much for that answer. That's the power of the LaTeX-Environment I so like: Somebody, somebody, somewhere has probably stumbled upon the same problem and solved it by writing a suitable package.
    – Toscho
    Sep 27, 2017 at 17:02

You must log in to answer this question.

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