# Put exam \answerline blanks as nodes inside \tikzpicture

The exam class allows you to leave spaces for students to write answers, using the answerline command. Furthermore, when \printanswers is enabled, an file can be generated that fills in these blanks with printed solutions. (See page 67 of the exam documentation.)

I would like to apply this idea to TikZ pictures. For example, a student is given answer boxes on a tikz picture and is asked to fill in the blanks. When \printanswers is enabled in the preamble, these blanks are completed with the teacher-supplied answers.

Here is a MWE where students are asked to fill in the blanks to label the unit circle. I have managed some makeshift answer boxes (using rectangle nodes):

\documentclass[addpoints,answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage[margin=.75 in]{geometry}
\usepackage{amsmath,amsfonts,amssymb,amsthm, color,srcltx,enumitem,bm,cancel,thmtools}
\usepackage{pgf,tikz}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{automata,arrows,positioning,calc,decorations.pathreplacing}
\usetikzlibrary{shapes} %For polygon nodes, see http://www.texample.net/tikz/examples/node-shapes/

\begin{document}
\begin{questions}
\question Fill in the designated angles and coordinates in the following unit circle:
\begin{center}
\begin{tikzpicture}[scale=5.3,cap=round,>=latex] %See http://www.texample.net/tikz/examples/unit-circle/
% draw the coordinates
\draw[->] (-1.5cm,0cm) -- (1.5cm,0cm) node[right,fill=white] {$x$};
\draw[->] (0cm,-1.5cm) -- (0cm,1.5cm) node[above,fill=white] {$y$};

% draw the unit circle
\draw[thick] (0cm,0cm) circle(1cm);

\foreach \x in {0,30,...,360} {
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:1cm);
% dots at each point
\filldraw[black] (\x:1cm) circle(0.4pt);
% draw each angle in degrees
\draw (\x:0.6cm) node[fill=white] {$\x^\circ$};
}
\foreach \x in {0,45,...,360} {
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:1cm);
% dots at each point
\filldraw[black] (\x:1cm) circle(0.4pt);
% draw each angle in degrees
\draw (\x:0.6cm) node[fill=white] {$\x^\circ$};
}
% draw each angle in radians
\foreach \x/\xtext in {
30/\frac{\pi}{6},
45/\frac{\pi}{4},
60/\frac{\pi}{3},
90/\frac{\pi}{2},
120/\frac{2\pi}{3},
135/\frac{3\pi}{4},
150/\frac{5\pi}{6},
180/\pi,
210/\frac{7\pi}{6},
225/\frac{5\pi}{4},
240/\frac{4\pi}{3},
270/\frac{3\pi}{2},
300/\frac{5\pi}{3},
315/\frac{7\pi}{4},
330/\frac{11\pi}{6},
360/2\pi}
\draw (\x:0.85cm) node {\tikz {\draw[fill=white] (0,0) rectangle ++(0.75,0.5);}};
\foreach \x/\xtext/\y in {
% the coordinates for the first quadrant
30/\frac{\sqrt{3}}{2}/\frac{1}{2},
45/\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
60/\frac{1}{2}/\frac{\sqrt{3}}{2},
% the coordinates for the second quadrant
150/-\frac{\sqrt{3}}{2}/\frac{1}{2},
135/-\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
120/-\frac{1}{2}/\frac{\sqrt{3}}{2},
% the coordinates for the third quadrant
210/-\frac{\sqrt{3}}{2}/-\frac{1}{2},
225/-\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
240/-\frac{1}{2}/-\frac{\sqrt{3}}{2},
% the coordinates for the fourth quadrant
330/\frac{\sqrt{3}}{2}/-\frac{1}{2},
315/\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
300/\frac{1}{2}/-\frac{\sqrt{3}}{2}}
\draw (\x:1.25cm) node {\tikz {\draw[fill=white] (-1.25,-0.75) rectangle ++(1.25,0.75);}};

