How to draw the following special table in tikz?

I don't know How to draw the following special table in tikz? I know some elementary commands of tikz but I am confused! Any suggest are appreciate.

UPDATE: WME:

    \documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,0)--(0,10)--(10,10)--(10,0)--(0,0);
\draw (0,1)--(10,1);
\draw (0,2)--(10,2);
\draw (0,3)--(10,3);
\draw (0,4)--(10,4);
\draw (0,5)--(10,5);
\draw (0,6)--(10,6);
\draw (0,7)--(10,7);
\draw (0,8)--(10,8);
\draw (0,9)--(10,9);
%---------------------
\draw (1,0)--(1,10);
\draw (2,0)--(2,10);
\draw (3,0)--(3,10);
\draw (4,0)--(4,10);
\draw (5,0)--(5,10);
\draw (6,0)--(6,10);
\draw (7,0)--(7,10);
\draw (8,0)--(8,10);
\draw (9,0)--(9,10);
%------------------------

\draw[very thick](0,10)--(10,10)--(10,0)--(0,0)--(0,9)-- (9,9)--(9,1)--        (1,1)--(1,8)--(8,8)--(2,8)--(8,8)--(8,2)--(2,2)--(2,7)--(7,7)--(7,3)--(3,3)--(3,6)   --(6,6)--(6,4)--(4,4)--(4,5)   --(5,5);
\end{tikzpicture}
\end{document}


• Take a look at chapter 20, Matrices and Alignment in the pgf manual, ctan.org/pkg/pgf . In particular, it specifies access to the corners and edges of a tikz matrix – Huang_d Jun 10 '17 at 14:49
• Please add a minimal working example (MWE) that illustrates what you've tried to do so far and where you got stuck. – TH. Jun 10 '17 at 14:49
• Your question leaves all the effort to our community, even typing the essentials of a TeX document such as \documentclass{}...\begin{document} etc. As it is, most of our users will be very reluctant to touch your question, and you are left to the mercy of our procrastination team who are very few in number and very picky about selecting questions. You can improve your question by adding a minimal working example (MWE) that more users can copy/paste onto their systems to work on. If no hero takes the challenge we might have to close your question. – Dr. Manuel Kuehner Jun 10 '17 at 15:19

My solution uses the spiral code which I translated to lua with some modification.

\documentclass{article}

\usepackage{luacode}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=1]
\draw[step=1,very thin,xshift=0.5cm,yshift=0.5cm] (-5,-5) grid (5,5);

% position numbers
\begin{luacode}
function isPrime(n)
primes = {}
if n <= 0 then
return false
end
if n <= 2 then
return true
end
if n % 2 == 0 then
return false
end
for i = 3,n/2,2 do
if n % i == 0 then
return false
end
end
return true
end

NUM = 100
di = 1
dj = 0
i = 0
j = 0
segment_passed = 0
segment_length = 1
for k=0, (NUM-1) do
if isPrime(k+1) then
tex.sprint("\\draw ("..(i-0.5)..","..(j-0.5)..") -- ("..(i+0.5)..","..(j+0.5)..");")
tex.sprint("\\draw ("..(i-0.5)..","..(j+0.5)..") -- ("..(i+0.5)..","..(j-0.5)..");")
tex.sprint("\\draw node at ("..i..","..j..") {\\colorbox{white}{"..(k+1).."}};")
else
tex.sprint("\\draw node at ("..i..","..j..") {"..(k+1).."};")
end
i = i + di
j = j + dj
segment_passed = segment_passed + 1

if segment_passed == segment_length then
segment_passed = 0
tmp = di
di = -dj
dj = tmp
if dj == 0 then
segment_length = segment_length + 1
end
end
end
\end{luacode}

% draw spiral
\begin{luacode}
NUM = 120
di = -1
dj = 0
i = 0
j = 0
segment_passed = 0
segment_length = 1
for k=0, (NUM-1) do
tex.sprint("\\draw[ultra thick,line cap=round] ("..(i+0.5)..","..(j+0.5)..") -- ")
i = i + di
j = j + dj
tex.sprint("("..(i+0.5)..","..(j+0.5)..");")
segment_passed = segment_passed + 1

if segment_passed == segment_length then
segment_passed = 0
tmp = di
di = -dj
dj = tmp
if dj == 0 then
segment_length = segment_length + 1
end
end
end
\end{luacode}

\end{tikzpicture}

\end{document}


Update I figured out that the sketch may belong to the Ulam spiral. Thus, the diagonal lines in case of some prime numbers should indicate the diagonal lines within such a spiral.

Just for fun, I adapted my example code to generate the figure similar to the Wikipedia article. (I'll hope it is not too much off topic)

\documentclass{article}

\usepackage{luacode}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=0.05]
% position circles
\begin{luacode}
function isPrime(n)
primes = {}
if n <= 0 then
return false
end
if n <= 2 then
return true
end
if n % 2 == 0 then
return false
end
for i = 3,n/2,2 do
if n % i == 0 then
return false
end
end
return true
end

NUM = 40000
di = 1
dj = 0
i = 0
j = 0
segment_passed = 0
segment_length = 1
for k=0, (NUM-1) do
if isPrime(k+1) then
tex.sprint("\\filldraw ("..i..","..j..") circle (0.3);")
else
-- tex.sprint("\\draw node at ("..i..","..j..") {"..(k+1).."};")
end
i = i + di
j = j + dj
segment_passed = segment_passed + 1

if segment_passed == segment_length then
segment_passed = 0
tmp = di
di = -dj
dj = tmp
if dj == 0 then
segment_length = segment_length + 1
end
end
end
\end{luacode}

\end{tikzpicture}

\end{document}


• I just noticed that not all prime number are crossed out. What is the algorithm behind that? – hzhr Jun 10 '17 at 18:53
• +1 for the effort. Some boxes are struck out though. – CroCo Jun 10 '17 at 23:54
• hzhr, Thanks for your patient and good answer. – C.F.G Jun 11 '17 at 4:35

    \documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,10}
{
\draw(0,\x)--(10,\x);
\draw(\x,0)--(\x,10);
}
\draw[ultra thick,red] (0,10)--(10,10);
\foreach \x in {9,7,...,1}
{
\draw[ultra thick,red] (0.5*\x+5.5,0.5*\x+5.5)--(0.5*\x+5.5,4.5-0.5*\x)--(4.5-0.5*\x,4.5-0.5*\x)
-- (4.5-0.5*\x,4.5+0.5*\x) -- (4.5+0.5*\x,4.5+0.5*\x);
}
\end{tikzpicture}
\end{document}


Shame on me, I didn't recognize the prime numbers !

This is my first LuaLatex attempt. Bit of a weird language, isn't it ?

The lua

--- makeMatrix.lua
function zeroTable(n)
myTable={}
for i=1,n do
myTable[i]={}
for j=1,n do
myTable[i][j] = "0"
end
end
return myTable
end

myListCrossed = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
isCrossed ={}
for key,value in pairs(myListCrossed) do
isCrossed[value]=true
end

function nodeContent(a)
if isCrossed[a] then
option="|[crossed]| "
else
option=""
end
return option .. a
end

function table(n)
myTable=zeroTable(n)
square=math.floor(n^2)
back=false
vertical=false
dir={}
dir[1]=1
dir[2]=0
a=square-1
myTable[1][1]=square
i=1
j=1
repeat
repeat
i=i+dir[1]
j=j+dir[2]
myTable[i][j] = nodeContent(a)
a=a-1
--print("i: " .. i ..  "  j: " .. j )
until (j==1+i and j+i<n+1) or (j==i and j+i>n+1) or (i+j==n+1)
tmp=-dir[2]
dir[2]= dir[1]
dir[1]=tmp
until a<1
return myTable
end

function printTable(myTable,n,printFunction)
doubleBackslash="\\\\"
for i=1,n do
str=""
for j=1,n do
str= str .. myTable[j][i] .. " & "
end
str=string.gsub(str, "& \$", doubleBackslash)
printFunction(str)
end
end

n=10
myTable=table(n)

--printTable(myTable,n,print) -- debug prints to console
--printTable(myTable,n,tex.print) -- what we'll call the function as


The LaTeX

\RequirePackage{luatex85}
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\directlua{dofile("makeMatrix.lua")}
\begin{document}
\tikzset
{
crossed/.style=
{
fill=blue!30!black,
fill opacity=.4,
text opacity=1,
},
square matrix/.style=
{
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes=
{
draw=gray,
minimum height=#1,
anchor=center,
text width=#1,
align=center,
inner sep=0pt
},
},
square matrix/.default=1cm
}

\begin{tikzpicture}

\node (myMatrix)
[
square matrix,
]
{
\directlua{printTable(myTable,n,tex.print)}
};

\draw [very thick, red]
(myMatrix-1-1.north west)
-| ++(10,-10)
-| ++(-10,9)
foreach \x in {8,6,...,1}
{
-| ++(\x+1,-\x)
-| ++(-\x,\x-1)
}
-- ++(1,0) ;
\end{tikzpicture}
\end{document}

• You forgot 1..... – JPi Jun 10 '17 at 20:28
• Yeah. I wondered about 1, but ruled against including it, per the convention I was taught at school. The list is hardwired in myListCrossed in the Lua file if change is needed. – marsupilam Jun 10 '17 at 20:36

Another solution using Tikz is

\documentclass[border={10pt}]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Container/.style={rectangle,draw=black,thick, minimum size=1cm},
XcrossBox/.style={cross out, draw=black, minimum size=1cm,thick},
LeftStrikeBox/.style={strike out, rotate=90, draw=black, minimum size=1cm,thick},
RightStrikeBox/.style={strike out, draw=black, minimum size=1cm,thick},
box/.style={rectangle,draw=white,fill=white, minimum size=.3cm},
]
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\foreach \x in {0,1,...,10}{
\foreach \y in {0,1,...,10}
\node[Container] at (\x,\y){};
}
\node[XcrossBox] at (5,5) {};
\node[box] at (5,5){5};
\node[LeftStrikeBox] at (4,4) {};
\node[box] at (4,4){8};
\node[RightStrikeBox] at (2,2) {};
\node[box] at (2,2){6};
\end{tikzpicture}

\end{document}


The output is

Filling the rest is simple I presume.