1

I'm working on this code

\documentclass[a4paper]{standalone}

\usepackage{amssymb,tikz}
\usetikzlibrary{positioning, calc}

\begin{document}
\newcounter{compteur}
\setcounter{compteur}{1}
\newcounter{lettres}
    \setcounter{lettres}{1}
    \begin{tikzpicture}
\node[matrix, inner sep=0](truc){
    \node[anchor=west, inner sep=0](question){Question~\thecompteur};
    \addtocounter{compteur}{1}
    \node[right=of question](carre\thelettres){$\square$};


    \node[above =\baselineskip of carre\thelettres]{\Alph{lettres}};
    \stepcounter{lettres}
    \foreach\x [evaluate = \x as \y using int(\x+1)] in {1,...,4}{
        \node[right=of carre\x](carre\y){$\square$};

        \node[above =\baselineskip of carre\thelettres]{\Alph{lettres}};
        \stepcounter{lettres}
        }

    \foreach\a in{2,...,15}{
        \node[anchor=east, below=\baselineskip of question, inner sep=0](question){Question~\thecompteur};
        \foreach\b in {1,...,5}{
            \path let \p1=(question), \p2=(carre\b) in node at (\x2,\y1){$\square$};
            }
    \addtocounter{compteur}{1}
    }
    \\
};
    \end{tikzpicture}
\end{document}

that compiles into :

enter image description here My problem is I would like to have several tables of this kind juxtaposed like some elements of another 6 rows/2columns matrix in which the 1st row/2nd column table element would have its first question labelled "Question 16" and so on.

Does a simple code solve this?

2

First step for reusing a sample is always creating a macro containing it. In your case these macro needs to accomplish three things:

  • adjustable number of first question (in case you don't want to continue counting in every situation)
  • variable number of questions
  • variable number of boxes

So I did the following:

\documentclass{standalone}

\usepackage{amssymb,tikz}
\usetikzlibrary{positioning}

\newcounter{question}
\setcounter{question}{1}
\newcounter{letter}

\tikzstyle{my question} = []
\tikzstyle{my box} = []
\tikzstyle{my heading} = []

% usage: \createtable[first question number]{number of questions}{number of letters}
\newcommand{\createtable}[3][\value{question}]{
  \setcounter{question}{#1} % do nothing by default
  \setcounter{letter}{1}
  \begin{tikzpicture}
  \node[my heading] (tmp1) {\Alph{letter}};
  \ifnum#3>1
  \foreach \current [evaluate = \current as \last using int(\current-1)] in {2,...,#3} {
    \stepcounter{letter}
    \node[my box, right=of tmp\last] (tmp\current) {\Alph{letter}};
  }
  \fi
%  \coordinate (upper right) at (tmp#3.north east);
  \foreach \y in {1,...,#2} {
    \node[my box, below=\baselineskip of tmp1] (tmp1) {$ \square $};
    \node[my question, left= of tmp1] (q\y) {Question~\arabic{question}};
    \stepcounter{question}
    \ifnum#3>1
    \foreach \x in {2,...,#3} {
      \node[my box, below=\baselineskip of tmp\x] (tmp\x) {$ \square $};
    }
    \fi
  }
%  \coordinate (lower left) at (q#2.south west);
%  \useasboundingbox (upper right) rectangle (lower left);
  \end{tikzpicture}
}

\begin{document}
  \createtable{5}{2}
  \createtable{2}{3}
\end{document}

Which leads to:

What do you need the matrix for? As far as I know it is not possible to put a & inside \foreach, so it seems to me that you have no benefit from that at all. In case you need that for a bounding box or something similar, check the comments in the code above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.