I would like to position "grids" on the page. I've placed them in a scope, named the local bounding box, and attempted to use that for relative positioning, e.g. shift={($(myscope.center)+(1cm,1cm)$)}. I've also attempted to position on the page with, e.g. ($(page.south west)$), but am not very pleased with the result.
To be specific, in the screenshot below, I'd like to be able to align the left side of the three grids relative to the left margin: currently the second grid is well aligned, but the first and third are a little too far from the left margin. I could go on attempting to tweak the shifting, but I have a feeling there's a better approach than this.
To provide context for my question, the grids are part of a "multiple-choice" sheet. I adapted some expl3 code found in the accepted answer here. The \vgrid and \hgrid commands produce vertical/horizontal grids: the integers are the question number; the letters are the answer choices; the starting number for the question can be changed; the correct answer is shown in blue (0 leaves the question unanswered). See details immediately below:
% ARGUMENTS of \vgrid and \hgrid
% [starting question number] [total number of questions] [total number of answer choices] [scaling factor] {list of zeros or list of the correct answer numbers, separated by semi-colons}
% #1 : question sequence starting number, default 1
% #2 : total number of questions in sequence, default 20
% #3 : total number of choices for each question, default 5
% #4 : scaling factor, default 1.0
% #5 : semi-colon separated list of numbers corresponding to correct answers, e.g. 1 for A, 2 for B, and 0 for not-answered
\documentclass[12pt]{article}
\usepackage[papersize={8.5in,11in},left=0.5in,right=0.5in,nohead,top=0.5in,nofoot,bottom=0.5in,marginparsep=0pt,showframe]{geometry}
\setlength{\parindent}{0pt}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
% HORIZONTAL GRID
\ExplSyntaxOn
\NewDocumentCommand{\hgrid}{ O{1} O{20} O{5} O{0.9} m}{%
\seq_set_split:Nnn \l_tmpa_seq{;}{#5}
% print question key (A, B, C...)
\int_step_inline:nnnn {1} {1} {#3} {%
\node at (0#4, #3#4-##1#4) {\int_to_Alph:n{##1}};}
% print question number (1, 2, 3...)
\int_step_inline:nnnn {#1} {1} {#1+#2-1} {%
\node at (##1#4-#1#4+1#4, #3#4) {##1};
\int_step_inline:nnnn {1} {1} {#3} {%
% draw an empty box for each question number/key
\node[draw,line~width=1#4pt,minimum~width=0.8#4cm,minimum~height=0.8#4cm] at (##1#4-#1#4+1#4, #3#4-####1#4) {};
% fill the correct box (0 to leave it empty)
\int_compare:nNnTF {####1} = {\seq_item:Nn \l_tmpa_seq {#1+#2-##1}} {\node[fill=blue,minimum~width=0.8#4cm,minimum~height=0.8#4cm] at (#1#4+#2#4-##1#4, #3#4-####1#4) {};}{}
}
}
}
\ExplSyntaxOff
% VERTICAL GRID
\ExplSyntaxOn
\NewDocumentCommand{\vgrid}{ O{1} O{20} O{5} O{0.9} m}{%
\seq_set_split:Nnn \l_tmpa_seq{;}{#5}
% print question key (A, B, C...)
\int_step_inline:nnnn {1} {1} {#3} {%
\node at (##1#4, #2#4) {\int_to_Alph:n{##1}};}
% print question number (1, 2, 3...)
\int_step_inline:nnnn {#1} {1} {#1+#2-1} {%
\node at (0#4, #1#4+#2#4-1#4-##1#4) {##1};
\int_step_inline:nnnn {1} {1} {#3} {%
% draw an empty box for each question number/key
\node[draw,line~width=1#4pt,minimum~width=0.8#4cm,minimum~height=0.8#4cm] at (####1#4, ##1#4-#1#4) {};
% fill the correct box (0 to leave it empty)
\int_compare:nNnTF {####1} = {\seq_item:Nn \l_tmpa_seq {#1+#2-##1}} {\node[fill=blue,minimum~width=0.8#4cm,minimum~height=0.8#4cm] at (####1#4, ##1#4-#1#4) {};}{}
}
}
}
\ExplSyntaxOff
\begin{document}\pagestyle{empty}
\begin{tikzpicture}[x=1cm, y=1cm, font=\scriptsize\bfseries]
% short horizontal grid
\begin{scope}[anchor=south east,
local bounding box=bb1]
\hgrid{0;2;3;4;5;4;3;2;1;2;3;4;5;4;3;2;1;2;3;4}
\end{scope}
% long horizontal grid
\begin{scope}[anchor=south east,
shift={($(bb1.south west)+(0cm,-5cm)$)},
local bounding box=bb2]
\hgrid[21][30][6][0.6]{0;2;3;4;5;6;5;4;3;2;1;2;3;4;5;6;5;4;3;2;1;2;3;4;5;6;5;4;3;2}
\end{scope}
% short vertical grid
\begin{scope}[anchor=south east,
shift={($(bb2.south west)+(1cm,-12cm)$)},
local bounding box=bb3]
\vgrid[51][10][4][1.1]{0;2;3;4;5;4;3;2;1;2}
\end{scope}
\end{tikzpicture}
\end{document}
Note: I have made a major edit to my question and in the process fixed some errors. If any errors remain, they are not intended!



picand then use https://tex.stackexchange.com/q/185279/86 to position the pic. – Andrew Stacey Oct 21 '21 at 10:26