\documentclass[12pt,a4paper]{article}

\usepackage{geometry}
\geometry{%
  a4paper,
  lmargin=1.7cm,
  rmargin=1.7cm,
  tmargin=4cm,
  bmargin=2cm,
  footskip=0pt,
  headheight=2cm}

\usepackage{amsmath}
\usepackage{amssymb}
\let\ge=\geqslant  \let\le=\leqslant
\usepackage{calc}
\usepackage{tikz}[2013/12/13 v3.0.0]
\usetikzlibrary{calc}
\tikzset{above/.default=6pt}
\tikzset{regular/.style={}}
\tikzset{hidden/.style={fill=yellow!80!white}}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage{lastpage}
\usepackage{ifthen}

% The baseline spacing is quite small for larger fonts
\makeatletter
\renewcommand\large{\@setfontsize\large\@xivpt{20}}
\renewcommand\Large{\@setfontsize\Large\@xviipt{24.5}}
\renewcommand\LARGE{\@setfontsize\LARGE\@xxpt{28}}
\renewcommand\huge{\@setfontsize\huge\@xxvpt{34}}
\makeatother

\newsavebox\templateimagebox
\newdimen\templateimagewd
\newif\iftesting  % say \testingtrue to have \card use simple
                  % parameters like {Q1} instead of {{regular}{Q1}}

\usepackage[export]{adjustbox}

\newcommand{\image}[1]{\includegraphics[max height=0.7\cardht, max width=0.7\cardwd]{#1}}
% There might be a nicer way to do the following, but I haven't yet
% found it.
\newcommand{\imagecap}[2]{%
  \savebox{\templateimagebox}{\includegraphics[max height=0.8\cardht, max width=0.8\cardwd]{#1}}%
  \setlength{\templateimagewd}{\wd\templateimagebox}%
  \hbox to \templateimagewd{%
    \begin{minipage}{\templateimagewd}%
      \usebox{\templateimagebox}\\
      \hbox to \templateimagewd{\hss #2\hss}%
    \end{minipage}}}

\newcommand{\makestyles}[3]{%
  \iftesting
    \def#1{regular}
    \def#2{#3}
  \else
    % split #3 into {style}{text}
    \makestylesa#1#2#3
  \fi
}
\newcommand{\makestylesa}[4]{%
  \def#1{#3}
  \def#2{#4}
}

% How much space should there be around the cards?
% The gap between cards is twice this amount
% This is zero by default, but can be modified by
% users of this header.
\newdimen\cardseph \setlength\cardseph{0pt}
\newdimen\cardsepv \setlength\cardsepv{0pt}
\newdimen\cardwd
\newdimen\cardht
\newdimen\cardtxtwd
\newdimen\offset
\setlength{\offset}{6mm}
\setlength{\cardwd}{3cm}  % defaults so table output works
\setlength{\cardht}{2cm}

% Card number #3 appears on row #1 column #2 with content #4 and label #5
% If card number is empty, don't draw circle around number
% Top left is (0,0) in our drawing
\newcommand{\card}[5]{%
  \coordinate (topl) at ($ (#2*\cardwd-\cardwd+\cardseph, \cardht-#1*\cardht-\cardsepv) $);
  \coordinate (botr) at ($ (#2*\cardwd-\cardseph, -#1*\cardht+\cardsepv) $);
  \coordinate (botl) at (topl |- botr);
  \coordinate (topr) at (topl -| botr);

  \def\templabel{#3}
  \ifx\templabel\empty
  \else
    \node[circle,draw,thin,inner sep=1pt]
      at ($ (topl) + (\offset,-\offset) $) {#3};
  \fi

  \def\templabel{#5}
  \ifx\templabel\empty
  \else \node at ($ (botl)!0.5!(botr) + (0,\offset) $) {\templabel};
  \fi
  \ifx\cardtitle\empty
  \else \node at ($ (topl)!0.5!(topr) + (0,-\offset) $) {\cardtitle};
  \fi

  \ifx\cardtitle\empty
    \ifx\templabel\empty
      \coordinate (cent) at ($ (topl)!0.5!(botr) $);
    \else
      \coordinate (cent) at ($ (topl)!0.5!(botr) + (0,0.5*\offset)$);
    \fi
  \else
    \ifx\templabel\empty
      \coordinate (cent) at ($ (topl)!0.5!(botr) + (0,-0.5*\offset)$);
    \else
      \coordinate (cent) at ($ (topl)!0.5!(botr) $);
    \fi
  \fi

  \makestyles\style\content{#4}

  \draw (topl) rectangle (botr);
  \node[text width=\cardtxtwd,align=flush center,\style] at (cent)
     {\content\par};
}

% This is identical to \card, except that it's intended for double-sided
% cards, so it doesn't draw a border.
\newcommand{\borderlesscard}[5]{%
  \coordinate (topl) at ($ (#2*\cardwd-\cardwd+\cardseph, \cardht-#1*\cardht-\cardsepv) $);
  \coordinate (botr) at ($ (#2*\cardwd-\cardseph, -#1*\cardht+\cardsepv) $);
  \coordinate (botl) at (topl |- botr);
  \coordinate (topr) at (topl -| botr);

  \def\templabel{#3}
  \ifx\templabel\empty
  \else
    \node[circle,draw,thin,inner sep=1pt]
      at ($ (topl) + (\offset,-\offset) $) {#3};
  \fi

  \def\templabel{#5}
  \ifx\templabel\empty
  \else \node at ($ (botl)!0.5!(botr) + (0,\offset) $) {\templabel};
  \fi
  \ifx\cardtitle\empty
  \else \node at ($ (topl)!0.5!(topr) + (0,-\offset) $) {\cardtitle};
  \fi

  \ifx\cardtitle\empty
    \ifx\templabel\empty
      \coordinate (cent) at ($ (topl)!0.5!(botr) $);
    \else
      \coordinate (cent) at ($ (topl)!0.5!(botr) + (0,0.5*\offset)$);
    \fi
  \else
    \ifx\templabel\empty
      \coordinate (cent) at ($ (topl)!0.5!(botr) + (0,-0.5*\offset)$);
    \else
      \coordinate (cent) at ($ (topl)!0.5!(botr) $);
    \fi
  \fi

  \makestyles\style\content{#4}

  \node[text width=\cardtxtwd,align=flush center,\style] at (cent)
     {\content\par};
}

% Domino number #3 appears on row #1 column #2 with left content #4,
% right content #5
% If card number is empty, don't draw circle around number
% Top left is (0,0) in our drawing
\newcommand{\domino}[5]{%
  \coordinate (topl) at ($ (#2*\cardwd-\cardwd+\cardseph, \cardht-#1*\cardht-\cardsepv) $);
  \coordinate (botr) at ($ (#2*\cardwd-\cardseph, -#1*\cardht+\cardsepv) $);
  \coordinate (botl) at (topl |- botr);
  \coordinate (topr) at (topl -| botr);
  \coordinate (topm) at ($ (topl)!0.5!(topr) $);
  \coordinate (botm) at ($ (botl)!0.5!(botr) $);

  \draw (topl) rectangle (botr);
  \draw [divider] (topm) -- (botm);

  \ifx\cardtitle\empty
  \else
    \node[fill=white] at ($ (topl)!0.5!(topr) + (0,-\offset) $) {\cardtitle};
  \fi
  \def\templabel{#3}
  \ifx\templabel\empty
  \else
    \node[circle,draw,thin,inner sep=1pt,fill=white]
      at ($ (topl)!0.5!(botr) $) {#3};
  \fi

  \ifx\cardtitle\empty
    \coordinate (centl) at ($ (topl)!0.5!(botm) $);
    \coordinate (centr) at ($ (topm)!0.5!(botr) $);
  \else
    \coordinate (centl) at ($ (topl)!0.5!(botm) + (0,-0.5*\offset)$);
    \coordinate (centr) at ($ (topm)!0.5!(botr) + (0,-0.5*\offset)$);
  \fi

  \makestyles\stylel\contentl{#4}
  \makestyles\styler\contentr{#5}

  \node[text width=\cardtxtwd,align=flush center,\stylel] at (centl)
     {\contentl\par};
  \node[text width=\cardtxtwd,align=flush center,\styler] at (centr)
     {\contentr\par};
}

\setlength{\parindent}{0pt}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
