36

Can you create images like this:

enter image description here

when you have something like

0 = green (#54ff00)
1 = white (#ffffff)
2 = red   (#ff0000)
3 = blue  (#0048ff)

Image (Python list of integers defined above):
[[2,0,0,0,0,0,0],
 [0,3,0,0,0,0,0],
 [0,3,2,1,1,0,0],
 [0,3,2,2,2,1,1],
 [0,3,2,0,0,1,0],
 [0,0,0,0,0,1,0],
 [0,0,0,0,0,1,0]]

with TikZ?

How far I got

The following MWE defines all colors, produces a grid of the correct size (though the size is determined by hand):

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{tikz}
\usepackage{xcolor}

\begin{document}
    \newcommand\n{7}
    \definecolor{green}{HTML}{54FF00}
    \definecolor{wite}{HTML}{FFFFFF}
    \definecolor{red}{HTML}{FF0000}
    \definecolor{blue}{HTML}{0048FF}
    \begin{tikzpicture}
        \foreach \x in {1,...,\n}{
            \foreach \y in {1,...,\n}{
                \begin{scope}[shift={(\x,\y)}]
                  \draw [fill=green] (0,0) rectangle (1,1);
                \end{scope}
            }
        }
    \end{tikzpicture}
\end{document}

Of course, I could simpy make a lot of adjusted

                \begin{scope}[shift={(\x,\y)}]
                  \draw [fill=green] (0,0) rectangle (1,1);
                \end{scope}

but I've wondered if this can (with reasonable effort) be done in TikZ / LaTeX.

My problem is giving TikZ the color array. I don't have any idea how to do this. I only know two types of looks in TikZ:

\foreach \x in {0,1,2,3,4,5}{...}
\foreach \number in {1,...,\n}{...}

I have never seen a nested over a 2D-array. All I have seen so far were 1D-Arrays of tuples (with fixed tuple size).

20

Here is a completely different approach from my other answer, and this one takes as its input, a form similar to that mentioned by the OP, namely:

\def\map{
[[2,0,0,0,0,0,0]
 [0,3,0,0,0,0,0]
 [0,3,2,1,1,0,0]
 [0,3,2,2,2,1,1]
 [0,3,2,0,0,1,0]
 [0,0,0,0,0,1,0]
 [0,0,0,0,0,1,0]]
}

In this case, this argument is passed to the macro \boxart. The box size is set with \setlength{\boxsize}{}.

EDIT: The OP has asked for a clarification on the code internals of \boxart. In this routine, I use some macros from the stringstrings package to strip out left brackets, turn commas into spaces, and turn right brackets into " . " strings. The result is a space-separated string (\thestring), that will look like

2 0 0 0 0 0 0 . 0 3 0     etc.

The \getargsC macro from readarray knows how to efficiently read space-delimited strings and is fed this string. The number of arguments in the string is stored in \narg and each argument is stored individually in \argi, \argii, \argiii, etc. Once that is done, a loop is set up (to go through \narg iterations, once for each item in \thestring), and each argument is checked. If it is a 0, a \gr is issued for a green block, and so on for 1, 2, and 3. If a . is found, a \par (paragraph) is issued. This while loop is performed inside a \parbox, so that the local line spacing can be set and the result doesn't have to start in the left column of the document (I should point out that the \parbox width was set arbitrarily by me, and may need to be manually tweaked by the user).

For the OP's edification, an \edef is an assignment where the contents of the argument are fully expanded before being assigned. Thus, what is found in \clr are the individual items from \thestring successively stored in the \arg... variables: \argi is 2, \argii is 0, ..., \argviii is ., etc.

\documentclass{article}
\usepackage{xcolor}
\usepackage{stringstrings}
\usepackage{readarray}
\newlength\boxsize
\setlength\boxsize{1ex}
\def\block#1{\fboxsep=0pt\fbox{\color{#1}\rule{\boxsize}{\boxsize}}\kern-\fboxrule}
\def\gr{\block{green}}
\def\rd{\block{red}}
\def\bl{\block{blue}}
\def\wh{\block{white}}
\newcounter{index}%
\newcommand\boxart[1]{%
  \setcounter{index}{0}%
  \convertchar[q]{#1}{,}{ }%
  \convertchar[q]{\thestring}{[}{}%
  \convertchar[q]{\thestring}{]}{ . }%
  \getargsC{\thestring}%
  \parbox[b]{8ex}{%
    \baselineskip\boxsize%
    \parindent 0ex%
    \parskip -.2\boxsize%
    \addtolength{\parskip}{-2\fboxrule}%
    \whiledo{%
      \theindex < \narg}{%
      \stepcounter{index}%
      \edef\clr{\csname arg\romannumeral\theindex\endcsname}%
      \expandafter\if\clr0\gr\fi%
      \expandafter\if\clr1\wh\fi%
      \expandafter\if\clr2\rd\fi%
      \expandafter\if\clr3\bl\fi%
     \expandafter\if\clr.\par\fi%
    }%
  }%
}
\begin{document}
%0 = green (#54ff00)
%1 = white (#ffffff)
%2 = red   (#ff0000)
%3 = blue  (#0048ff)
\def\map{
[[2,0,0,0,0,0,0]
 [0,3,0,0,0,0,0]
 [0,3,2,1,1,0,0]
 [0,3,2,2,2,1,1]
 [0,3,2,0,0,1,0]
 [0,0,0,0,0,1,0]
 [0,0,0,0,0,1,0]]
}
Here it is: \boxart{\map}

\def\map{
[[2,0,0,0,0]
 [0,3,0,0,0]
 [0,3,2,0,0]
 [0,3,2,1,1]
 [0,3,0,1,0]]
}
\setlength{\boxsize}{1.2ex}
Another: \boxart{\map}
\end{document} 

enter image description here

33

Here is a very simple solution using TikZ:

\documentclass[tikz]{standalone}
\def\pixels{
  {2,0,0,0,0,0,0},
  {0,3,0,0,0,0,0},
  {0,3,2,1,1,0,0},
  {0,3,2,2,2,1,1},
  {0,3,2,0,0,1,0},
  {0,0,0,0,0,1,0},
  {0,0,0,0,0,1,0}%
}
\definecolor{pixel 0}{HTML}{54FF00}
\definecolor{pixel 1}{HTML}{FFFFFF}
\definecolor{pixel 2}{HTML}{FF0000}
\definecolor{pixel 3}{HTML}{0048FF}
\begin{document}
\begin{tikzpicture}
  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \draw[fill=pixel \pix] (\x,-\y) rectangle +(1,1);
    }
  }
\end{tikzpicture}
\end{document}

enter image description here

  • +1 for simplicity (finally an answer I understand completely and that might help me to create other stuff :-) ) – Martin Thoma Feb 2 '14 at 17:30
  • The better solution ! bravo !! – Alain Matthes Feb 3 '14 at 9:34
  • Is there any way to set default \pix value (I mean, if it cannot value anything in the matrix, it will fill with, say, red)? – pushpen.paul Sep 8 '17 at 17:26
  • @pushpen.paul There is no matrix. It's just a list of lists. The \pix macro is just an item from a list. If one item is empty, the \pix macro is empty and the color pixel (with a space at the end) is used (it's like a default color). – Paul Gaborit Sep 8 '17 at 21:44
20

A matrix of nodes could be an option:

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

\begin{document}

\tikzset{ 
table/.style={
  matrix of nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,minimum size=1cm,align=center},
  nodes in empty cells
  }
}

\definecolor{0}{HTML}{54FF00}
\definecolor{1}{HTML}{FFFFFF}
\definecolor{2}{HTML}{FF0000}
\definecolor{3}{HTML}{0048FF}

\begin{tikzpicture}

\matrix (mat) [table]
{
|[fill=2]| & |[fill=0]|  & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| \\
|[fill=0]| & |[fill=3]|  & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| \\
|[fill=0]| & |[fill=3]|  & |[fill=2]| & |[fill=1]| & |[fill=1]| & |[fill=0]| & |[fill=0]| \\
|[fill=0]| & |[fill=3]|  & |[fill=2]| & |[fill=2]| & |[fill=2]| & |[fill=1]| & |[fill=1]| \\
|[fill=0]| & |[fill=3]|  & |[fill=2]| & |[fill=0]| & |[fill=0]| & |[fill=1]| & |[fill=0]| \\
|[fill=0]| & |[fill=0]|  & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| \\
|[fill=0]| & |[fill=0]|  & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| & |[fill=0]| \\
};
\end{tikzpicture}

\end{document}

Or, even shorter, selecting the dominant color as default:

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

\begin{document}

\definecolor{0}{HTML}{54FF00}
\definecolor{1}{HTML}{FFFFFF}
\definecolor{2}{HTML}{FF0000}
\definecolor{3}{HTML}{0048FF}

\tikzset{ 
table/.style={
  matrix of nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,fill=0,minimum size=1cm,align=center},
  nodes in empty cells
  }
}

\begin{tikzpicture}

\matrix (mat) [table]
{
|[fill=2]| &   &  &  &  &  &  \\
 & |[fill=3]|  &  &  &  &  &  \\
 & |[fill=3]|  & |[fill=2]| & |[fill=1]| & |[fill=1]| &  &  \\
 & |[fill=3]|  & |[fill=2]| & |[fill=2]| & |[fill=2]| & |[fill=1]| & |[fill=1]| \\
 & |[fill=3]|  & |[fill=2]| &  &  & |[fill=1]| &  \\
 &   &  &  &  &  &  \\
 &   &  &  &  &  &  \\
};
\end{tikzpicture}

\end{document}

enter image description here

And with a slight (though a little slow) help from pgfplotstable

\documentclass[border=3pt]{standalone}
\usepackage{pgfplotstable}

\pgfplotstableread[col sep=comma]{%
2,0,0,0,0,0,0
0,3,0,0,0,0,0
0,3,2,1,1,0,0
0,3,2,2,2,1,1
0,3,2,0,0,1,0
0,0,0,0,0,1,0
0,0,0,0,0,1,0
}\mycolortable
\newcommand\n{7}
\definecolor{c0}{HTML}{54FF00}
\definecolor{c1}{HTML}{FFFFFF}
\definecolor{c2}{HTML}{FF0000}
\definecolor{c3}{HTML}{0048FF}


\begin{document}
\begin{tikzpicture}
    \foreach \x[count=\xi from 0] in {1,...,\n}{
        \foreach \y[count=\yi from 0] in {1,...,\n}{
            \begin{scope}[shift={(\x,-\y)}]
            \pgfplotstablegetelem{\yi}{\xi}\of{\mycolortable}
            \draw[ultra thick,fill=c\pgfplotsretval] (0,0) rectangle (1,1);
            \end{scope}
        }
    }
\end{tikzpicture}
\end{document}

enter image description here

  • I've added a similar example, Gonzalo. I couldn't resist :) – percusse Feb 2 '14 at 0:09
  • 1
    @percusse by all means! Great addition. – Gonzalo Medina Feb 2 '14 at 0:16
  • for the pgfplotstable-part: Can you get the size of the table automatically? By the way, it does not have to be a square. – Martin Thoma Feb 3 '14 at 6:28
  • @moose All three options will allow arbitrary rectangular shapes: in the first two, simply fill the matrix of nodes with as many rows and columns as required; in the third one (pgfplotstable), define \m to control the rows and use \m and \n on the nested cycles. Regarding the question about the size of the table, I am not sure what you are asking. Could you please elaborate? – Gonzalo Medina Feb 3 '14 at 13:26
  • Ok, I try to make clear what I mean: I think it should not be necessary to define \n (or \m) excplicitly. These parameters are implicitly given by the information within pgfplotstableread (for example: 2,0,0,0,0,0,0\n2,0,0,0,0,0,0 is \m=2 and \n=7). – Martin Thoma Feb 3 '14 at 13:35
10

Make a stack (without tikz):

\documentclass{article}
\usepackage{xcolor}
\usepackage{stackengine}
\def\block#1{\kern-\fboxrule\fboxsep=0pt\fbox{\color{#1}\rule{1ex}{1ex}}}
\def\gr{\block{green}}
\def\rd{\block{red}}
\def\bl{\block{blue}}
\def\wh{\block{white}}
\setstackgap{S}{-\fboxrule}
\begin{document}
\Shortstack{
\rd\gr\gr\gr\gr\gr\gr\\
\gr\bl\gr\gr\gr\gr\gr\\
\gr\bl\rd\wh\wh\gr\gr\\
\gr\bl\rd\rd\rd\wh\wh\\
\gr\bl\rd\gr\gr\wh\gr\\
\gr\gr\gr\gr\gr\wh\gr\\
\gr\gr\gr\gr\gr\wh\gr
}
\end{document} 

enter image description here

8

I have found this question just by surfing TeX.SX, and I thought I'll post an answer not recreating the questioner's figure, but the Arecibo message that I have “typesetted” a while ago in TikZ and LuaLaTeX.

In the code I'm only iterating over a huge two-dimensional array which contains the “message”, and creating nodes according to the values at a given index.

\documentclass{article}

\usepackage[nohead,%
    nofoot,%
    nomarginpar,%
    paperwidth=210mm,%
    paperheight=297mm,%
    tmargin=5mm,%
    rmargin=5mm,%
    bmargin=5mm,%
    lmargin=5mm,
    vscale=1,%
    hscale=1]{geometry}

\usepackage[svgnames]{xcolor}

\usepackage{tikz}

\usepackage{luacode}

\newlength{\zeropt}
\setlength{\zeropt}{0pt}
\setlength{\parskip}{\zeropt}
\setlength{\parindent}{\zeropt}
\setlength{\baselineskip}{\zeropt}

\tikzset{%
    cell/.style={%
        minimum size=0.35cm%
    }%
}
\tikzset{%
    one/.style={%
        fill=White%
    },%
    two/.style={%
        fill=DarkOrchid!50!Fuchsia%
    },%
    three/.style={%
        fill=LimeGreen%
    },%
    four/.style={%
        fill=Blue!65!Cyan%
    },%
    five/.style={%
        fill=Crimson!50!Red%
    },%
    six/.style={%
        fill=Gold!50!Yellow%
    }%
}

\pagestyle{empty}

\begin{luacode*}
    arecibo_message = {{0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
        {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0},
        {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {3, 3, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 3, 3, 0, 0, 0, 0, 3, 3, 0, 0, 0},
        {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 3, 0, 0, 0, 0},
        {3, 3, 0, 3, 0, 0, 0, 3, 3, 0, 0, 0, 3, 3, 0, 0, 0, 0, 3, 3, 0, 3, 0},
        {3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
        {3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {3, 3, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 3, 3, 0, 0, 0},
        {3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0},
        {3, 3, 0, 3, 0, 0, 0, 0, 3, 3, 0, 0, 0, 3, 3, 3, 0, 0, 3, 3, 0, 3, 0},
        {3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
        {3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
        {0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0},
        {0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, 1, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, 1, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0},
        {0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
        {0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
        {0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0},
        {0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 4, 4, 4, 0, 5, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 0, 5, 0, 5, 5, 5, 0, 5, 0, 0, 1, 0, 1, 1, 0, 1, 1},
        {0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 5, 5, 0, 0, 5, 0, 0, 1, 1, 1, 1, 1, 1},
        {1, 0, 1, 1, 1, 0, 0, 0, 0, 5, 5, 5, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1},
        {0, 0, 4, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1},
        {0, 0, 4, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
        {0, 0, 4, 0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 6, 6, 6, 0, 6, 0, 6, 0, 0, 0, 6, 0, 6, 0, 6, 0, 6, 0, 6, 0, 6},
        {0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 6, 0, 6, 0, 6, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0},
        {0, 0, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 2, 0, 0, 0, 0},
        {0, 2, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 0},
        {0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 0},
        {0, 2, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0},
        {0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 4, 4, 4, 4, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 4, 4, 4, 4, 0, 0, 0}}

    function print_message(message)
        local cols = #message[1]
        local rows = #message

        tex.sprint([[\begin{tikzpicture}]])

        for j = 1, cols do
            for i = 1, rows do
                if message[i][j] == 1 then
                    tex.sprint([[\node[cell, one] at (]])
                elseif message[i][j] == 2 then
                    tex.sprint([[\node[cell, two] at (]])
                elseif message[i][j] == 3 then
                    tex.sprint([[\node[cell, three] at (]])
                elseif message[i][j] == 4 then
                    tex.sprint([[\node[cell, four] at (]])
                elseif message[i][j] == 5 then
                    tex.sprint([[\node[cell, five] at (]])
                elseif message[i][j] == 6 then
                    tex.sprint([[\node[cell, six] at (]])
                end

                if message[i][j] ~= 0 then
                    tex.sprint(0.35 * (j - 1))
                    tex.sprint([[cm,]])
                    tex.sprint(0.35 * (-i + 1))
                    tex.sprint([[cm){};]])
                end
            end
        end

        tex.sprint([[\end{tikzpicture}]])

    end
\end{luacode*}

\begin{document}
\pagecolor{Black}
\vspace*{\fill}
\begin{center}
\luadirect{print_message(arecibo_message)}
\end{center}
\vspace*{\fill}
\end{document}
6

A solution with PSTricks, just for fun!

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pgfmath} % it must be explicitly loaded!

\def\pixels{
  {2,0,0,0,0,0,0},
  {0,3,0,0,0,0,0},
  {0,3,2,1,1,0,0},
  {0,3,2,2,2,1,1},
  {0,3,2,0,0,1,0},
  {0,0,0,0,0,1,0},
  {0,0,0,0,0,1,0}%
}
\definecolor{pixel 0}{HTML}{54FF00}
\definecolor{pixel 1}{HTML}{FFFFFF}
\definecolor{pixel 2}{HTML}{FF0000}
\definecolor{pixel 3}{HTML}{0048FF}

\begin{document}
\begin{pspicture}[fillstyle=solid,dimen=monkey](7,-7)
    \foreach \line [count=\y from 0] in \pixels 
    {
        \foreach \pix [count=\x from 0] in \line 
        {
            \rput(\x,-\y){\psframe[fillcolor=pixel \pix](1,-1)}
        }
    }
\end{pspicture}
\end{document}

enter image description here

  • 3
    I really got surprised to know that color names can contain spaces. – kiss my armpit Feb 2 '14 at 10:08
  • dimen key provides 3 options with primate names: indri for inner, monkey for middle, and orangutan for outer. – kiss my armpit Feb 2 '14 at 10:18
3

If you don't care about the black lines, you can use a simple \rule to mimic a pixel and then use the picture environment. I guess it's not exactly what you had in mind but just in case, here you go:

 \documentclass{minimal}

 %Pixel macro and its dimension
 \newcommand{\dimension}{10pt}
 \newcommand{\pixel}[2]{\textcolor{#1}{\rule{#2}{#2}}}


 %Colors
 \usepackage{xcolor}
 \definecolor{couleura}{RGB}{255, 0, 0}
 \definecolor{couleurb}{RGB}{84, 255, 0}
 \definecolor{couleurc}{RGB}{0, 72, 255}
 \definecolor{couleurd}{RGB}{255, 255, 255}

 %Colored pixels
 \newcommand{\pixela}{\pixel{couleura}{\dimension}}
 \newcommand{\pixelb}{\pixel{couleurb}{\dimension}}
 \newcommand{\pixelc}{\pixel{couleurc}{\dimension}}
 \newcommand{\pixeld}{\pixel{couleurd}{\dimension}}

 \begin{document}

 \setlength{\unitlength}{\dimension}%
 \begin{picture}(7,7)%
     \put (0,7) {\pixela}%
     \put (1,7) {\pixelb}%
     \put (2,7) {\pixelb}%
     \put (3,7) {\pixelb}%
     \put (4,7) {\pixelb}%
     \put (5,7) {\pixelb}%
     \put (6,7) {\pixelb}%
     \put (0,6) {\pixelb}%
     \put (1,6) {\pixelc}%
     \put (2,6) {\pixelb}%
     \put (3,6) {\pixelb}%
     \put (4,6) {\pixelb}%
     \put (5,6) {\pixelb}%
     \put (6,6) {\pixelb}%
     \put (0,5) {\pixelb}%
     \put (1,5) {\pixelc}%
     \put (2,5) {\pixela}%
     \put (3,5) {\pixeld}%
     \put (4,5) {\pixeld}%
     \put (5,5) {\pixelb}%
     \put (6,5) {\pixelb}%
     \put (0,4) {\pixelb}%
     \put (1,4) {\pixelc}%
     \put (2,4) {\pixela}%
     \put (3,4) {\pixela}%
     \put (4,4) {\pixela}%
     \put (5,4) {\pixeld}%
     \put (6,4) {\pixeld}%
     \put (0,3) {\pixelb}%
     \put (1,3) {\pixelc}%
     \put (2,3) {\pixela}%
     \put (3,3) {\pixelb}%
     \put (4,3) {\pixelb}%
     \put (5,3) {\pixeld}%
     \put (6,3) {\pixelb}%
     \put (0,2) {\pixelb}%
     \put (1,2) {\pixelb}%
     \put (2,2) {\pixelb}%
     \put (3,2) {\pixelb}%
     \put (4,2) {\pixelb}%
     \put (5,2) {\pixelb}%
     \put (6,2) {\pixelb}%
     \put (0,1) {\pixelb}%
     \put (1,1) {\pixelb}%
     \put (2,1) {\pixelb}%
     \put (3,1) {\pixelb}%
     \put (4,1) {\pixelb}%
     \put (5,1) {\pixelb}%
     \put (6,1) {\pixelb}%
  \end{picture}

 \end{document}

The result:

Big drawback : the code is very long ! But on the other hand, it's pretty fast. And, if you're lazy, I wrote a python script a little while ago to generate the tex code from a png file (the png needs to have an alpha layer). You can check it out here:

http://alexisfles.ch/en/latex/pixelart.html

1
\begin{tikzpicture}
\definecolor{green}{HTML}{54FF00}
\definecolor{white}{HTML}{FFFFFF}
\definecolor{red}{HTML}{FF0000}
\definecolor{blue}{HTML}{0048FF}

\fill[green](0,0)rectangle(7,7);

\fill[red](0,6)rectangle ++(1,1);
\fill[red](2,2)rectangle ++(1,3);
\fill[red](3,3)rectangle ++(2,1);

\fill[blue](1,2)rectangle ++(1,4);

\fill[white](3,4)rectangle ++(2,1);
\fill[white](6,3)rectangle ++(1,1);
\fill[white](5,0)rectangle ++(1,4);

\draw[step =1cm , black, line width=3] (0,0)grid(7,7);
\end{tikzpicture}
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Martin Schröder Nov 24 '15 at 6:25
  • 3
    It would be helpful if you posted not only a code snippet (though a relevant one, of course), but made it immediately compilable by providing instructions such as \documentclass{<whatever>}, \usepackage{any needed packages>}, \begin{document}, and \end{document}. Providing a couple of sentences to explain how your solution works and how it differs from others would also make your posting more helpful. Please consider editing your positing in this manner. (Have a look at the other answers to this posting for some examples.) – Mico Nov 24 '15 at 6:26

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.