Skip to content

Instantly share code, notes, and snippets.

@mlhetland
Created March 4, 2022 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlhetland/e7c219a498897aa31f31bd3dd0052ef1 to your computer and use it in GitHub Desktop.
Save mlhetland/e7c219a498897aa31f31bd3dd0052ef1 to your computer and use it in GitHub Desktop.
Simple LaTeX code for drawing playing-card like rectangles with numbers
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{tikzcards}
% Copyright 2019 Magnus Lie Hetland
\RequirePackage{tikz}
\usetikzlibrary{math}
\RequirePackage{options}
\RequirePackage{etoolbox}
\options{
/tikzcards/.new family,
/tikzcards/.cd,
width/.new length = 2.5in,
height/.new length = 3.5in,
corner radius/.new length = .125in,
margin/.new length = .1875in,
scale/.new value = 1,
angle/.new value = 0,
value/.new value,
flip/.new toggle,
back color/.new value = black!25,
text color/.new value = black,
jitter/.new toggle,
inner margin/.new length = .1875in,
draw slot/.new toggle,
draw card/.new toggle = true,
dim/.new style = [black!20]{text color = #1},
seed/.new cmd = \pgfmathsetseed{#1} % Note: Just sets the global seed
}
\newcommand{\cardset}[1]{\options{/tikzcards, #1}}
\newcommand{\tikzcard}[1][]{{
\cardset{#1}
\letoption{/tikzcards/width}\width
\letoption{/tikzcards/height}\height
\letoption{/tikzcards/margin}\margin
\letoption{/tikzcards/inner margin}\innermargin
\letoption{/tikzcards/angle}\angle
\letoption{/tikzcards/scale}\scale
\iftoggle{/tikzcards/jitter} {
\tikzmath {
\angle = \angle + 5*rand;
\xoff = 5*rand;
\yoff = 5*rand;
}
}{
\tikzmath {
\xoff = 0;
\yoff = 0;
}
}
\tikzmath{
\bigtextscale = \scale * 18;
\smalltextscale = \scale * 3.5;
}
\begin{scope}[scale=\scale]
\iftoggle{/tikzcards/draw slot}{
\foreach \xsign in {-1,1} {
\foreach \ysign in {-1,1} {
\draw (\xsign*.5\width + \xsign*.5\margin,
\ysign*.5\height + \ysign*.5\margin -\ysign*2\margin)
-- ++(0,\ysign*2\margin) -- ++(-\xsign*2\margin,0)
;
}
}
\draw (0,0) node
{\scalebox{\bigtextscale}{{\color{black!20}\option{/tikzcards/value}}}}
;
}{}
% For the bounding box -- the card itself is drawn as an overlay:
\path (-.5\width,-.5\height) rectangle +(\width, \height);
% Jitter the card (but not the slot):
\begin{scope}[overlay]
\begin{scope}[xshift = \xoff pt, yshift = \yoff pt]
\begin{scope}[rotate=\angle]
\iftoggle{/tikzcards/draw card}{
\draw[very thin,
fill=white,
rounded corners=\scale * \option{/tikzcards/corner radius}]
(-.5\width,-.5\height) rectangle +(\width, \height);
\iftoggle{/tikzcards/flip}{
\fill[\option{/tikzcards/back color}]
(-.5\width + \margin, -.5\height + \margin)
rectangle (.5\width - \margin, .5\height - \margin)
;
\draw[white, line width = \scale*6pt]
(-.5\width + \margin + \innermargin + .5\pgflinewidth,
-.5\height + \margin + \innermargin + .5\pgflinewidth)
rectangle
(.5\width - \margin - \innermargin - .5\pgflinewidth,
.5\height - \margin - \innermargin - .5\pgflinewidth)
;
}{
\draw (0,0) node[rotate=\angle,\option{/tikzcards/text color}]
{\scalebox{\bigtextscale}{\option{/tikzcards/value}}}
;
\draw (-.5\width, .5\height) +(+\margin, -\margin)
node[inner sep=0, anchor=north west, rotate=\angle,
\option{/tikzcards/text color}]
{\scalebox{\smalltextscale}{\option{/tikzcards/value}}}
;
\draw (.5\width, -.5\height) +(-\margin, +\margin)
node[inner sep=0, anchor=north west, rotate={\angle+180},
\option{/tikzcards/text color}]
{\scalebox{\smalltextscale}{\option{/tikzcards/value}}}
;
}
}{}
\end{scope}
\end{scope}
\end{scope}
\end{scope}
}}
\newcommand{\card}[1]{\tikz\tikzcard[#1];}
\endinput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment