2

I am trying to rearrange the value inside grid. For example, the below code

 \documentclass[tikz,border=7mm]{standalone} 

\begin{document}

\tikz\draw grid(4,4)foreach[count=~]\l in{1,...,16}  
{({.5+mod(~-1,4},{3.5-div(~-1,4})node{\l}}; 

\end{document}

gives a 4*4 grid where each box has a number from 1 to 16. like-

1  2  3  4
5  6  7  8
9  10 11 12
13 14 15 16

How can I perform the same thing using two for loop such that the grid becomes-

1  2  2   1
3  4  4   3
3  4  4   3
1  2  2   1

For 8 rows and 8 column, each quadrant should start with 1 and end with 16.

    1  2  3  4     4  3  2  1  
    5  6  7  8     8  7  6  5  
    9  10 11 12    12 11 10 9
    13 14 15 16    16 15 14 13

    13 14 15 16    16 15 14 13  
    9  10 11 12    12 11 10 9
    5  6  7   8    8  7  6  5
    1  2  3   4    4  3  2  1

Also , How can I print inside grid such as-

a=1  a=2  a=2   a=1
a=3  a=4  a=4   a=3
a=3  a=4  a=4   a=3
a=1  a=2  a=2   a=1

I am trying to construct the general code that is valid for x rows and y columns instead of 4 rows and 4 columns.

I found the above code from here.

6
  • 4
    Welcome to TeX - LaTeX! Rather than posting a code snippet, it is better to post a compilable minimal document. Also, please show us what you have tried to get a double loop. Apr 9, 2017 at 16:54
  • @AndrewSwann if u want the generic code for the value rearrangement, I can give it to you. I have searched and am still searching on SE regarding post, but found no help so far!
    – Michael
    Apr 9, 2017 at 16:57
  • The easy solution is to create a tikz array \def\myarray{{1,2,2,1,3,4,4,3,3,4,4,3,1,2,2,1}}. See page 927 of PGF manual. Apr 9, 2017 at 20:40
  • If you have the algorithm that generates those numbers (based on i and j indices, I suppose?), please do add that to the question. Apr 9, 2017 at 20:41
  • 2
    Andrew means that you should post a complete and compileable code example that people can run without additional changes. Apr 9, 2017 at 20:43

3 Answers 3

2

Here is how you get a double loop, you add a second foreach inside the expression {...} that the first foreach loops over.

Sample output

\documentclass{article}

\usepackage{tikz}

\begin{document}

\tikz\draw grid(4,4) foreach \x  in {1,...,4}
  { foreach[evaluate={\z = int((\x*(5-\x)/2+\y*(5-\y))-5)}] \y in {1,...,4}
  {({.5+(\x-1)},{4.5-\y}) node{$a = \z$}}};

\end{document}

To change the values printed, just adjust the formula for \z in the evaluate statement. Note that int is used here, so that a decimal answer 1.0 etc. is not display.

Here is a version for your new pattern specification with variables \n and \m (even numbers) giving the total number of columns and rows, and a variable \s storing a dimension to scale the box size so the contents fit:

Sample output

\documentclass{article}

\usepackage{tikz}

\begin{document}

\def\n{6}
\def\m{8}
\def\s{1.5cm}
\tikz\draw grid[step=\s](\n*\s,\m*\s) foreach[evaluate] \x  in {1,...,\n}
  { foreach[evaluate={\z = int(min(\x,\n+1-\x)+\n*min(\y-1,\m-\y)/2)}] \y in {1,...,\m}
  {({\s*(.5+(\x-1))},{\s*(\m+.5-\y)}) node{$a=\z$}}};

\end{document}
1
1
\documentclass[tikz,border=7mm]{standalone} 
\begin{document}
\tikz\draw grid(4,4)foreach
    [evaluate={\x=.5+mod(\l-1,4);\y=3.5-div(\l-1,4);\z=int(5-(\x-2)^2/2-(\y-2)^2)}]
    \l in{1,...,16}{(\x,\y)node{$a=\z$}};
\end{document}

5
  • Is it in the general form ? can I use it for 10 rows and 25 columns? of-course I have change value for $l$ , but what else need to be changed? would u plz provide the general form?
    – Michael
    Apr 9, 2017 at 23:46
  • @MikeSQ If you don't tell what's the rule for filling in the array, nobody can help to extend it.
    – egreg
    Apr 10, 2017 at 8:06
  • the rule is same , so what u 've done for 4 rows and 4 columns, will be done for 10 rows and 25 or 26 columns. To be precise, u generalize the same procedure for x rows and y columns instead of 4 rows and 4 columns.
    – Michael
    Apr 10, 2017 at 11:37
  • @MikeSQ You give a single sample and say "generalize from it". Even in the intelligence tests they give you more than one example, for the simple reason that otherwise the task is highly ambiguous. In your application, will a longer first line read 1 2 3 4 ... 4 3 2 1 or 1 2 2 ... 2 1? How are the lines further down constructed? The only rule that seems to be apparent is that there is a left-right and top-bottom symmetry.
    – gernot
    Apr 10, 2017 at 12:02
  • @gernot each quadrant should be symmetric, plz see the post, I have added an example of 8 rows and 8 columns.
    – Michael
    Apr 10, 2017 at 12:21
1

Like this? (Now that I've corrected the symmetry...)

enter image description here

I produced this with the following Metapost code, wrapped up in luamplib. Compile with lualatex.

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    numeric u, v, N; N = 2; 
    u = 14mm; v = 9mm;
    for i=0 upto 3:
        for j=0 upto N-1:
            for k=0 upto N-1:
                label("$a=" & decimal (N**2-(k+N*j)) & "$", 
                      (k+2/3,j+2/3) xscaled u yscaled v
                      if (i mod 3) > 0: reflectedabout(up,down) fi
                      if i > 1: reflectedabout(left,right) fi
                );
            endfor
        endfor
    endfor
endfig;
\end{mplibcode}
\end{document}

If you set N=4 in the above you get:

enter image description here

and so on...

enter image description here

The values are generated by the label in the middle of the three loops. Specifically decimal (N**2-(k+N*j)) generates a string with the value of N^2-(k+N*j) -- if you want different numbers you can adjust this formula. So to get numbers starting at a=1 in the middle, you could change the formula to decimal (1+k+N*j).

If you wanted the numbers is some other order, then you could (for example) swap the values of j and k in the coordinates to get a column-first sequence, or subtract them from N to get the numbers in reverse order.

5
  • +1 , but would u plz address the problem when we want start with a=1 instead of a=16 ?
    – Michael
    Apr 10, 2017 at 13:01
  • Where is the "start" ? in the middle or at the outer corner?
    – Thruston
    Apr 10, 2017 at 13:02
  • ur top-left,bottom-left, top-right,bottom-right corner started with a=16 for N=4, what if I want to start top-left,bottom-left, top-right,bottom-right corner with a=1 for N=4?
    – Michael
    Apr 10, 2017 at 13:06
  • See updated answer.
    – Thruston
    Apr 10, 2017 at 13:07
  • ! LaTeX Error: File `luatex85.sty' not found. ...errrrr!! latex is really out of reach for beginner!!!! Now I have to know which file is required and where to keep them!!! By the way, where is the grid? spacing between each quadrant is not required but grid is.
    – Michael
    Apr 10, 2017 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .