# Embedding Examples & Solutions In Worksheets

How can I "embed" examples and solutions into my code to make easy-to-use worksheets and solutions manuals?

Context: I am creating a fractions workbook for students, many of whom have many unfortunate challenges in life, so the workbook must be uber easy to use. This means the solutions manual needs to look exactly like the worksheets, except with answers in them. I am struggling to make this happen to the point where I'm not sure even how to make a decent MWE.

Below are links to my attempted MWE and a PDF of what I want. There is a large gap to close!

(If this thread needs to be broken down into smaller bits, just let me know and I'll do that.)

Any suggestions?

How I want it to look, edited with pen My MWE of LaTeX code

\documentclass[12pt,letterpaper]{article}

\usepackage{amsmath}
\usepackage{exsheets}
\usepackage{tikz}

\newlength{\ALT} %Sets name new my length, where ALT = Answer Line Thickness
\setlength{\ALT}{0.5pt} %sets thickness of that line
\newlength{\DLL} % DLL = default line length; that is if I don't otherwise specify the line length, it will be as long as I set it to here.
\setlength{\DLL}{0.8cm}
\newcommand{\AS}[\DLL]{\rule{#1}{\ALT}}

\begin{document}

\section{Worksheet Now}
Find the unknown.

\begin{question}
$$3+\AS=4$$
\end{question}

\begin{solution}
$$1$$
\end{solution}

\begin{question}
$$4=\AS-10$$
\end{question}

\begin{solution}
$$14$$
\end{solution}

\noindent
Shade the indicated length starting at zero.
\begin{question}
$$\frac{2}{5}$$

\tikz[xscale=2]
{
% X axes
\draw [line width =1pt]
(0,0)--(3,0)
(0,.5)--(3,0.5);
% Vertical tick marks and answer boxes
\foreach \x in {0,...,15}
\draw [line width=1pt](\x/5,-0.4)--(\x/5,0.5);
% Whole Number Labels
\foreach \x in {0,1,2,3}
\node[below] at (\x,-0.6) {\x};
}
\end{question}

\begin{solution}
$$\frac{5}{3}$$

\tikz[xscale=2]
{
\filldraw[gray] (0,0) rectangle ({2/5},0.5);
% X axes
\draw [line width =1pt]
(0,0)--(3,0)
(0,.5)--(3,0.5);
% Vertical tick marks and answer boxes
\foreach \x in {0,...,15}
\draw [line width=1pt](\x/5,-0.4)--(\x/5,0.5);
% Whole Number Labels
\foreach \x in {0,1,2,3}
\node[below] at (\x,-0.6) {\x};
}
\end{solution}

\begin{question}
$$\frac{5}{3}$$

\tikz[xscale=1.5]
{
% X axes
\draw [line width =1pt]
(0,0)--(3,0)
(0,.5)--(3,0.5);
% Vertical tick marks and answer boxes
\foreach \x in {0,...,9}
\draw [line width=1pt](\x/3,-0.4)--(\x/3,0.5);
% Whole Number Labels
\foreach \x in {0,1,2,3}
\node[below] at (\x,-0.6) {\x};
}
\end{question}

\begin{solution}
$$\frac{5}{3}$$
\tikz[xscale=1.5]{
\filldraw[gray] (0,0) rectangle ({5/3},0.5);
% X axes
\draw [line width =1pt]
(0,0)--(3,0)
(0,.5)--(3,0.5);
% Vertical tick marks and answer boxes
\foreach \x in {0,...,9}
\draw [line width=1pt](\x/3,-0.4)--(\x/3,0.5);
% Whole Number Labels
\foreach \x in {0,1,2,3}
\node[below] at (\x,-0.6) {\x};
}
\end{solution}

\pagebreak
\section{Solutions Now}
\printsolutions

\end{document}

• @Jubobs. Ok, but it's a little long... Sep 3, 2015 at 19:23
– cfr
Sep 3, 2015 at 22:36
• @cfr I added the image :) Sep 3, 2015 at 22:50
• @cfr I misread your comment so my reply didn't make sense (which is why I deleted it again) Sep 3, 2015 at 23:35

I wouldn't use exsheets' solution environment unless you want the solutions printed separately from the exercises. Instead I'd use a conditional switch. For example exsheets provides

\PrintSolutionsTF{if answers are printed}{if answers are not printed}


One could use them to define a macro

\newcommand\answer{\PrintSolutionsTF{#1}{\phantom{#1}}}


This either prints the argument or the space the arguments needs. A bit more customization (color, underlined answer, space, … – uses the xcolor package):

\makeatletter

\ifmmode
\else
\fi
}

}
\newcommand\answer@line@math{\answerline@text{$#1#2$}}
\makeatother


With an lrbox one could also define an environment for larger chunks of input.

So a complete example could be the following:

\documentclass{article}
\usepackage{exsheets,amsmath,tikz}

\makeatletter

\ifmmode
\else
\fi
}

}
\newcommand\answer@line@math{\answerline@text{$#1#2$}}
\makeatother

% uncomment to get answers printed:
% \SetupExSheets{solution/print=true}

\begin{document}

\section*{Find the unknown.}
\begin{question}
$$3 + \answerline{1} = 4$$
\end{question}
\begin{question}
$$4 = \answerline{14} -10$$
\end{question}

\section*{Shade the indicated length starting at zero.}
\begin{question}
$$\frac{2}{5}$$

\begin{tikzpicture}
\PrintSolutionsT{ \fill[answercolor] (0,0) rectangle (1,.5) ; }
\draw[step=5mm] (0,0) grid (7.5,.5) ;
\foreach \x [evaluate=\x as \xpos using \x*2.5] in {0,...,3}
{ \draw (\xpos,0)--++(0,-.2) node[below] {\x} ; }
\end{tikzpicture}
\end{question}

\begin{question}
$$\frac{5}{3}$$

\begin{tikzpicture}
\PrintSolutionsT{ \fill[answercolor] (0,0) rectangle (2.5,.5) ; }
\draw[step=5mm] (0,0) grid (4.5,.5) ;
\foreach \x [evaluate=\x as \xpos using \x*1.5] in {0,...,3}
{ \draw (\xpos,0)--++(0,-.2) node[below] {\x} ; }
\end{tikzpicture}
\end{question}

\end{document}


This gives And with \SetupExSheets{solution/print=true}: • This looks fantastic! I was just out of town for the long weekend but will take a closer look at this soon. Thanks so much! Sep 8, 2015 at 4:09
• @WeCanLearnAnything glad if my answer helps :) Sep 8, 2015 at 21:30
• Ok, I've been going through the code for a bit now and would like to try to make sense of it. Am I allowed to create a new "answer" to this thread, copy your code, then put my comments in there to see if I understand? Sep 8, 2015 at 22:20
• That's not really how this site works. Maybe you can start a new thread to this respect over at latex-community.org/forum and leave me a comment here. A forum is much easier for discussions… we could also talk about this here in chat. Sep 8, 2015 at 22:40
• Ok, I'll try that. Sep 8, 2015 at 22:43