我正在写试卷,我想在问题之后立即写下解决方案。然后我想打开和关闭解决方案,这可以通过命令来实现\solution
。当解决方案打开时,会显示解决方案。当解决方案关闭时,我希望命令\lines{}
打印 n 行,其中 n 是解决方案中的行数。该命令\lines{3}
将打印三行。在下面的例子中,解决方案有 8 行长,所以我希望\value{equation}
将其设置为 8,以便 else 情况会针对此特定问题打印 8 行。我应该进行哪些更改才能使其正常工作?
\documentclass[12pt]{extarticle} % change for bigger fonts
\usepackage[utf8]{inputenc} % must include
\usepackage{exam} % the exam.sty styles
\usepackage{array}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{setspace}
\usepackage{gensymb}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{lineno}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\usetikzlibrary{arrows}
\newif\ifsolutions
\solutionstrue % set to true to show solutions, false to hide solutions
\newcounter{linescount} % Define a new counter
% Define a command to show a solution if the solutions boolean is true
\newcommand{\solution}[1]{%
\ifsolutions%
\setcounter{linescount}{0} % Reset the counter
Solution:
\begin{align*} #1
\end{align*}
\setcounter{linescount}{\value{equation}}
\else%
\lines{\thelinescount}%
\fi%
}
\begin{document} % begin the document
\begin{shortanswer} % short answer section
\begin{shortquestion}{}
\item\mrks{2} Solve for $x$:
\begin{equation*}
\displaystyle \frac{x+2}{5}-\frac{x-2}{3}=20
\end{equation*}
\solution{
\frac{3(x+2)}{15}-\frac{5(x-2)}{15}&=20\\
\frac{3x+6-5x+10}{15} &= 20\\
\frac{-2x+16}{15} &=20\\
-2x+16 &= 20 \times 15 \\
-2x+16 &= 300 \\
-2x &= 284 \\
x &= \frac{284}{-2} \\
x &= 142
}
\end{shortquestion}
\end{document}