1

I'd like a box that looks exactly like this, width 5 units, height 7 units, each dot measuring 1 by 1 unit:

enter image description here

I've tried the following (including fiddling with the numbers):

\begin{tikzpicture}
\draw[thick,dotted] (0,0) rectangle (0.2, 0.3);
\end{tikzpicture}

But the above code produces something imperfect like:

enter image description here

2

enter image description here

\documentclass[tikz,border=3.14pt]{standalone}
\begin{document}
\begin{tikzpicture}[scale=0.05]
\foreach \X in {0,2,4}
{\fill (\X,0) rectangle ++(1,1); 
\fill (\X,6) rectangle ++(1,1); }
\foreach \X in {2,4}
{\fill (0,\X) rectangle ++(1,1); 
\fill (4,\X) rectangle ++(1,1); }
\end{tikzpicture}
\end{document}

UPDATE: \fill instead of \filldraw.

  • Awesome thanks. Is there an easy way I could scale up or down the size of this box without fiddling with each individual number? – dtcm840 Apr 11 '18 at 3:51
  • @dtcm840 Yes. Just use \begin{tikzpicture}[scale=2] instead of \begin{tikzpicture} in oder to double the size, or \begin{tikzpicture}[xscale=1.5] in order to stretch its horizontal dimension by 50%. – user121799 Apr 11 '18 at 3:56
  • When I try to scale it down (e.g. to \begin{tikzpicture}[scale=0.05]), now the spaces between the dots are smaller than each dot. Any suggestions on what to do? – dtcm840 Apr 11 '18 at 4:00
  • @dtcm840 You're right. My stupid mistake. This came from me using \filldraw instead of \fill. So the line width added to the size of the boxes. I fixed that. – user121799 Apr 11 '18 at 4:19
  • Cheers mate, works perfect now. Thanks for taking the time and effort to help me. – dtcm840 Apr 11 '18 at 4:30
2

An alternative solution with a dashed pattern. In this case the rectangle is drawn with four lines whit origins which compensate the width of lines.

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning}

\newcommand{\myunit}{1mm}

\begin{document}
\begin{tikzpicture}[
    mydash/.style={line width=\myunit, 
        dashed, 
        dash pattern=on \myunit off \myunit}]

\draw[mydash] (0,0) --++(5*\myunit,0);
\draw[mydash] (.5*\myunit,-.5*\myunit) --++(0,7*\myunit);
\draw[mydash] (0,6*\myunit) --++(5*\myunit,0);
\draw[mydash] (4.5*\myunit,-.5*\myunit) --++(0,7*\myunit);
\end{tikzpicture}
\end{document}

enter image description here

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.