2

I wish to display images in specific arrangements, much like these Blokus pieces

enter image description here

For example, here are some apples arranged in shape number 6.

enter image description here

I can individually code each one, for example here is the code that achieves shape 6:

\begin{tikzpicture} 
\node[inner sep=0pt] at (2, 6) {\includegraphics[width=.15\textwidth]{apple}}; 
\node[inner sep=0pt] at (2, 4) {\includegraphics[width=.15\textwidth]{apple}}; 
\node[inner sep=0pt] at (0, 2) {\includegraphics[width=.15\textwidth]{apple}};
\node[inner sep=0pt] at (2, 2) {\includegraphics[width=.15\textwidth]{apple}}; \end{tikzpicture}

However I was hopeful that it might be possible to define a 4x4 grid, and then specify which positions I would like to place the apple in.

For example: The grid might be enter image description here

And then the apple diagram would be \GridImage{B, F, J, N, M}{apple}

Thanks so much, Wendy

1

2 Answers 2

6

Here's one way to do this. I've used a property list to store the coordinates of the grid, and two key value parameters to adjust the sizes: one to set the x and y value of the grid itself, and the other the scale for the image. If your images are uniform size, you can set these values initially using the \GridSetup command. The images themselves are inserted with the command \GridPlace. Rather than use the letters A-P to denote the grid positions, I've used a more readable a1-a4, b1-b4, etc. which makes visualizing positions much simpler.

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\ExplSyntaxOn
\keys_define:nn {grid} {
    grid.dim_set:N = \l_wendy_grid_dim,
    grid.initial:n = .75cm,
    scale.tl_set:N = \l_wendy_scale_tl,
    scale.initial:n = .15
}
\prop_new:N \l_wendy_grid_prop
\prop_set_from_keyval:Nn \l_wendy_grid_prop {
a1 = {(0,6)},
a2 = {(2,6)},
a3 = {(4,6)},
a4 = {(6,6)},
b1 = {(0,4)},
b2 = {(2,4)},
b3 = {(4,4)},
b4 = {(6,4)},
c1 = {(0,2)},
c2 = {(2,2)},
c3 = {(4,2)},
c4 = {(6,2)},
d1 = {(0,0)},
d2 = {(2,0)},
d3 = {(4,0)},
d4 = {(6,0)}
}
\NewDocumentCommand{\GridSetup}{m}{
    \keys_set:nn {grid}{#1}
}
\NewDocumentCommand{\GridPlace}{O{}mm}{
\group_begin:
\GridSetup{#1}
\begin{tikzpicture}[x=\l_wendy_grid_dim,y=\l_wendy_grid_dim]
    \clist_map_inline:nn {#2} {
        \prop_get:NnN \l_wendy_grid_prop {##1} \l_tmpa_tl
        {\node~at~\l_tmpa_tl~{\includegraphics[scale=\l_wendy_scale_tl]{#3}};}
        }
\end{tikzpicture}
\group_end:
}
\ExplSyntaxOff
\begin{document}
\GridPlace{b1,b2,b3,a2,c2}{cow}

\GridPlace[grid=.5cm,scale=.1]{a1,b1,c1,d1,d2}{cow}

\GridPlace[scale=.1]{a1,a2,a3,a4,b4}{cow}

\GridPlace[grid=.55cm,scale=.05]{a1,a2,b2,c2,d2}{cow}
\end{document}

output of code

1
  • This is perfect, thank you! Commented May 28 at 3:50
2

This is an adaption of Alan's answer which avoids the use of tikz and adds options to show the grid and set the grid size. The maximum grid size is 4 by 4, because the property list holding the translations for a1 etc. only defines 4 rows and 4 columns. However, this could obviously be extended if required.

By default, the grid is not shown but the full grid is set in terms of space. By restricting the number of columns and/or rows, this space is reduced. I've included show grid for the first of Alan's examples and a variant to demonstrate these options.

Note that the setup is a little different, possibly. I'm not sure if my rows and columns run the same way. Moreover, for Alan's setup, you needed to specify half of the grid dimension you wanted, whereas you should specify the actual value here. So, in Alan's examples, I've doubled the grid dimensions to compensate.

alternative cow arranger: output with and without cattle grids

\documentclass{article}
% ateb: https://tex.stackexchange.com/a/718972/ addaswyd o ateb Alan Munn: https://tex.stackexchange.com/a/718956/ addaswyd o gwestiwn Wendy Taylor: https://tex.stackexchange.com/q/718908/
\usepackage{graphicx}
\ExplSyntaxOn
\keys_define:nn { wendy } {
    grid.dim_set:N = \l_wendy_grid_dim,
    grid.initial:n = 1.5cm,
    scale.tl_set:N = \l_wendy_scale_tl,
    scale.initial:n = .15,
    rows .int_set:N = \l_wendy_rows_int,
    rows .initial:n = 4,
    cols .int_set:N = \l_wendy_cols_int,
    cols .initial:n = 4,
    show ~ grid .bool_set:N = \l_wendy_show_bool,
    show ~ grid .initial:n = false,
    show ~ grid .default:n = true,
}
\box_new:N \l_wendy_grid_box
\prop_new:N \l_wendy_grid_prop
\cs_new_protected_nopar:Npn \wendy_put:nn #1 #2 {
  \put #1 {#2}
}
\cs_generate_variant:Nn \wendy_put:nn { V }
\prop_set_from_keyval:Nn \l_wendy_grid_prop {
  a1 = {(0,0)},
  a2 = {(0,1)},
  a3 = {(0,2)},
  a4 = {(0,3)},
  b1 = {(1,0)},
  b2 = {(1,1)},
  b3 = {(1,2)},
  b4 = {(1,3)},
  c1 = {(2,0)},
  c2 = {(2,1)},
  c3 = {(2,2)},
  c4 = {(2,3)},
  d1 = {(3,0)},
  d2 = {(3,1)},
  d3 = {(3,2)},
  d4 = {(3,3)}
}

\NewDocumentCommand{\GridSetup}{m}{
  \keys_set:nn {wendy}{#1}
}
\NewDocumentCommand{\GridPlace}{O{}mm}{
  \group_begin:
  \GridSetup{#1}
  \hbox_set:Nn \l_wendy_grid_box { \includegraphics[scale=\l_wendy_scale_tl] { #3 } }
  \setlength \unitlength {\l_wendy_grid_dim}
  \begin{picture}(\l_wendy_cols_int,\l_wendy_rows_int)(0,0)
    \clist_map_inline:nn {#2} {
      \prop_get:NnN \l_wendy_grid_prop {##1} \l_tmpa_tl
      \wendy_put:Vn \l_tmpa_tl {\box_use:N \l_wendy_grid_box}
    }
    \bool_if:NT \l_wendy_show_bool
    {
      \int_set_eq:NN \l_tmpa_int \l_wendy_rows_int
      \int_set_eq:NN \l_tmpb_int \l_wendy_cols_int
      \int_incr:N \l_tmpa_int
      \int_incr:N \l_tmpb_int
      \multiput(0,0)(0,1){\l_tmpa_int} {\line(1,0){\l_wendy_cols_int}}
      \multiput(0,0)(1,0){\l_tmpb_int} {\line(0,1){\l_wendy_rows_int}}
    }
  \end{picture}
  \group_end:
}
\ExplSyntaxOff
\begin{document}
\GridPlace[show grid]{b1,b2,b3,a2,c2}{cow}
\GridPlace[rows=3,cols=3,show grid]{b1,b2,b3,a2,c2}{cow}

\GridPlace[grid=1cm,scale=.1,rows=4,cols=2]{a1,b1,c1,d1,d2}{cow}

\GridPlace[scale=.1,rows=2,cols=4]{a1,a2,a3,a4,b4}{cow}

\GridPlace[grid=1.1cm,scale=.05,rows=4,cols=2]{a1,a2,b2,c2,d2}{cow}
\end{document}
3
  • 1
    Thanks for your response, also very neat! Commented May 28 at 3:50
  • @WendyTaylor Believe it or not, this version is lighter weight. But if you're using tikz anyway, that's no advantage ;).
    – cfr
    Commented May 28 at 4:52
  • I am using a lot of tikz :) Commented May 31 at 3:37

You must log in to answer this question.

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