Text FormattingMaking Sense of Code

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

Making Sense of Code

Post by LaTexLearner »

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

Code: Select all

%%%  MY COMMENTS

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

\makeatletter
% a color for answers
\colorlet{answercolor}{orange}

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

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

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

%%% No idea what's happening from here until the printing options stuff.
\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}

\end{document}
Last edited by LaTexLearner on Wed Sep 09, 2015 4:31 am, edited 1 time in total.

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

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

Making Sense of Code

Post by LaTexLearner »

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: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Making Sense of Code

Post by cgnieder »

LaTexLearner wrote:

Code: Select all

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

Code: Select all

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

Code: Select all

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

Code: Select all

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

Code: Select all

\newcommand\answerline@math[1]{\mathpalette\answer@line@math{#1}}
\newcommand\answer@line@math[2]{\answerline@text{$#1#2$}}
\makeatother
\mathpalette\foo{x} is the same as this:

Code: Select all

\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

Code: Select all

\newcommand\answer@line@math[1]{\answerline@text{$#1$}}
the contents always would be printed as text style.

Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Making Sense of Code

Post by cgnieder »

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:

Code: Select all

\newcommand\answerline[2][\answerspace]{%
  \begingroup
    \ifdim#1=\answerspace
    \else
      \setlength\answerspace{#1}%
    \fi
    \ifmmode
      \answerline@math{#2}%
    \else
      \answerline@text{#2}%
    \fi
  \endgroup
}
site moderator & package author
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Making Sense of Code

Post by LaTexLearner »

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

Code: Select all

%%% 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: 139
Joined: Tue Mar 10, 2015 11:06 am

Making Sense of Code

Post by LaTexLearner »

cgnieder wrote:
LaTexLearner wrote:

Code: Select all

%%% 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: 139
Joined: Tue Mar 10, 2015 11:06 am

Making Sense of Code

Post by LaTexLearner »

cgnieder wrote:
LaTexLearner wrote:

Code: Select all

%%% 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?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Making Sense of Code

Post by cgnieder »

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:

    Code: Select all

    \SetupExSheets{solution/print=true}
    It is an option of exsheets and described in the manual.
  2. \phantom{...} typesets the space that ... would need.
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Making Sense of Code

Post by cgnieder »

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

Code: Select all

\setlength\answerspace{2cm}
to another value or use the adapted proposal where \answerline got an option for other lengths than the default.
site moderator & package author
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Making Sense of Code

Post by Johannes_B »

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

Code: Select all

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