% draw the horizontal and vertical coordinates
% the placement is better this way
\draw   (-1.25cm,0cm) node[fill=white] {\tikz {\draw[fill=white] (-1.25,-0.75) rectangle ++(1.25,0.75);}}
(1.25cm,0cm)  node[fill=white] {\tikz {\draw[fill=white] (-1.25,-0.75) rectangle ++(1.25,0.75);}}
(0cm,-1.25cm) node[fill=white] {\tikz {\draw[fill=white] (-1.25,-0.75) rectangle ++(1.25,0.75);}}
(0cm,1.25cm)  node[fill=white] {\tikz {\draw[fill=white] (-1.25,-0.75) rectangle ++(1.25,0.75);}};
\end{tikzpicture}
\end{center}

\begin{solution}
\begin{center}
\begin{tikzpicture}[scale=5.3,cap=round,>=latex]
% draw the coordinates
\draw[->] (-1.5cm,0cm) -- (1.5cm,0cm) node[right,fill=white] {$x$};
\draw[->] (0cm,-1.5cm) -- (0cm,1.5cm) node[above,fill=white] {$y$};

% draw the unit circle
\draw[thick] (0cm,0cm) circle(1cm);

\foreach \x in {0,30,...,360} {
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:1cm);
% dots at each point
\filldraw[black] (\x:1cm) circle(0.4pt);
% draw each angle in degrees
\draw (\x:0.6cm) node[fill=white] {$\x^\circ$};
}
\foreach \x in {0,45,...,360} {
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:1cm);
% dots at each point
\filldraw[black] (\x:1cm) circle(0.4pt);
% draw each angle in degrees
\draw (\x:0.6cm) node[fill=white] {$\x^\circ$};
}
% draw each angle in radians
\foreach \x/\xtext in {
30/\frac{\pi}{6},
45/\frac{\pi}{4},
60/\frac{\pi}{3},
90/\frac{\pi}{2},
120/\frac{2\pi}{3},
135/\frac{3\pi}{4},
150/\frac{5\pi}{6},
180/\pi,
210/\frac{7\pi}{6},
225/\frac{5\pi}{4},
240/\frac{4\pi}{3},
270/\frac{3\pi}{2},
300/\frac{5\pi}{3},
315/\frac{7\pi}{4},
330/\frac{11\pi}{6},
360/2\pi}
\draw (\x:0.85cm) node[fill=white] {$\xtext$};
\foreach \x/\xtext/\y in {
% the coordinates for the first quadrant
30/\frac{\sqrt{3}}{2}/\frac{1}{2},
45/\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
60/\frac{1}{2}/\frac{\sqrt{3}}{2},
% the coordinates for the second quadrant
150/-\frac{\sqrt{3}}{2}/\frac{1}{2},
135/-\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
120/-\frac{1}{2}/\frac{\sqrt{3}}{2},
% the coordinates for the third quadrant
210/-\frac{\sqrt{3}}{2}/-\frac{1}{2},
225/-\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
240/-\frac{1}{2}/-\frac{\sqrt{3}}{2},
% the coordinates for the fourth quadrant
330/\frac{\sqrt{3}}{2}/-\frac{1}{2},
315/\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
300/\frac{1}{2}/-\frac{\sqrt{3}}{2}}
\draw (\x:1.25cm) node[fill=white] {$\left(\xtext,\y\right)$};

% draw the horizontal and vertical coordinates
% the placement is better this way
\draw (-1.25cm,0cm) node[above=1pt] {$(-1,0)$}
(1.25cm,0cm)  node[above=1pt] {$(1,0)$}
(0cm,-1.25cm) node[fill=white] {$(0,-1)$}
(0cm,1.25cm)  node[fill=white] {$(0,1)$};
\end{tikzpicture}
\end{center}
\end{solution}
\end{questions}
\end{document}


Notice that separate figures are generated for the student's work and answer key. I would like \printanswers to simply print the solutions within the already supplied answer boxes.

It seems like \answerline is not supported within the TikZ picture environment, as it causes errors. Can anyone think of a better way to accomplish this?

• Can't you just define a style answer which has text opacity=0 that changes from 0 to 1 when you issue \printanswers? – user121799 Apr 16 '19 at 16:43

