21

Is there a LaTeX package that will allow me to draw domino tiles without having to draw each one separately in tikz?

25

I had this code for quite some time, maybe it can be used as inspiration for a better implementation:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\tikzset{
  dot hidden/.style={},
  line hidden/.style={},
  dot colour/.style={dot hidden/.append style={color=#1}},
  dot colour/.default=black,
  line colour/.style={line hidden/.append style={color=#1}},
  line colour/.default=black
}

\usepackage{xparse}

\NewDocumentCommand{\domino}{mm}{
\begin{tikzpicture}[x=2em,y=2em,radius=0.1]
\draw[rounded corners=0.5,line hidden] (0,0) rectangle (1,2);

\draw[line hidden] (0,1) -- (1,1);
\ifodd#1
  \fill[dot hidden] (0.5,1.5) circle;
\fi
\ifnum#1>1
   \fill[dot hidden] (0.2,1.2) circle;
   \fill[dot hidden] (0.8,1.8) circle; 
   \ifnum#1>3
      \fill[dot hidden] (0.8,1.2) circle;
      \fill[dot hidden] (0.2,1.8) circle;   
   \fi
   \ifnum#1>5
      \fill[dot hidden] (0.2,1.5) circle;
      \fill[dot hidden] (0.8,1.5) circle;  
   \fi
\fi
\ifodd#2
   \fill[dot hidden] (0.5,0.5) circle;
\fi
\ifnum#2>1
   \fill[dot hidden] (0.2,0.2) circle;
   \fill[dot hidden] (0.8,0.8) circle;
   \ifnum#2>3
      \fill[dot hidden] (0.8,0.2) circle;
      \fill[dot hidden] (0.2,0.8) circle;   
   \fi
   \ifnum#2>5
      \fill[dot hidden] (0.2,0.5) circle;
      \fill[dot hidden] (0.8,0.5) circle;  
   \fi
\fi
\end{tikzpicture}
}

\begin{document}

\domino{0}{0}
\domino{0}{1}
\domino{0}{2}
\domino{0}{3}
\domino{0}{4}
\domino{0}{5}
\domino{0}{6}

\domino{1}{0}
\domino{1}{1}
\domino{1}{2}
\domino{1}{3}
\domino{1}{4}
\domino{1}{5}
\domino{1}{6}

\domino{2}{0}
\domino{2}{1}
\domino{2}{2}
\domino{2}{3}
\domino{2}{4}
\domino{2}{5}
\domino{2}{6}

\domino{3}{0}
\domino{3}{1}
\domino{3}{2}
\domino{3}{3}
\domino{3}{4}
\domino{3}{5}
\domino{3}{6}

\domino{4}{0}
\domino{4}{1}
\domino{4}{2}
\domino{4}{3}
\domino{4}{4}
\domino{4}{5}
\domino{4}{6}

\domino{5}{0}
\domino{5}{1}
\domino{5}{2}
\domino{5}{3}
\domino{5}{4}
\domino{5}{5}
\domino{5}{6}

\domino{6}{0}
\domino{6}{1}
\domino{6}{2}
\domino{6}{3}
\domino{6}{4}
\domino{6}{5}
\domino{6}{6}

\end{document}

The output:

Quack

Hope it helps. :)

  • 2
    Your code for the dots is more elegant than the "brute force" one I used. – Gonzalo Medina Sep 12 '15 at 18:14
  • 2
    @Gonzalo: No way, I like yours better. :) – Paulo Cereda Sep 12 '15 at 18:20
24

Another option:

enter image description here

Simply use \Domino{<number>}{<number>} to draw the tile; adjust \DominoWd to have the desired size for the tiles.

The code:

\documentclass[varwidth=13.6cm,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,positioning,shadows}

\newlength\DominoWd
\setlength\DominoWd{1cm}
\newsavebox\mybox

\newcommand\PlaceDot[2]{
\fill[white] ([shift={(#1\DominoWd,#2\DominoWd)}]0,0) circle [radius=0.1\DominoWd];
}

\newcommand\domino[1]{%
\tikz[overlay]
{
\ifcase#1\relax
\or
\PlaceDot{0}{0}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0}{0}\PlaceDot{0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{0.3}\PlaceDot{-0.3}{-0.3}\PlaceDot{0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{0.3}\PlaceDot{0}{0}\PlaceDot{0.3}{-0.3}\PlaceDot{-0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{0.3}\PlaceDot{-0.3}{0}\PlaceDot{0.3}{0}\PlaceDot{0.3}{-0.3}\PlaceDot{-0.3}{-0.3}
\fi}%
}

\newcommand\Domino[3][]{%
\begin{tikzpicture}[#1]
\node[
  rectangle split,
  rectangle split parts=2,
  fill=black,
  text width=\DominoWd,
  align=center,
  text=white,
  drop shadow={black!20,opacity=1},
  rounded corners=0.13\DominoWd,
  #1
  ] 
  (dom)
  {%
    \rule[-0.5\DominoWd]{0pt}{\DominoWd}\domino{#2}%
    \nodepart{two}\rule[-0.5\DominoWd]{0pt}{\DominoWd}\domino{#3}%
  };
\draw[white!80!black]
  ([xshift=0.1\DominoWd]dom.west) -- ([xshift=-0.1\DominoWd]dom.east);
\end{tikzpicture}%
}

\begin{document}

\foreach \Valora in {0,...,6}
  {%
    \foreach \Valorb in {0,...,6}
      {\Domino{\Valora}{\Valorb}\quad}\par\medskip%
   }%   

\end{document}

Update

Using chains is easy to typeset an actual domino game:

enter image description here

The code:

\documentclass[varwidth=13.6cm,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,positioning,shadows,chains,scopes}

\newlength\DominoWd
\setlength\DominoWd{1cm}
\newsavebox\mybox

\newcommand\PlaceDot[2]{
\fill[white] ([shift={(#1\DominoWd,#2\DominoWd)}]0,0) circle [radius=0.08\DominoWd];
}

\newcommand\domino[1]{%
\tikz[overlay]
{
\ifcase#1\relax
\or
\PlaceDot{0}{0}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0}{0}\PlaceDot{0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{0.3}\PlaceDot{-0.3}{-0.3}\PlaceDot{0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{0.3}\PlaceDot{0}{0}\PlaceDot{0.3}{-0.3}\PlaceDot{-0.3}{-0.3}
\or
\PlaceDot{-0.3}{0.3}\PlaceDot{0.3}{0.3}\PlaceDot{-0.3}{0}\PlaceDot{0.3}{0}\PlaceDot{0.3}{-0.3}\PlaceDot{-0.3}{-0.3}
\fi}%
}

\newcounter{domtile}

\newcommand\Domino[3][]{%
\stepcounter{domtile}
\node[
  rectangle split,
  rectangle split parts=2,
  fill=black,
  text width=\DominoWd,
  align=center,
  text=white,
  outer sep=0pt,
  inner sep=0pt,
  %drop shadow={black!20,opacity=1},
  rounded corners=0.13\DominoWd,
  #1
  ] 
  (dom\thedomtile)
  {%
    \rule[-0.5\DominoWd]{0pt}{\DominoWd}\domino{#2}%
    \nodepart{two}\rule[-0.5\DominoWd]{0pt}{\DominoWd}\domino{#3}%
  };
\draw[white!80!black,shorten >= 0.11\DominoWd,shorten <= 0.11\DominoWd]
  (dom\thedomtile.west) -- (dom\thedomtile.east);
}

\newenvironment{dominogame}[1][]
  {\begin{tikzpicture}[#1]}
  {\end{tikzpicture}}

\begin{document}

\begin{dominogame}[start chain=1 going above,node distance=1pt,every node/.append style={on chain=1}]
\Domino[shift={(\DominoWd,0.5\DominoWd)},rotate=90]{6}{6}
\Domino[shift={(\DominoWd,0.5\DominoWd)}]{5}{6}
\Domino{2}{5}
\Domino{3}{2}
\Domino[shift={(0,2\DominoWd+1.5pt)},continue chain=1 going left,rotate=90]{1}{3}
\Domino[shift={(-2\DominoWd-1pt,\DominoWd)},rotate=90]{5}{1}
\Domino[shift={(-1.5\DominoWd-1pt,-2.5\DominoWd-1pt)},continue chain=1 going below,rotate=180]{0}{5}
\Domino{5}{0}
\Domino[shift={(-\DominoWd,-0.5\DominoWd)},rotate=90]{0}{0}
\Domino[shift={(-\DominoWd,-0.5\DominoWd)}]{0}{3}
\Domino{3}{6}
\Domino{6}{1}
\Domino[shift={(-\DominoWd,-0.5\DominoWd)},rotate=90]{1}{1}
{
[start chain=2 going below,every node/.append style={on chain=2}]
\Domino[shift={(0pt,-0.5\DominoWd)},at=(dom1)]{6}{4}
\Domino[shift={(0pt,-2\DominoWd-1.5pt)},continue chain=2 going right,rotate=90]{4}{0}
}
\end{dominogame}

\end{document}
  • 4
    How did you add the faint little gray dots between the dominoes that disappear when looked at? Can it be done with pstrick? – TonioElGringo Sep 16 '15 at 11:23
11

Simply using scaled stacks (no tikz) with the invocation \domino{<top>}{<bottom>}. The "unit" box is scaled to final size \domsz, currently set to 1cm. The relative size of dots in the square is controlled by altering \domwd (the unscaled square size of the "unit" box, into which \bullets are placed), and the offset of dots from the edge of the unscaled box is controlled by \domos. The border of the unscaled box is of width \fboxrule, which can be changed.

Note that \blt is a bullet altered so that there is uniform spacing on all sides of the glyph (to see this, try \fbox{\blt}).

\documentclass{article}
\usepackage{stackengine,scalerel}
\def\domsz{1cm}
\def\domwd{20pt}
\def\domos{1.8pt}
\def\blt{\stackon[.3pt]{$\bullet$}{}\kern-.3pt}
\def\domsq{\fboxsep=0pt\fbox{\rule{\domwd}{0pt}\rule{0pt}{\domwd}}}
\newcommand\domx[1]{\savestack\tmpbox{%
    \if0#1\domsq\else
    \if1#1\stackinset{c}{}{c}{}{\blt}{\domsq}\else
    \if2#1\stackinset{l}{\domos}{t}{\domos}{\blt}{%
      \stackinset{r}{\domos}{b}{\domos}{\blt}{\domsq}}\else
    \if3#1\stackinset{l}{\domos}{t}{\domos}{\blt}{%
      \stackinset{r}{\domos}{b}{\domos}{\blt}{%
      \stackinset{c}{}{c}{}{\blt}{\domsq}}}\else
    \if4#1\stackinset{l}{\domos}{t}{\domos}{\blt}{%
      \stackinset{r}{\domos}{b}{\domos}{\blt}{%
      \stackinset{l}{\domos}{b}{\domos}{\blt}{%
      \stackinset{r}{\domos}{t}{\domos}{\blt}{\domsq}}}}\else
    \if5#1\stackinset{l}{\domos}{t}{\domos}{\blt}{%
      \stackinset{r}{\domos}{b}{\domos}{\blt}{%
      \stackinset{l}{\domos}{b}{\domos}{\blt}{%
      \stackinset{r}{\domos}{t}{\domos}{\blt}{%
      \stackinset{c}{}{c}{}{\blt}{\domsq}}}}}\else
    \if6#1\stackinset{l}{\domos}{t}{\domos}{\blt}{%
      \stackinset{r}{\domos}{c}{}{\blt}{%
      \stackinset{l}{\domos}{c}{}{\blt}{%
      \stackinset{r}{\domos}{b}{\domos}{\blt}{%
      \stackinset{l}{\domos}{b}{\domos}{\blt}{%
      \stackinset{r}{\domos}{t}{\domos}{\blt}{\domsq}}}}}}\else
    \fi\fi\fi\fi\fi\fi\fi}\ignoremathstyle\scaleto{\tmpbox}{\domsz}%
}
\newcommand\domino[2]{\stackon[-\fboxrule]{\domx{#2}}{\domx{#1}}}
\begin{document}
\domino{1}{3} \domino{2}{4} \domino{0}{6} \domino{5}{5}\par
\domino{3}{4} \domino{4}{6} \domino{6}{2} \domino{5}{1}
\end{document}

enter image description here

So, for example, simply changing \fboxrule=1pt\def\domwd{17pt} will produce

enter image description here

5

Could be without any kind of graphics, tikz or any other package?

mwe

\documentclass[a5paper]{article}
\font\domino=domino
\catcode`\^=12 \offinterlineskip
\parskip.5em\parindent0pt
\begin{document}\tiny
Domino tiles:\par
{\domino<12} {\domino<34} {\domino5>0} \dots even  {\domino8>9}\par 
Starting the game:\par
{\domino<65<52<24<46<66}\par 
Watching your game:\par
{\domino ^1^24\\1v2v3}\par
Domino wall:\par
\obeylines\domino 
  <62<13^4\parskip0pt
  ^1^3<3>4v4
  v2v5<5>6<11
\end{document}

Alternatively, with packages graphicx and hhcount (Fancy Counters):

mwe

\documentclass[12pt,a5paper]{article}
\usepackage{hhcount} %http://tug.ctan.org/tex-archive/usergrps/uktug/baskervi/5_5/
\usepackage{graphicx} 
\def\dm#1#2{\mbox{\fcdice{#1}\rule{.05ex}{.75em}\fcdice{#2}}}
\def\md#1#2{\smash{\raisebox{-.35em}{\rotatebox{90}{\dm#1#2}}}}
\def\vd#1#2{\rotatebox{90}{\dm#1#2}}
\begin{document}
\Huge\parskip0pt\parindent0pt
\dm23 \dm35 \md55 \dm54\md44\dm40\\
\vd16 \dm13\md33 \dm34 \dm41\md11\vd10
\end{document}

Note: hhcount require hhutils0 available in the same web directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.