LaTeX forum ⇒ Text FormattingMaking Sense of Code Topic is solved

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

Making Sense of Code

Postby LaTexLearner » Wed Sep 09, 2015 4:22 am

This is a follow-up to a thread on using the exsheets package to embed examples and solutions.

http://tex.stackexchange.com/questions/ ... orksheets/

I am trying to make sense of the first part of the code (non-TikZ stuff) and have preceded my comments in the code with %%%.

  1. %%% MY COMMENTS
  2.  
  3. \documentclass{article}
  4. \usepackage{exsheets,amsmath}
  5.  
  6. \makeatletter
  7. % a color for answers
  8. \colorlet{answercolor}{orange}
  9.  
  10. %%% I did not understand the whole "true/false input stream" explanation in the exsheets manual.
  11. \newcommand\answer[1]{\PrintSolutionsTF{#1}{\phantom{#1}}}
  12.  
  13. %%% Defines a new length called "\answerspaces" and sets them to be 2cm long.
  14. \newlength\answerspace
  15. \setlength\answerspace{2cm}
  16.  
  17. %%% If math mode something happens and if not in math mode something else happens?
  18. \newcommand\answerline[1]{%
  19. \ifmmode
  20. \answerline@math{#1}%
  21. \else
  22. \answerline@text{#1}%
  23. \fi
  24. }
  25.  
  26. %%% No idea what's happening from here until the printing options stuff.
  27. \newcommand*\answerline@text[1]{%
  28. \underline{\makebox[\answerspace][c]{\answer{\color{answercolor}#1}}}%
  29. }
  30. \newcommand\answerline@math[1]{\mathpalette\answer@line@math{#1}}
  31. \newcommand\answer@line@math[2]{\answerline@text{$#1#2$}}
  32. \makeatother
  33.  
  34.  
  35. % uncomment to get answers printed:
  36. % \SetupExSheets{solution/print=true}
  37.  
  38. \begin{document}
  39.  
  40. \section*{Find the unknown.}
  41. \begin{question}
  42. \( 3 + \answerline{1} = 4 \)
  43. \end{question}
  44. \begin{question}
  45. \( 4 = \answerline{14} -10 \)
  46. \end{question}
  47.  
  48. \end{document}
Last edited by LaTexLearner on Wed Sep 09, 2015 4:31 am, edited 1 time in total.

Tags:

LaTexLearner
Posts: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Wed Sep 09, 2015 4:28 am

Also, is there any way to set the answer space so that the default length is, say, 2cm, but there is an optional argument to make it longer?

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Wed Sep 09, 2015 10:12 am

LaTexLearner wrote:
  1. %%% I did not understand the whole "true/false input stream" explanation in the exsheets manual.
  2. \newcommand\answer[1]{\PrintSolutionsTF{#1}{\phantom{#1}}}


The input stream is everything TeX sees for typesetting. It's a term from the TeX book I believe. The idea is easy: when the option solution/print=true is active the first argument of \PrintSolutionsTF is printed otherwise the second.

LaTexLearner wrote:
  1. %%% Defines a new length called "\answerspaces" and sets them to be 2cm long.
  2. \newlength\answerspace
  3. \setlength\answerspace{2cm}

yes (except for the trailing s)

LaTexLearner wrote:
  1. %%% If math mode something happens and if not in math mode something else happens?
  2. \newcommand\answerline[1]{%
  3. \ifmmode
  4. \answerline@math{#1}%
  5. \else
  6. \answerline@text{#1}%
  7. \fi
  8. }

yes

LaTexLearner wrote:
  1. %%% No idea what's happening from here until the printing options stuff.
  2. \newcommand*\answerline@text[1]{%
  3. \underline{\makebox[\answerspace][c]{\answer{\color{answercolor}#1}}}%
  4. }

Something is underlined \underline{...}. Within a box ob length \answerspace is placed and the contents are centered: \makebox[\answerspace][c]{...}. This ensures we get a line with length \answerspace. In this box we put \answer{\color{answercolor}#1}. This is the macro we defined earlier that will only print something when solution/print=true but then will print it in orange.

LaTexLearner wrote:
  1. \newcommand\answerline@math[1]{\mathpalette\answer@line@math{#1}}
  2. \newcommand\answer@line@math[2]{\answerline@text{$#1#2$}}
  3. \makeatother


\mathpalette\foo{x} is the same as this:

  1. \mathchoice
  2. {\foo{\displaystyle}{x}}
  3. {\foo{\textstyle}{x}}
  4. {\foo{\scriptstyle}{x}}
  5. {\foo{\scriptscriptstyle}{x}}


With this it is possible to use \answerline in the different places in math (displayed equations, inline math, superscripts,…) and still get the correct size. If we did instead
  1. \newcommand\answer@line@math[1]{\answerline@text{$#1$}}

the contents always would be printed as text style.

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Wed Sep 09, 2015 10:18 am

LaTexLearner wrote:Also, is there any way to set the answer space so that the default length is, say, 2cm, but there is an optional argument to make it longer?


One possibility: change the definition of \answerline like this:
  1. \newcommand\answerline[2][\answerspace]{%
  2. \begingroup
  3. \ifdim#1=\answerspace
  4. \else
  5. \setlength\answerspace{#1}%
  6. \fi
  7. \ifmmode
  8. \answerline@math{#2}%
  9. \else
  10. \answerline@text{#2}%
  11. \fi
  12. \endgroup
  13. }
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

LaTexLearner
Posts: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Wed Sep 09, 2015 5:15 pm

Let's see if I can take ~one of these at a time.

cgnieder wrote:
LaTexLearner wrote:
  1. %%% I did not understand the whole "true/false input stream" explanation in the exsheets manual.
  2. \newcommand\answer[1]{\PrintSolutionsTF{#1}{\phantom{#1}}}


The input stream is everything TeX sees for typesetting. It's a term from the TeX book I believe. The idea is easy: when the option solution/print=true is active the first argument of \PrintSolutionsTF is printed otherwise the second.


I see.

  1. Where is the the option solution/print=true set and how do you set it?
  2. Does \phantom{#1} mean "blank"?

LaTexLearner
Posts: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Wed Sep 09, 2015 5:26 pm

cgnieder wrote:
LaTexLearner wrote:
  1. %%% If math mode something happens and if not in math mode something else happens?
  2. \newcommand\answerline[1]{%
  3. \ifmmode
  4. \answerline@math{#1}%
  5. \else
  6. \answerline@text{#1}%
  7. \fi
  8. }

yes



  1. If the text after \answerline is in math mode then X happens. What is X? And if if it is not in math mode, then Y happens. What's Y?
  2. What do the @math and @text actually mean? (I'll postpone asking questions about the last section of the code until I understand this part.)
  3. What does \fi mean? I Googled it but could not make sense of it.
Last edited by LaTexLearner on Wed Sep 09, 2015 6:18 pm, edited 1 time in total.

LaTexLearner
Posts: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Wed Sep 09, 2015 5:42 pm

cgnieder wrote:
LaTexLearner wrote:
  1. %%% No idea what's happening from here until the printing options stuff.
  2. \newcommand*\answerline@text[1]{%
  3. \underline{\makebox[\answerspace][c]{\answer{\color{answercolor}#1}}}%
  4. }

Something is underlined \underline{...}. Within a box ob length \answerspace is placed and the contents are centered: \makebox[\answerspace][c]{...}. This ensures we get a line with length \answerspace. In this box we put \answer{\color{answercolor}#1}. This is the macro we defined earlier that will only print something when solution/print=true but then will print it in orange.


Got it. Good explanation!

My only question: What if I didn't want the answer space to be a 2cm line? E.g. What if I wanted, say, 3cm of blank space on the workbook pages for students to write in and for my solutions to appear in those 3cm?

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Wed Sep 09, 2015 6:08 pm

LaTexLearner wrote:
  1. Where is the the option solution/print=true set and how do you set it?
  2. Does \phantom{#1} mean "blank"?


  1. Look at the code in your first post on the top:
    1. \SetupExSheets{solution/print=true}

    It is an option of exsheets and described in the manual.
  2. \phantom{...} typesets the space that ... would need.
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Wed Sep 09, 2015 6:10 pm

LaTexLearner wrote:My only question: What if I didn't want the answer space to be a 2cm line? E.g. What if I wanted, say, 3cm of blank space on the workbook pages for students to write in and for my solutions to appear in those 3cm?


Either change the default in
  1. \setlength\answerspace{2cm}

to another value or use the adapted proposal where \answerline got an option for other lengths than the default.
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

User avatar
Johannes_B
Site Moderator
Posts: 3584
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Wed Sep 09, 2015 6:12 pm

LaTexLearner wrote:Let's see if I can take ~one of these at a time.

cgnieder wrote:
LaTexLearner wrote:
  1. %%% I did not understand the whole "true/false input stream" explanation in the exsheets manual.
  2. \newcommand\answer[1]{\PrintSolutionsTF{#1}{\phantom{#1}}}


The input stream is everything TeX sees for typesetting. It's a term from the TeX book I believe. The idea is easy: when the option solution/print=true is active the first argument of \PrintSolutionsTF is printed otherwise the second.


I see.

  1. Where is the the option solution/print=true set and how do you set it?
  2. Does \phantom{#1} mean "blank"?



1. Look at your initial post, it is commented out, there is even an explanation.

2. Yes, a phantom reserves the space but does not typeset. something mysterious is going on, like a phantom you cannot see.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 7 guests