Here is a proposal, very much inspired by the beamer-overlay-styles library. There is a node style answer which has by default text opacity=0 but changes to text opacity=1 when \printanswers is issued. So

\documentclass[addpoints,answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage[margin=.75 in]{geometry}
\usepackage{amsmath,amsfonts,amssymb,amsthm, color,srcltx,enumitem,bm,cancel,thmtools}
\usepackage{pgf,tikz}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{automata,arrows,positioning,calc,decorations.pathreplacing}
\usetikzlibrary{shapes} %For polygon nodes, see http://www.texample.net/tikz/examples/node-shapes/
\begin{document}
\begin{questions}
\question Fill in the designated angles and coordinates in the following unit circle:
\begin{center}
\begin{tikzpicture}[scale=5.3,cap=round,>=latex]
% draw the coordinates
\draw[->] (-1.5cm,0cm) -- (1.5cm,0cm) node[right,fill=white] {$x$};
\draw[->] (0cm,-1.5cm) -- (0cm,1.5cm) node[above,fill=white] {$y$};

% draw the unit circle
\draw[thick] (0cm,0cm) circle(1cm);

\foreach \x in {0,30,...,360} {
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:1cm);
% dots at each point
\filldraw[black] (\x:1cm) circle(0.4pt);
% draw each angle in degrees
\draw (\x:0.6cm) node[fill=white] {$\x^\circ$};
}
\foreach \x in {0,45,...,360} {
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:1cm);
% dots at each point
\filldraw[black] (\x:1cm) circle(0.4pt);
% draw each angle in degrees
\draw (\x:0.6cm) node[fill=white] {$\x^\circ$};
}
% draw each angle in radians
\foreach \x/\xtext in {
30/\frac{\pi}{6},
45/\frac{\pi}{4},
60/\frac{\pi}{3},
90/\frac{\pi}{2},
120/\frac{2\pi}{3},
135/\frac{3\pi}{4},
150/\frac{5\pi}{6},
180/\pi,
210/\frac{7\pi}{6},
225/\frac{5\pi}{4},
240/\frac{4\pi}{3},
270/\frac{3\pi}{2},
300/\frac{5\pi}{3},
315/\frac{7\pi}{4},
330/\frac{11\pi}{6},
360/2\pi}
\draw (\x:0.85cm) node[fill=white,answer] {$\xtext$};
\foreach \x/\xtext/\y in {
% the coordinates for the first quadrant
30/\frac{\sqrt{3}}{2}/\frac{1}{2},
45/\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
60/\frac{1}{2}/\frac{\sqrt{3}}{2},
% the coordinates for the second quadrant
150/-\frac{\sqrt{3}}{2}/\frac{1}{2},
135/-\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
120/-\frac{1}{2}/\frac{\sqrt{3}}{2},
% the coordinates for the third quadrant
210/-\frac{\sqrt{3}}{2}/-\frac{1}{2},
225/-\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
240/-\frac{1}{2}/-\frac{\sqrt{3}}{2},
% the coordinates for the fourth quadrant
330/\frac{\sqrt{3}}{2}/-\frac{1}{2},
315/\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
300/\frac{1}{2}/-\frac{\sqrt{3}}{2}}
\draw (\x:1.25cm) node[fill=white,answer] {$\left(\xtext,\y\right)$};

% draw the horizontal and vertical coordinates
% the placement is better this way
\draw (-1.25cm,0cm) node[above=1pt] {$(-1,0)$}
(1.25cm,0cm)  node[above=1pt] {$(1,0)$}
(0cm,-1.25cm) node[fill=white] {$(0,-1)$}
(0cm,1.25cm)  node[fill=white] {$(0,1)$};
\end{tikzpicture}
\end{center}
\end{questions}
\end{document}


yields

but when \printanswers is uncommented it yields

Let me mention that the texts are only invisible, not gone. That is, if you give the students the pdf files, they will be able to copy and thus see the answer. However, if you print out the exams, this won't be an issue.

• Is there some way to ensure all "answer boxes" are the same size? Some are too small for students to write in. – Mathemanic Jan 23 at 6:19