3

In the exam class, you can reveal solutions by uncommenting the command \printanswers. This allows the author to keep both question and solution in the same document, without needing to manually comment and uncomment each individual solution.

Using \begin{solution}...\end{solution} I can follow up printing a blank axis alongside with a separate graph containing the solution plot, but this has the side-effect of using a lot of space, paper, and ink.

When \printanswers is issued, in a similar vein to this question which modifies a TikZ plot, I would like LaTeX to decide to print ONE of the following :

  • an empty axis for drawing a graph when \printanswers is commented out
  • The solution plot on top of the axis when \printanswers is uncommented.

Below is my MWE, which currently prints two separate plots:

\documentclass[addpoints]{exam}
\usepackage[margin=.75 in]{geometry}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{automata,arrows,positioning,calc,decorations.pathreplacing}
\usetikzlibrary{shapes}
\pgfplotsset{my style/.append style={grid,
axis x line=middle, axis y line=middle, xlabel={$x$}, ylabel={$y$}}
}

%The following is to make a node style answer which has by default text opacity=0 but changes to text opacity=1 when \printanswers is issued. See https://tex.stackexchange.com/questions/485101/put-exam-answerline-blanks-as-nodes-inside-tikzpicture
\tikzset{answer/.style={draw,text opacity=0}}
\let\oldprintanswers\printanswers
\def\printanswers{\oldprintanswers\tikzset{answer/.style={text opacity=1}}}
\printanswers

\begin{document}
\section{Exam}
\begin{questions}
\question[20] (Graphing)
Graph $y=\frac{1}{2}x-3$:
\begin{center}
\begin{tikzpicture}
\begin{axis}[my style, height=7cm,
xtick={-5,-4,...,5}, ytick={-5,-4,...,5},
xmin=-6, xmax=6, ymin=-6, ymax=6]
\end{axis}
\end{tikzpicture}
\end{center}

\begin{solution}
\begin{center}
\begin{tikzpicture}
\begin{axis}[my style, height=7cm,
xtick={-5,-4,...,5}, ytick={-5,-4,...,5},
xmin=-6, xmax=6, ymin=-6, ymax=6]
\addplot[domain=-6:6, samples=100, mark=none, thick, blue]{1/2*x-3};
\end{axis}
\end{tikzpicture}
\end{center}
\end{solution}

\newpage

\question[20] 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
\draw (-1.25cm,0cm) node[fill=white,answer] {$(-1,0)$}
      (1.25cm,0cm)  node[fill=white,answer] {$(1,0)$}
      (0cm,-1.25cm) node[fill=white,answer] {$(0,-1)$}
      (0cm,1.25cm)  node[fill=white,answer] {$(0,1)$};
\end{tikzpicture}
\end{center}
\end{questions}
\end{document}

Can anyone think of an elegant way to accomplish this? Thanks for looking.

2

This works like the linked answer: the opacity is 0 initially if you add answer plot to the options of \addplot,

\documentclass[addpoints]{exam}
\usepackage[margin=.75 in]{geometry}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{automata,arrows,positioning,calc,decorations.pathreplacing}
\usetikzlibrary{shapes}
\pgfplotsset{my style/.append style={grid,
axis x line=middle, axis y line=middle, xlabel={$x$}, ylabel={$y$}}
}

%The following is to make a node style answer which has by default text opacity=0 but changes to text opacity=1 when \printanswers is issued. See https://tex.stackexchange.com/questions/485101/put-exam-answerline-blanks-as-nodes-inside-tikzpicture
\tikzset{answer/.style={draw,text opacity=0},answer plot/.style={opacity=0}}
\let\oldprintanswers\printanswers
\def\printanswers{\oldprintanswers\tikzset{answer/.style={text opacity=1},
answer plot/.style={opacity=1}}}
%\printanswers

\begin{document}
\section{Exam}
\begin{questions}
\question[20] (Graphing)
Graph $y=\frac{1}{2}x-3$:

%\begin{solution}
\begin{center}
\begin{tikzpicture}
\begin{axis}[my style, height=7cm,
xtick={-5,-4,...,5}, ytick={-5,-4,...,5},
xmin=-6, xmax=6, ymin=-6, ymax=6]
\addplot[domain=-6:6, samples=100, mark=none, thick, blue,answer plot]{1/2*x-3};
\end{axis}
\end{tikzpicture}
\end{center}
%\end{solution}

\newpage

\question[20] 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
\draw (-1.25cm,0cm) node[fill=white,answer] {$(-1,0)$}
      (1.25cm,0cm)  node[fill=white,answer] {$(1,0)$}
      (0cm,-1.25cm) node[fill=white,answer] {$(0,-1)$}
      (0cm,1.25cm)  node[fill=white,answer] {$(0,1)$};
\end{tikzpicture}
\end{center}
\end{questions}
\end{document}

enter image description here

but gets set to 1 if \printanswers is uncommented, i.e. you remove the % before \printanswers.

enter image description here

|improve this answer|||||
  • It works! Thank you! – Mathemanic Jul 1 '19 at 5:29
0

You could use the optional package, with lines like

\usepackage[answers]{optional} % or use "noanswers"

and then instead of commenting or uncommenting \printanswers, you would pass "answers" or "noanswers" to the optional package. Then use commands like

\opt{answers}{\printanswers}
...
\opt{noanswers}{\begin{tikzpicture} ... just axes here ...}
|improve this answer|||||

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.