6

The black points in the picture form the letter A. It is created with using for loop only. \ifnum doesn't allow two or more conditions. So I draw the desire result using five nest for loop. It is an inefficient way to do so...Is there a better way to do so?

enter image description here

Here is the code:

 \documentclass{article}

 \usepackage{tikz}
 \usepackage{amssymb}
 \usepackage{xifthen}

 \begin{document}

 \begin{figure}

   \begin{tikzpicture}[scale=1]

      \foreach \x in {0,...,11}
         \foreach \y in {0,...,14}
           {
             \draw (\x,\y) circle(2pt); 
           }

     \foreach \x in {1,...,3}
         \foreach \y in {1,...,11}
            {
              \fill (\x,\y) circle(2pt);              
            }

     \foreach \x in {8,...,10}
        \foreach \y in {1,...,11}
           {
             \fill (\x,\y) circle(2pt);              
           }

     \foreach \x in {4,...,8}
        \foreach \y in {4,...,6}
          {
             \fill (\x,\y) circle(2pt);              
          }

     \foreach \x in {4,...,8}
       \foreach \y in {9,...,11}
            {
              \fill (\x,\y) circle(2pt);              
            }

   \end{tikzpicture}
   \end{figure}
  \end{document}
  • You can nest \ifnum ... \fi, though, to combine conditions. – cfr Jul 20 '17 at 12:51
13

You can punch through instead.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\clip[postaction=draw] foreach\x in{0,...,11}{\foreach\y in{0,...,14}{(\x,\y)circle(2pt)}};
\fill[even odd rule] (0.5,0.5) |- (10.5,11.5) |- (7.5,0.5) |- (3.5,3.5) |- (0.5,0.5) 
                     (3.5,6.5) rectangle (7.5,8.5) ;
\end{tikzpicture}
\end{document}

enter image description here

  • 4
    This is genius. – marsupilam Jul 20 '17 at 13:55
5

Something like this?

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \foreach \x in {0,...,11}
  {
    \foreach \y [evaluate=\y as \s using  {((\y==0)||(\y>11)) ? "" : ( ( (((\x>0)&&(\x<4))||(\x>7)&&(\x<11)) || ( ( (\x>3)&&(\x<9) ) && ( ( (\y>3)&&(\y<7) ) ||  (\y>8) ) ) ) ? "fill" : "" )  }  ] in {0,...,14}
    {
      \draw [\s] (\x,\y) circle(2pt);
    }
  }
\end{tikzpicture}
\end{document}

dotted 'A'

5

With luaLaTeX.

For using standalone class, I still need this

The output

enter image description here

The code

\RequirePackage{luatex85}
\documentclass[tikz,border=2pt]{standalone}
\usepackage{luacode}
\begin{luacode*}
  function a(i,j)
    middle = (j > 4 and j < 9) and (i>11 or i==7 or i==8)
    square = (i < 15) and (i > 3) and (j>1) and (j<12) 
    return square and not middle
  end

  function drawOrFill(i,j,letter)
    tex.print(letter(i,j) and [=[\fill[blue]]=] or [[\draw]])
  end
\end{luacode*}

\begin{document}
\begin{tikzpicture}
  \foreach \i in {1,...,15}
  {
    \foreach \j in {1,...,12}
    {
      \directlua{drawOrFill(\i,\j,a)} (\j,-\i) circle (4pt);
    }
  }
\end{tikzpicture}
\end{document}

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.