GeneralHow can I separate exercises from solutions?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

How can I separate exercises from solutions?

Post by LaTexLearner »

I'm in the midst of creating introductory multiplication and division exercises, exemplified by the following, except that I would like to separate the exercises from the solutions.
PicForOnlinePost.PNG
PicForOnlinePost.PNG (55.6 KiB) Viewed 11261 times


I can almost do it now... but not quite.

I made the code below which compiles, but does not separate the exercises from the solutions. I commented out the way I thought I could separate solutions and exercises, but that did not work unless I remove \Diagram from the definition of \Solution, meaning that there is some tikZ problem with \xdef... I think.

All the pictures and files are in this Dropbox folder. https://www.dropbox.com/sh/04uujszim2jr ... 2tTBa?dl=0

[Side note: I do not know how to turn normal text into a link, so I pasted that huge URL instead...]

Sorry for the long MWE; I'm not sure what I could remove and still get the point across! If anyone needs me to comment on any of the code, I'm happy to. :)

Code: Select all

\documentclass{article}

\usepackage[margin = 1.5cm, landscape]{geometry}
\usepackage{graphicx}
\usepackage{pgf}
 \pgfmathsetseed{\number\pdfrandomseed}
\usepackage{pgffor}
\usepackage{tikz}

\setlength{\parindent}{0cm}


\newcommand{\InitVariables}
{ \pgfmathrandominteger{\Sets}{1}{10}
  \pgfmathrandominteger{\PerSet}{1}{10}
  \pgfmathsetmacro{\Total}{int(\Sets*\PerSet)}
  }

\newcommand{\picturescale}{0.4}

\newcommand{\Diagram}{}
\newcommand{\ChosenPic}{}
\newcommand{\Exercise}{}
\newcommand{\Solution}{}

\newcommand{\GenOneExerciseAndSolution}
{\InitVariables
  
 \renewcommand{\Diagram}
  {\ifcase\PerSet\relax
    \or \renewcommand{\ChosenPic}{SetOfOne} 
    \or \renewcommand{\ChosenPic}{SetOfTwo} 
    \or \renewcommand{\ChosenPic}{SetOfThree} 
    \or \renewcommand{\ChosenPic}{SetOfFour} 
    \or \renewcommand{\ChosenPic}{SetOfFive} 
    \or \renewcommand{\ChosenPic}{SetOfSix}
    \or \renewcommand{\ChosenPic}{SetOfSeven} 
    \or \renewcommand{\ChosenPic}{SetOfEight} 
    \or \renewcommand{\ChosenPic}{SetOfNine}
    \or \renewcommand{\ChosenPic}{SetOfTen} 
    \fi
   
   \begin{tikzpicture}
   \foreach \x in {1,...,\Sets}
  {\node[anchor=center] at (2.3*\x,0) {\includegraphics[scale=\picturescale]{\ChosenPic}};}
   \end{tikzpicture}
   }  
  
 \renewcommand{\Exercise}
 {Exercise \x

  \_\_\_\_ \ boats

  \_\_\_\_ \ passengers in each boat

  \_\_\_\_ \ passengers in all

  \Diagram
  }


 \renewcommand{\Solution}
 {Solution \x

  \Sets \ boats

  \PerSet \ passengers in each boat

  \Total \ passengers in all

  \Diagram
  }
 }


\newcommand{\ManySolutions}{}

%This worked but did not separate exercises from solutions.

\newcommand{\ManyExercises}
{\foreach \x in {1,...,10}
  {\GenOneExerciseAndSolution \vskip 1cm \Exercise \vskip 1cm \Solution}
 }


%This did not work, but would separate exercises from solutions if it did.

%\newcommand{\ManyExercises}
%{\foreach \x in {1,...,10}
%  {\GenOneExerciseAndSolution \vskip 1cm \Exercise
%   \xdef\ManySolutions{\ManySolutions \Solution}    
%   }
% }


\begin{document}

\Large

\ManyExercises

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics
Post Reply