7

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

enter image description here

My MWE of LaTeX code

\documentclass[12pt,letterpaper]{article}

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

% underline/answer space macro
\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}[1][\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]
{
 % Shaded bar length
 \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]{
 % Shaded bar length
 \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}
10
  • Please edit your question and add your MWE there, instead of linking to it.
    – jub0bs
    Sep 3, 2015 at 19:22
  • @Jubobs. Ok, but it's a little long... Sep 3, 2015 at 19:23
  • Please upload the image showing how you want it to look here as well. I get a blank page with just Dropbox's header and footer when I follow your link. There's a Download button but clicking it does zilch for me.
    – cfr
    Sep 3, 2015 at 22:36
  • 1
    @cfr I added the image :)
    – cgnieder
    Sep 3, 2015 at 22:50
  • 1
    @cfr I misread your comment so my reply didn't make sense (which is why I deleted it again)
    – cgnieder
    Sep 3, 2015 at 23:35

1 Answer 1

3

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}
\PrintSolutionsT{if answers are printed}
\PrintSolutionsF{if answers are not printed}

One could use them to define a macro

\newcommand\answer[1]{\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
% a color for answers:
\colorlet{answercolor}{orange}

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

\newlength\answerspace
\setlength\answerspace{2cm}

\newcommand\answerline[1]{%
  \ifmmode
    \answerline@math{#1}%
  \else
    \answerline@text{#1}%
  \fi
}

\newcommand*\answerline@text[1]{%
  \underline{\makebox[\answerspace][c]{\answer{\color{answercolor}#1}}}%
}
\newcommand\answerline@math[1]{\mathpalette\answer@line@math{#1}}
\newcommand\answer@line@math[2]{\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
% a color for answers:
\colorlet{answercolor}{orange}

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

\newlength\answerspace
\setlength\answerspace{2cm}

\newcommand\answerline[1]{%
  \ifmmode
    \answerline@math{#1}%
  \else
    \answerline@text{#1}%
  \fi
}

\newcommand*\answerline@text[1]{%
  \underline{\makebox[\answerspace][c]{\answer{\color{answercolor}#1}}}%
}
\newcommand\answerline@math[1]{\mathpalette\answer@line@math{#1}}
\newcommand\answer@line@math[2]{\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

enter image description here

And with \SetupExSheets{solution/print=true}:

enter image description here

6
  • 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 :)
    – cgnieder
    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.
    – cgnieder
    Sep 8, 2015 at 22:40
  • Ok, I'll try that. Sep 8, 2015 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.