Text FormattingLonger Embedded Solutions

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Longer Embedded Solutions

Post by LaTexLearner »

This is basically part III of my questions about embedding examples and solutions into my worksheets using exsheets. Here is Part I and Part II.

Now, I'm trying to figure out how to keep the answer spacing consistent for longer answers across both the worksheets and the solution pages. It didn't work in this MWE: depending on whether the printing is set to \SetUpExsheets{solution/print\true or \SetUpExsheets{solution/print\false, the answer spacing varies. It may have had something to do with my use of the \phantom in both of the "false" arguments.

(I also don't know why the last line is orange, btw...)

Code: Select all

\documentclass[12pt,letterpaper]{article}

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

\colorlet{answercolor}{orange}

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

\begin{document}

\begin{question}
Show \(\frac{5}{4}+\frac{3}{4}=2\) using a number line.
\end{question}

\PrintSolutionsTF
 {
  \vspace{1.5cm}
  \begin{tikzpicture}[xscale=3,answercolor]
  \draw(0,0)--(3,0);
  \draw[-latex] (0,0.4)--(5/4,0.4);
  \draw[-latex] (5/4,0.4)--(8/4,0.4);
  \foreach \x in {0,1,2,3}
   \node[below] at (\x,-0.3) {\x};
  \foreach \x in {0,...,12}
   \draw (\x/4,-0.2)--(\x/4,0.2);
  \end{tikzpicture}
  \vspace{1.5cm}
 }
 {
  \phantom
   {
    \vspace{1.5cm}
    \begin{tikzpicture}[xscale=3,answercolor]
    \draw(0,0)--(3,0);
    \draw[-latex] (0,0.4)--(5/4,0.4);
    \draw[-latex] (5/4,0.4)--(8/4,0.4);
    \foreach \x in {0,1,2,3}
     \node[below] at (\x,-0.3) {\x};
    \foreach \x in {0,...,12}
     \draw (\x/4,-0.2)--(\x/4,0.2);
    \end{tikzpicture}
    \vspace{1.5cm}
   }
 }

\begin{question}
What is the mathematical purpose of fractions?
\end{question}

\PrintSolutionsTF
{\color{answercolor}{Fractions allow for equal sharing when ``normal" division would either produce remainders e.g. \(11\div3\) or be impossible, e.g. \(3\div4\). The second purpose of fractions is to be the numbers in between whole numbers.}\vspace{2cm}}
{\phantom{Fractions allow for equal sharing when ``normal" division would either produce remainders e.g. \(11\div3\) or be impossible, e.g. \(3\div4\). The second purpose of fractions is to be the numbers in between whole numbers.}\vspace{2cm}}

\begin{question}
Just to see the spacing below Exercise 2... Not sure why this is orange, though...
\end{question}

\end{document}
And once I figure this out, I'm going to try to create a macro that prevents me from having to type out every solution twice, the phantom thing, etc...

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Longer Embedded Solutions

Post by cgnieder »

Writing things twice for every answer really is no satisfying solution. Here is a suggestion for an environment you could use:

Code: Select all

\documentclass[12pt,letterpaper]{article}

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

\colorlet{answercolor}{orange}

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

\newbox\answerbox
\newenvironment{longanswer}
  {%
    \par\noindent
    \begin{lrbox}{\answerbox}
      \begin{minipage}{\linewidth}
      \begin{quote}
        \color{answercolor}%
  }
  {%
      \end{quote}%
      \end{minipage}%
    \end{lrbox}%
    \PrintSolutionsTF
      {\usebox{\answerbox}}
      {\phantom{\usebox{\answerbox}}}%
    \par
  }

\begin{document}

\begin{question}
  Show \(\frac{5}{4}+\frac{3}{4}=2\) using a number line.
\end{question}

\begin{longanswer}
  \vspace*{1.5cm}
  \begin{tikzpicture}[xscale=3,answercolor]
  \draw(0,0)--(3,0);
  \draw[-latex] (0,0.4)--(5/4,0.4);
  \draw[-latex] (5/4,0.4)--(8/4,0.4);
  \foreach \x in {0,1,2,3}
   \node[below] at (\x,-0.3) {\x};
  \foreach \x in {0,...,12}
   \draw (\x/4,-0.2)--(\x/4,0.2);
  \end{tikzpicture}
  \vspace*{1.5cm}
\end{longanswer}

\begin{question}\label{qu:fractions-purpose}
  What is the mathematical purpose of fractions?
\end{question}

\begin{longanswer}
  Fractions allow for equal sharing when ``normal" division would either
  produce remainders e.g. \(11\div3\) or be impossible, e.g. \(3\div4\). The
  second purpose of fractions is to be the numbers in between whole numbers.
\end{longanswer}

\begin{question}
  Just to see the spacing below Exercise~\ref{qu:fractions-purpose}\ldots\ Not
  sure why this is orange, though\ldots
\end{question}

\end{document}
site moderator & package author
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Longer Embedded Solutions

Post by Stefan Kottwitz »

By the way: I noticed the use of \vspace*{1.5cm}. Earlier it was \vspace without star. If a page break would happen at this position, \vspace* would produce the space, \vspace would omit it. The latter I like more, because here (and often) the space is meant as separation space, and at the top of a page there's no separation needed to the text before. Similar at the page bottom.

Stefan
LaTeX.org admin
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: Longer Embedded Solutions

Post by cgnieder »

True. But IMHO in exercise sheets at the top of a page should start a new exercise anyway (which very likely means manual page breaking, too) so it shouldn't really matter here...

I'm also not sure if it really would be discarded here, anyway: the whole answer is inside a minipage.
site moderator & package author
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Longer Embedded Solutions

Post by LaTexLearner »

cgnieder wrote:Writing things twice for every answer really is no satisfying solution...
No kidding!

It's obvious what the environment does and I can see the structure of it being a quote inside of a minipage inside of an lrbox.

I don't quite get what those things do, though.
  1. What is the purpose of using the quote?
  2. What does \lrbox do? I Googled it, as usual, but I found the explanations jargon-rich to the point where I couldn't make sense of it. I understand it's some type of box that can be moved and manipulated...
  3. ... But then there's a minipage inside of that box. This kind of sounds like a box inside of a box. Why?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Longer Embedded Solutions

Post by cgnieder »

LaTexLearner wrote:I don't quite get what those things do, though.
  1. What is the purpose of using the quote?
  2. What does \lrbox do? I Googled it, as usual, but I found the explanations jargon-rich to the point where I couldn't make sense of it. I understand it's some type of box that can be moved and manipulated...
  3. ... But then there's a minipage inside of that box. This kind of sounds like a box inside of a box. Why?
  1. The {quote} environment isn't necessary but since it sets it contents apart from the usual text we get some spacing around the answer this way. Just an easy way of having some visual formatting.
  2. The {lrbox} environment is handy when trying to catch the contents of an environment inside a box. It is the environment version of \sbox if you will.
  3. The {minipage} is so we don't get one single long line inside the box we store. This way we have line breaking etc.
site moderator & package author
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Longer Embedded Solutions

Post by LaTexLearner »

Alright.

It will take me time to figure out what the boxes and minipages do, but I will spend some more time on that alone.

Onto the next type of longer answers, the ones with TikZ.

How do I make exercise #2 have the same layout whether or not the solution is printed?

Code: Select all

\documentclass[12pt,letterpaper]{article}

\usepackage{exsheets}
\colorlet{answercolor}{orange}

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

\usepackage{tikz}

\begin{document}

\noindent
Each horizontal line is broken into how many segments?

\begin{question}
\tikz{
\draw (0,0)--(4,0);
\foreach \x in {0,...,4}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,3}
 \draw[-latex, answercolor] (\x+0.1,-0.15)--(\x+0.9,-0.15);
\node[answercolor] at (2,-0.8) {4};
} 
\end{question}

\begin{question}
\tikz{
\draw (0,0)--(6,0);
\foreach \x in {0,...,6}
 \draw (\x,-0.3)--(\x,0.3);
\PrintSolutionsT
{
 \foreach \x in {0,...,5}
 \draw[-latex,answercolor] (\x+0.1,-0.15)--(\x+0.9,-0.15);
 \node[answercolor] at (3,-0.8) {6};}
}
\end{question}

\begin{question}
For easier reference with the
\end{question}
\end{document}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Longer Embedded Solutions

Post by cgnieder »

LaTexLearner wrote:Onto the next type of longer answers, the ones with TikZ.
I'd prefer not to solve/answer different questions in the same thread. But perhaps it doesn't matter much in this case…
LaTexLearner wrote:How do I make exercise #2 have the same layout whether or not the solution is printed?
Your problem here is that the node below the line needs space when it is drawn. So in order to get the same space when the answers aren't printed you need to draw the node anyway but make the contents invisible. Here is one way:

Code: Select all

\documentclass[12pt,letterpaper]{article}

\usepackage{exsheets}
\colorlet{answercolor}{orange}

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

\usepackage{tikz}

\begin{document}

\noindent
Each horizontal line is broken into how many segments?

\begin{question}
\begin{tikzpicture}
  \draw (0,0)--(4,0);
  \foreach \x in {0,...,4}
    \draw (\x,-0.3)--(\x,0.3);
  \PrintSolutionsT{
    \foreach \x in {0,...,3}
      \draw[-latex, answercolor] (\x+0.1,-0.15)--(\x+0.9,-0.15);
  }
  \PrintSolutionsTF{\node[answercolor]}{\node[opacity=0]} at (2,-0.8) {4};
\end{tikzpicture}
\end{question}

\begin{question}
\begin{tikzpicture}
  \draw (0,0)--(6,0);
  \foreach \x in {0,...,6}
    \draw (\x,-0.3)--(\x,0.3);
  \PrintSolutionsT{
    \foreach \x in {0,...,5}
      \draw[-latex,answercolor] (\x+0.1,-0.15)--(\x+0.9,-0.15);
  }
  \PrintSolutionsTF{\node[answercolor]}{\node[opacity=0]} at (3,-0.8) {6};
\end{tikzpicture}
\end{question}

\begin{question}
For easier reference with the
\end{question}
\end{document}
Regards
site moderator & package author
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Longer Embedded Solutions

Post by LaTexLearner »

Awesome! That looks like a great solution.

Is it possible to put the \PrintSolutionsTF{}{} command further inside TikZ?

This did not work, though it would, I think, save me a lot of typing if there were many optional arguments or draw paths or whatever.

Code: Select all

\node[\PrintSolutionsTF{answercolor}{opacity=0}] at (3,-0.8) {6};
Your code worked, though.

Code: Select all

\PrintSolutionsTF{\node[answercolor]}{\node[opacity=0]} at (3,-0.8) {6};
The error I got was :
Package pgfkeys Error: I do not know the key '/tikz/opacity=0' and I am going to ignore it. Perhaps you misspelled it.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Longer Embedded Solutions

Post by cgnieder »

LaTexLearner wrote:

Code: Select all

\node[\PrintSolutionsTF{answercolor}{opacity=0}] at (3,-0.8) {6};
Funny: that is exactly what I tried first when writing my last answer. I am not sure why this doesn't work but my guess is that it is due to how TikZ/pgf reads the argument to \node. It obviously expands \PrintSolutionsTF but very likely after reading the options when it is too late. It doesn't see an option opacity with value 0 but sees an option opacity=0 (thinking the = and 0 were part of the option name). Such an option obviously isn't defined which explains the error. This is the reason why I put \PrintSolutionsTF outside of the path.

I can think of one or two ways to get the expansion of \PrintSolutionsTF in the option of \node but none would make the code easier or easier to read…
site moderator & package author
Post Reply