1

I would like to create a exercises&solutions math's sheet for my students.

I would love to be able to insert each exercise and solution in one section like :

\begin{exercise)
  $(2x-3)^2-(3x+2)^2=$
\end{exercise)
\begin{solution)
  $-(x+5)(5x-1)$
\end{solution)

and to be able to have that kind of rendering when compiling :

enter image description here

I looked at some packages here but I couldn't find exactly what I wanted.

Do you have some advices for me ?

cgnieder
  • 66,645
gilhoo
  • 147
  • I'm not exactly sure, but isn't that just rendering inputted math, with two sections? Possibly making one section float to the left. – GrandFleet Dec 19 '18 at 19:34
  • You should consider using the sagetex package as it can do the algebra problems. This makes it less likely that there is a mistake in your answer key. See, for example, the selected answer to the question here. – DJP Dec 21 '18 at 16:53

1 Answers1

4

This is doable (with not quite the required syntax, but…) with not too much effort with xsim.

The main idea is:

  • create templates for the desired layout
  • use the same environment body both for exercises and solutions using the parameter solution as option to the exercise environment (instead of setting this manually as option to each exercise it could also be built in into the template in order to set it automatically)
  • create a shortcut for \IfInsideSolutionTF to still be able to use different content in exercises and solutions
  • setup xsim to use the new templates

Please ask if something in the code is unclear.

\documentclass{article}
\usepackage[no-files]{xsim}
\usepackage{needspace}
\usepackage{tasks}

\DeclareExerciseEnvironmentTemplate{custom}{%
  \par\vspace{\baselineskip}
  \Needspace*{2\baselineskip}
  \noindent\sffamily
  \textbf{\XSIMmixedcase{\GetExerciseName}~\GetExerciseProperty{counter}}%
  \GetExercisePropertyT{subtitle}{\hspace{3em}{\small#1}}\par
  \normalfont
}{}

\DeclareExerciseEnvironmentTemplate{flushright}{%
  \begin{flushright}
  \begin{minipage}{.4\linewidth}
    \textsf{Solutions:}\par\normalfont
}{%
  \end{minipage}%
  \end{flushright}%
}

\xsimsetup{
  exercise/within = section ,
  exercise/template = custom ,
  solution/template = flushright
}

\renewcommand*\theexercise{\thesection-\arabic{exercise}}

\newcommand\QA[2]{\IfInsideSolutionTF{#2}{#1}}

\begin{document}

\setcounter{section}{1}
\setcounter{exercise}{30}

\begin{exercise}[ID=one,solution,subtitle=Factorize as much as possible the following expressions.]
  \begin{tasks}
    \task \QA{$(2x-3)^2 - (3x-2)^2 =$}{$-(x+5)(5x-1)$}
    \task \QA{$(x^2-25) - 2(5-x)(x+6) =$}{$(x+5)(3x-17)$}
    \task \QA{$2x(x+2) + (x+1)^2 + 2 =$}{$3(x+1)^2$}
  \end{tasks}
\end{exercise}
\printsolution{exercise}{one}

\end{document}

enter image description here

cgnieder
  • 66,645
  • Oh ! @cgnieder : I'm amazed ! I'm far away to know as much as you on Latex... Thx you so much for the time you took to code this answer. – gilhoo Apr 07 '20 at 19:04