3

The code below generates several exams with the name of the students. The code also randomizes the order of enumerate items.

I’d like to print at the end of this document (in an additional page) each student’s name and the corresponding Answer Key (question number + correct letter). Is it possible? It is possible to label in some way the items corresponding to the correct answers and print them at the end?

HOW COULD I DO THIS?

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[top=6mm,includehead,left=1.5cm,bottom=2cm,right=1.5cm,headsep=0.5cm]{geometry}
\usepackage{tikz}
\usepackage{float} % pct para crrg fig. com 2 ou + colunas.
\usepackage{multicol} % pct para crrg 2 ou + colunas
\usepackage{xcolor}
\usepackage{fancyhdr} 
\usepackage{graphicx}
\usepackage{amsfonts,amssymb,amsmath}



\usepackage{etoolbox}
\usepackage{pgfkeys}
\usepackage{pgfmath}

% code for generating a random permutation
\newcounter{randomListLength}%   current length of our random list
\newcounter{randomListPosition}% current list index
\newcounter{newRandomListElementPosition}% position to insert new element
% insert #1 into the next position of \newRandomList unless the position
% index \randomListPosition is equal to \newRandomListElementPosition in
% which case the \newRandomListElement is added first
\newcommand\randomlyInsertElement[1]{%
  \stepcounter{randomListPosition}%
  \ifnum\value{randomListPosition}=\value{newRandomListElementPosition}%
    \listxadd\newRandomList{\newRandomListElement}%
  \fi%
  \listxadd\newRandomList{#1}%
}
% \randomlyInsertInList{list name}{new list length}{new element}
\newcommand\randomlyInsertInList[3]{%
  \pgfmathparse{random(1,#2)}%
  \setcounter{newRandomListElementPosition}{\pgfmathresult}%
  \ifnum\value{newRandomListElementPosition}=#2\relax%
    \listcsxadd{#1}{#3}%
  \else%
    \def\newRandomList{}% start with an empty list
    \def\newRandomListElement{#3}% and the element that we need to add
    \setcounter{randomListPosition}{0}% starting from position 0
    \xdef\currentList{\csuse{#1}}
    \forlistloop\randomlyInsertElement\currentList%
    \csxdef{#1}{\newRandomList}%
  \fi%
}
% define some pgfkeys to allow key-value arguments
\pgfkeys{/randomList/.is family, /randomList,
  environment/.code = {\global\letcs\beginRandomListEnvironment{#1}
                       \global\letcs\endRandomListEnvironment{end#1}
                      },
  enumerate/.style = {environment=enumerate},
  itemize/.style = {environment=itemize},
  description/.style = {environment=description},
  seed/.code = {\pgfmathsetseed{#1}}
}
\pgfkeys{/randomList, enumerate}% enumerate is the default

% finally, the code to construct the randomly permuted list
\makeatletter
\newcounter{randomListCounter}% for constructing \randomListItem@<k>'s

% \useRandomItem{k} prints item number k
\newcommand\useRandomItem[1]{\csname randomListItem@#1\endcsname}

% \setRandomItem{k} saves item number k for future use
% and builds a random permutation at the same time
\def\setRandomItem#1\par{\stepcounter{randomListCounter}%
       \expandafter\protected@xdef\csname randomListItem@\therandomListCounter\endcsname{\noexpand\item#1}%
       \randomlyInsertInList{randomlyOrderedList}{\therandomListCounter}{\therandomListCounter}%
}%
\let\realitem=\item
\makeatother
\newenvironment{randomList}[1][]{% optional argument -> pgfkeys
  \pgfkeys{/randomList, #1}% process optional arguments
  \setcounter{randomListLength}{0}% initialise length of random list
  \def\randomlyOrderedList{}% initialise the random list of items
  % Nthing is printed in the main environment. Instead, \item is
  % used to slurp the "contents" of the item into randomListItem@<counter>
  \let\item\setRandomItem%      
}
{% now construct the list environment by looping over the randomly ordered list
  \let\item\realitem
  \setcounter{randomListCounter}{0}
  \beginRandomListEnvironment\relax
    \forlistloop\useRandomItem\randomlyOrderedList
  \endRandomListEnvironment
}
% test compatibility with enumitem
\usepackage{enumitem}
\newlist{Testlist}{enumerate}{1} %
\setlist[Testlist]{label*=\alph*)}
\setlist{nosep}\parindent=0pt% for more compact output




%----------- PARA CRIAR PÁGINA EM BRANCO, CASO NÚMERO DE PÁGINAS SEJA ÍMPAR --------------------
\usepackage{afterpage}
\newcommand\blankpage{%
    \null
    \thispagestyle{empty}%
    \addtocounter{page}{-1}%
    \newpage}
%----------- DESCOMENTAR "\afterpage{\blankpage}" AO FINAL DO DOCUMENTO ------------------------




%-----------     RELACIONADO AO CABEÇALHO E RODAPÉ     -----------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\titlee}{\textbf{Exam 1 - Physics}}
\newcommand{\data}{\today}
\newcommand{\valor}{12,0}
%\newcommand{\class}{\phantom{CTAII-2V}} % Não adicionar nome da turma
\newcommand{\class}{CTAII-2V}            % Adicionar nome da turma




\renewcommand{\footrulewidth}{1.2pt} % linha no footer
\renewcommand{\headrulewidth}{1.2pt}% linha no header 

\rhead{\ttfamily \titlee{}}
\lfoot{\ttfamily Prof. Lucas }
\rfoot{\ttfamily \data{}}

\pagestyle{fancy} 

\fancypagestyle{firststyle}{%
  \renewcommand{\headrulewidth}{0pt}% Remove header rule
  \lhead{%
    \def\scale{0.6}%
    \def\roundc{\scale*0.1cm}% rc = 10% de sc
    \raisebox{-2\baselineskip}[0pt][0pt]{\begin{tikzpicture}[scale=\scale]
      \definecolor{tempcolor}{RGB}{200,25,30}
      \filldraw[tempcolor][rounded corners=\roundc] (2cm,0cm) rectangle (1.2cm,-0.5cm);
      \filldraw[tempcolor][rounded corners=\roundc] (2cm,-0.6cm) rectangle (1.1cm,-1.1cm);
      \filldraw[tempcolor][rounded corners=\roundc] (2cm,-0.6cm) rectangle (1.5cm,-1.1cm);
      \filldraw[tempcolor][rounded corners=\roundc] (1.7cm,-1.2cm) rectangle (1.2cm,-1.7cm);
      \filldraw[tempcolor][rounded corners=\roundc] (0.6cm,-1.2cm) rectangle (1.1cm,-1.7cm);
      \filldraw[tempcolor][rounded corners=\roundc] (1.6cm,-1.2cm) rectangle (1.8cm,-1.7cm);
      \filldraw[tempcolor][rounded corners=\roundc] (1.7cm,-1.8cm) rectangle (1.2cm,-2.3cm);
      \node[scale=\scale,fill=white] at (5.92,-0.85) {\resizebox{6.7cm}{0.5cm}{\sffamily \textbf{PHANTOM}}};
      \node[scale=\scale,fill=white] at (4.52,-1.55) {\resizebox{5cm}{0.5cm}{\sffamily {School}}};
    \end{tikzpicture}}
  }
  \chead{\raisebox{-2\baselineskip}[0pt][0pt]{\rule[-2.5\baselineskip]{\linewidth}{1.2pt}}}
  \rhead{\raisebox{-2\baselineskip}[0pt][0pt]{\begin{tabular}{@{}r@{}}
    \tikz \node at (0,0) {\titlee{}};\\ 
    \tikz[baseline]{\node[anchor=base, draw=black, thick, minimum width=6.5cm,
    minimum height=7mm,rounded corners, yshift=-2mm,label={[fill=white,label
    distance=-2mm,inner xsep=2pt]90:{\ttfamily Name}:}] {\ttfamily \myname};} 
    \tikz[baseline]{\node[anchor=base, draw=black, thick, minimum width=2cm, minimum height=7mm,rounded corners, yshift=-2mm,label={[fill=white,label distance=-2mm,inner xsep=2pt]90:{\ttfamily Sign.}:}] {\phantom{\ttfamily 15,0}};}    
    \tikz[baseline]{\node[anchor=base, draw=black, thick, minimum width=2cm, minimum height=7mm,rounded corners, yshift=-2mm,label={[fill=white,label distance=-2mm,inner xsep=2pt]90:{\ttfamily Class}:}] {\ttfamily \class};}   
    \tikz[baseline]{\node[anchor=base, draw=black, thick, minimum width=1.5cm, minimum height=7mm,rounded corners, yshift=-2mm,label={[fill=white,label distance=-2mm,inner xsep=2pt]90:{\ttfamily Marks}:}] {\ttfamily 15,0};} 
    \tikz[baseline]{\node[anchor=base, draw=black, thick, minimum width=1.5cm, minimum height=7mm,rounded corners, yshift=-2mm,label={[fill=white,label distance=-2mm,inner xsep=2pt]90:{\ttfamily Nota}:}] {\phantom{\ttfamily 15,0}};}
    \end{tabular}}
  }
  \lfoot{\ttfamily Prof. Lucas}
  \rfoot{\ttfamily \today}
}

\AtBeginDocument{
  \thispagestyle{firststyle}
  \vspace*{2\baselineskip}
}
\usepackage{pgfplotstable}
% from https://tex.stackexchange.com/a/445369/121799
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{#3}\of{#1}%
    \let#4\pgfplotsretval
}

\usepackage{filecontents}
\begin{filecontents*}{students.csv}
name 
{Cosmic Cuttlefish}
{Bionic Beaver}
{Xenial Xerus}
{Trusty Tahr}
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%----------- PARA ADICIONAR CÍRCULO NEGRO À NUMERAÇÃO -------------------------------------------------------------
\newcommand*\circleded[1]{%
  \tikz[baseline=(char.base)]
    \node[
      shape=circle,
      fill,
      inner sep=1.5pt,
      text=white,
      minimum size=1.5em
    ] (char) {#1};
}
%------------------------------------------------------------------------------------------------------------------


\begin{document}
\pgfplotstableread{students.csv}\loadedtable
\pgfplotstablegetrowsof{\loadedtable}
\pgfmathtruncatemacro{\rownum}{\pgfplotsretval-1}
\foreach \X in {0,...,\rownum}
{\ReadOutElement{\loadedtable}{\X}{name}{\tmpname}
\xdef\myname{\tmpname}
\thispagestyle{firststyle}
\vspace*{2\baselineskip}
\begin{multicols}{2}

\fbox{\begin{minipage}{21.5em}
\begin{center}
\vspace{2mm}
{\sffamily\small \bf INSTRUCTIONS}
\end{center}
\begin{itemize}[noitemsep]
{\footnotesize
   \item SIGN YOUR NAME ON THIS SHEET.
   \item BE SURE EACH MARK IS DARK AND COMPLETELY FILLS THE INTEENDED SPACE.
   \item USE ONLY A PENCIL WITH SOFT, BLACK LEAD (Nº 2 OR HB) TO COMPLETE THE ANSWER SHEET.
   \item COMPLETELY ERASE ANY ERRORS OT STRAY MARKS.\\
}
\end{itemize}
\end{minipage}}

%%%%%%%%%%% PARA O CARTÃO-RESPOSTA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{center}
\fbox{\begin{minipage}{20em}
\begin{center}
\item 
{\sffamily\small
\textbf{ANSWER SHEET}\vspace{6pt}
\begin{tikzpicture}[font={\sffamily}] % sans serif font (like Arial)
  \foreach \col in {0,1} { %total number of columns of answers -1
   \begin{scope}[xshift=\col*3.2 cm]
    \foreach \line in {1,2,...,4} { %%% ESPECIFICAR AQUI O NÚMERO DE LINHAS
        \begin{scope}[yshift=-.6*\line cm]
        \pgfmathparse{int(Mod(\line,2))?"gray!30":"white"} % alternating row colours
        \edef\filler{\pgfmathresult}

                \draw [fill=\filler] (-.22,-.35) rectangle(2.8cm,.7em);
         \pgfmathtruncatemacro\q{(4*\col)+\line}  %%% O FATOR MULTIPLICATIVO DE \col DEVE SER O NÚMERO DE LINHAS
           \node at (0,-.05) {\textbf{\q}}; %print line number

          \footnotesize \foreach \count/\desc in {1/A, 2/B, 3/C, 4/D, 5/E} { % \desc = value in circle
                %\node at ({\count * 0.5}, 0.4) {\desc};
                %\node (rect) at (0,0){};

                \node[draw,circle,inner sep=0.6pt] at ({\count * 0.5},-.05) {\desc}; %print letter in circle
            }
        \end{scope}
    } %end of foreach set of lines
    \end{scope} %each column region
   }
\end{tikzpicture}
}
\end{center}
\end{minipage}}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{enumerate}
%%%%%%% PARA ADICIONAR CÍRCULO NEGRO À NUMERAÇÃO %%%%%%%%%%%%
\renewcommand{\labelenumi}{\bfseries\circleded{\theenumi}} 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



 \item {\framebox{\scriptsize \sf 1,5}} When a certain force is applied to an object with a mass of 2 kg, its acceleration is 10.0 m/s$^2$. When the same force is applied to a different object, its acceleration is 4.0 m/s$^2$. The mass of second object is
\vspace{2mm}
\begin{randomList}[environment=Testlist]
\item 10.0 kg

\item 8.0 kg

\item 5.0 kg

\item 2.5 kg

\item 1.25 kg

  \end{randomList}


\vspace{1cm} 


 \item {\framebox{\scriptsize \sf 1,5}} If a firefly collides with the windshield of a fast-moving bus, which statement is correct?
  \begin{randomList}[environment=Testlist]
   \item The firefly experiences an impact force with a larger magnitude

   \item The bus experiences an impact force with a larger magnitude

   \item The firefly and bus experience forces with same direction

   \item The firefly and bus experience forces with same magnitude

   \item None of the above

  \end{randomList}


\vspace{1cm} 


\item  {\framebox{\scriptsize \sf 1,5}} The tension in a string from which a 4.0-kg object is suspended in an elevator is equal to 44 N. What is the acceleration of the elevator?
\vspace{2mm} 
  \begin{randomList}[environment=Testlist]
   \item 11 m/s$^2$ upward

   \item 1.2 m/s$^2$ upward

   \item 1.2 m/s$^2$ downward

   \item 10 m/s$^2$ upward

   \item 2.4 m/s$^2$ downward

  \end{randomList} 


\vspace{1cm} 


\item  {\framebox{\scriptsize \sf 1,5}} What is the smallest value of the force F such that the 2.0-kg block will not slide down the wall? The coefficient of static friction between the block and the wall is 0.2.
\vspace{2mm}
\begin{randomList}[environment=Testlist]
   \item 98 N

   \item 20 N

   \item 10 N

   \item 4 N

   \item 2 N

\end{randomList}  


\vspace{1cm} 


\item {\framebox{\scriptsize \sf 1,5}} In a game of shuffleboard (played on a horizontal surface), a puck is given an initial speed of 6.0 m/s. It slides a distance of 9.0 m before coming to rest. What is the coefficient of kinetic friction between the puck and the surface?
\vspace{2mm}
\begin{randomList}[environment=Testlist]
   \item 0.20

   \item 0.18

   \item 0.15

   \item 0.13

   \item 0.27

\end{randomList}   


\vspace{1cm}  


\item {\framebox{\scriptsize \sf 1,5}} A 3.0-kg block slides on a frictionless 20º inclined plane. A force of 16 N acting parallel to the incline and up the incline is applied to the block. What is the acceleration of the block?
\vspace{2mm}
\begin{randomList}[environment=Testlist]
\item  2.0 m/s$^2$ down the incline

\item  5.3 m/s$^2$ up the incline

\item  2.0 m/s$^2$ up the incline

\item  3.9 m/s$^2$ down the incline

\item  3.9 m/s$^2$ up the incline

\end{randomList}     


\vspace{1cm}  


\item {\framebox{\scriptsize \sf 1,5}} An airplane flies in a horizontal circle of radius 500 m at a speed of 150 m/s. If the plane were to fly in the same 1000 m circle at a speed of 300 m/s, by what factor would its centripetal acceleration change?
\vspace{2mm}
\begin{randomList}[environment=Testlist]
\item  0.25

\item  0.50

\item  1.00

\item  2.00

\item  4.00

\end{randomList}  


\vspace{1cm}  


\item {\framebox{\scriptsize \sf 1,5}} A 5-kg ball hangs at one end of a string that is attached to a support on a railroad boxcar. When the boxcar accelerates to the right, the rope makes an angle of 30º with the vertical. The acceleration of gravity is 9.8 m/s$^2$ and air resistance is negligible. Find the acceleration of the boxcar.
\vspace{2mm}
\begin{randomList}[environment=Testlist]
\item  56.6 m/s$^2$

\item  49.0 m/s$^2$

\item  17.0 m/s$^2$

\item  9.8 m/s$^2$

\item  5.7 m/s$^2$

\end{randomList}    



\end{enumerate}

\end{multicols} % fim ambiente 2 ou + cols.
%\afterpage{\blankpage}
\clearpage
\setcounter{page}{1}
}

\end{document}
  • Yes, I've done something similar using the textmerg package. – JPi Dec 20 '18 at 21:12
  • @JPi , could you be more specific in your answer? – FrozenCore Dec 23 '18 at 9:39

Your Answer

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

Browse other questions tagged or ask your own question.