TikZ 可以创建像素艺术图像吗?

TikZ 可以创建像素艺术图像吗?

你能创建如下图像吗:

在此处输入图片描述

当你有类似的东西

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]]

使用 TikZ 吗?

我走了多远

以下 MWE 定义所有颜色,产生正确大小的网格(尽管大小是手工确定的):

\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}

当然,我可以简单地做很多调整

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

但我想知道这是否可以(通过合理的努力)在 TikZ / LaTeX 中完成。

我的问题是给 TikZ 颜色数组。我不知道该怎么做。我只知道 TikZ 中的两种外观:

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

我从未见过嵌套在二维数组上的数组。到目前为止,我所见过的都是一维元组数组(元组大小固定)。

答案1

这是一个与我的其他答案完全不同的方法,并且该方法采用与 OP 提到的类似的形式作为输入,即:

\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]]
}

在这种情况下,此参数传递给宏\boxart。框大小用设置\setlength{\boxsize}{}

编辑:原帖作者要求澄清 的代码内部结构\boxart。在此例程中,我使用stringstrings包中的一些宏来删除左括号,将逗号转换为空格,并将右括号转换为“ 。”字符串。结果是一个空格分隔的字符串 ( \thestring),看起来像

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

\getargsC中的宏知道readarray如何有效地读取以空格分隔的字符串,并将该字符串作为输入。字符串中的参数数量存储在中\narg,每个参数单独存储在\argi、、\argii\argiii中。完成后,设置一个循环(进行\narg迭代,对 中的每个项目进行一次迭代\thestring),并检查每个参数。如果是0,则为绿色块发出 ,对于、和 ,\gr依此类推。如果找到 ,则发出 (段落)。这个 while 循环在 内部执行,这样可以设置本地行距,并且结果不必从文档的左列开始(我应该指出,宽度是我任意设置的,可能需要用户手动调整)。123.\par\parbox\parbox

对于 OP 的启发,\edef是一个作业,其中论证的内容是完全扩展在赋值之前。因此,在 中找到的是依次存储在变量中\clr的单个项:是、是、...、是等。\thestring\arg...\argi2\argii0\argviii.

\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} 

在此处输入图片描述

答案2

这是一个使用 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}

在此处输入图片描述

答案3

可能matrix of nodes有一个选项:

\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}

或者,更简短地说,选择主色作为默认颜色:

\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}

在此处输入图片描述

并在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}

在此处输入图片描述

答案4

我刚刚在 TeX.SX 上发现了这个问题,我想我会发布一个答案,而不是重新创建提问者的形象,但是阿雷西博消息我之前已经在 TikZ 和 LuaLaTeX 中对其进行了“排版”。

在代码中,我仅遍历包含“消息”的巨大二维数组,并根据给定索引的值创建节点。

\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}

相关内容