## LaTeX forum ⇒ Text Formatting ⇒ Making 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

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 %%%.

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
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?

cgnieder
Site Moderator
Posts: 1978
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:
LaTexLearner wrote:
%%% I did not understand the whole "true/false input stream" explanation in the exsheets manual.\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:
%%%  Defines a new length called "\answerspaces" and sets them to be 2cm long.\newlength\answerspace\setlength\answerspace{2cm}

yes (except for the trailing s)

LaTexLearner wrote:
%%% If math mode something happens and if not in math mode something else happens? \newcommand\answerline[1]{%   \ifmmode     \answerline@math{#1}%   \else     \answerline@text{#1}%   \fi}

yes

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

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:

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

\mathchoice   {\foo{\displaystyle}{x}}   {\foo{\textstyle}{x}}   {\foo{\scriptstyle}{x}}   {\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
\newcommand\answer@line@math[1]{\answerline@text{$#1$}}

the contents always would be printed as text style.

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

cgnieder
Site Moderator
Posts: 1978
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:
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:
\newcommand\answerline[2][\answerspace]{%   \begingroup     \ifdim#1=\answerspace     \else       \setlength\answerspace{#1}%     \fi     \ifmmode       \answerline@math{#2}%     \else       \answerline@text{#2}%     \fi   \endgroup}
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

LaTexLearner
Posts: 138
Joined: Tue Mar 10, 2015 11:06 am
Let's see if I can take ~one of these at a time.

cgnieder wrote:
LaTexLearner wrote:
%%% I did not understand the whole "true/false input stream" explanation in the exsheets manual.\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
cgnieder wrote:
LaTexLearner wrote:
%%% If math mode something happens and if not in math mode something else happens? \newcommand\answerline[1]{%   \ifmmode     \answerline@math{#1}%   \else     \answerline@text{#1}%   \fi}

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
cgnieder wrote:
LaTexLearner wrote:
%%% No idea what's happening from here until the printing options stuff.\newcommand*\answerline@text[1]{%   \underline{\makebox[\answerspace][c]{\answer{\color{answercolor}#1}}}%}

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?

cgnieder
Site Moderator
Posts: 1978
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:
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:
\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

cgnieder
Site Moderator
Posts: 1978
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:
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
\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

Johannes_B
Site Moderator
Posts: 3618
Joined: Thu Nov 01, 2012 4:08 pm
LaTexLearner wrote:Let's see if I can take ~one of these at a time.

cgnieder wrote:
LaTexLearner wrote:
%%% I did not understand the whole "true/false input stream" explanation in the exsheets manual.\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.