在里面考試班,您可以通过取消注释命令来显示解决方案\printanswers
。这允许作者将问题和解决方案保留在同一个文档中,而无需手动注释和取消注释每个单独的解决方案。
使用\begin{solution}...\end{solution}
我可以跟进打印一个空白轴以及分离包含解决方案图的图表,但是这会带来占用大量空间、纸张和墨水的副作用。
发行时间\printanswers
与这个问题修改了 TikZ 图,我希望 LaTeX 决定打印下列内容之一:
\printanswers
注释掉后,用于绘制图形的空轴\printanswers
取消注释时,解决方案图显示在轴的顶部。
下面是我的 MWE,它当前打印两个单独的图:
\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}
有人能想到一个优雅的方法来实现这一点吗?谢谢你的关注。
答案1
这就像链接的答案一样:不透明度0
最初是如果你添加answer plot
到选项中\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}
1
但如果\printanswers
取消注释,即删除%
之前的 ,则会设置为\printanswers
。
答案2
您可以使用选修的包中,有如下几行
\usepackage[answers]{optional} % or use "noanswers"
然后,您无需注释或取消注释\printanswers
,只需将“answers”或“noanswers”传递给optional
包即可。然后使用类似
\opt{answers}{\printanswers}
...
\opt{noanswers}{\begin{tikzpicture} ... just axes here ...}