Text FormattingMaking Sense of Code

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
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:
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?
  3. What does \fi mean? I Googled it but could not make sense of it.

1. If you are currently in mathmode, process the argument as such, if not (text) process the argument as text.
2. They are defined in the intial post as well. They keep sure that math is typeset correctly (i.e. correct sizes).
3. \fi is if backwards and closes the conditional.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

Recommended reading 2024:

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

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

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

Making Sense of Code

Post by cgnieder »

LaTexLearner wrote:
  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.
We need a bit of background for TeX-conditionals. They all have the form
\if... \fi. The \fi terminates the conditional
(it is an if backwards if you will):

Code: Select all

\ifsomething
  do if condition is true
\fi
or in the longer form

Code: Select all

\ifsomething
  do if condition is true
\else
  do if condition is false
\fi
In our case:

Code: Select all

\ifmmode % check if TeX currently is in math mode
  \answerline@math{#1}% insert \answerline@math{#1} if condition is true
\else
  \answerline@text{#1}% insert \answerline@text{#1} if condition is false
\fi
Before the whole code starts you see the macro
\makeatletter. This macro makes the character @
a letter in TeX's eyes. This means it now behaves like a, b, c,… At the end of
the code there is \makeatother which makes it a non-letter
again. As long as @ is a letter it can be part of macro names.
This means \answerline@math is one macro with the name
“answerline@math”. The same is true for \answerline@text.
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:If the text after \answerline is in math mode then X happens.
Not quite: remember that we are talking about macros here. When (La)TeX encounters \answerline{foo} it is replaced by its definition but with foo in place of #1. We get

Code: Select all

\ifmmode
  \answerline@math{foo}%
\else
  \answerline@text{foo}%
\fi
Since my example is not in math mode \ifmmode evaluates to false so we end up (not directly but that's irrelevant here) with

Code: Select all

\answerline@text{foo}
Then this macro does expand to its definition again with foo in place of #1:

Code: Select all

\underline{\makebox[\answerspace][c]{\answer{\color{answercolor}foo}}}
and so on…

If we start with $\answerline{foo}$ the same procedure starts but this time \ifmmode evaluates to true and we end up with

Code: Select all

\answerline@math{foo}
site moderator & package author
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Making Sense of Code

Post by LaTexLearner »

Johannes_B wrote:...

2. Yes, a phantom reserves the space but does not typeset. something mysterious is going on, like a phantom you cannot see.
What do you mean by "reserves the space but does not typeset"? i.e. Will the worksheets have a blank space the size of the unprinted answer?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Making Sense of Code

Post by cgnieder »

LaTexLearner wrote:What do you mean by "reserves the space but does not typeset"? i.e. Will the worksheets have a blank space the size of the unprinted answer?
See for yourself:

Code: Select all

\documentclass{article}
\begin{document}
AAA \fbox{A} A

A\phantom{A}A \fbox{\phantom{A}} A
\end{document}
site moderator & package author
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Making Sense of Code

Post by LaTexLearner »

cgnieder wrote:...

In our case:

Code: Select all

\ifmmode % check if TeX currently is in math mode
  \answerline@math{#1}% insert \answerline@math{#1} if condition is true
\else
  \answerline@text{#1}% insert \answerline@text{#1} if condition is false
\fi
Before the whole code starts you see the macro
\makeatletter. This macro makes the character @
a letter in TeX's eyes. This means it now behaves like a, b, c,… At the end of
the code there is \makeatother which makes it a non-letter
again. As long as @ is a letter it can be part of macro names.
This means \answerline@math is one macro with the name
“answerline@math”. The same is true for \answerline@text.
Got it. This makes way more sense now. And I can see how \answerline@math and \answerline@text are defined in further detail below.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Making Sense of Code

Post by LaTexLearner »

LaTexLearner wrote:

Code: Select all

\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$}}
Let me see if I understand all this now:

The top section of code says that if the \answerline[1]{...} command is inside a math environment, to replace {...} with \answerline@math{#1}, a command which is below. If the \answerline[1] command is not in a math environment, then replace {...} with \answerline@text{#1}, also defined below.

The middle section of the code defines serves two purposes. First, it defines what a textual answer space should look like (underline with a text box of \answerspace size) and what the answer itself should look like. Question: How does this command specify where to "look" for the actual content of the answer?

The bottom section of the code defines what a math-mode answer should look like. I can see why the answer space/underline will still be there, but am otherwise a little lost for at least two reasons:
  1. [*EDITED] I understand that the @ symbol has been specified as a letter, but I still don't see the purpose of using the @ symbol as opposed to, say, a capital Z or A or hyphen or something like that.
  2. What does \mathpalette do? I Googled that one but the answers were way over my head.
Last edited by LaTexLearner on Thu Sep 10, 2015 4:48 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: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.
Let me rephrase that question as it didn't come out right.

What I meant about 3cm of space was 3cm of vertical space. Maybe this would have two horizontal rules all the way across or maybe not. Is that possible?

Also, what is the "adapted proposal" you mentioned?
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Re: Making Sense of Code

Post by LaTexLearner »

P.S. I really REALLY appreciate all the help everyone on these forums provides. Cgnieder, Johannes_B, Stefan_K, etc. thank you thank you thank you!

Right now, If there is some way for me to repay the favor and/or contribute to this community, please let me know. :P
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. I still don't understand that the @ symbol has been specified as a letter, but I still don't see the purpose of using the @ symbol as opposed to, say, a capital Z or A or hyphen or something like that.
  2. What does \mathpalette do? I Googled that one but the answers were way over my head.
It is a widely used practice to distinguish between “document commands”, i.e., macros for usage after \begin{document} and internal commands that better are not accessed directly. To make it harder to use internal macros the usual use it to put a @ in the name somewhere. If you look at the code of packages or in the LaTeX kernel you will see this a lot. (For understanding how this works the understanding of TeX's category codes is necessary, though)

The macro \mathpalette is defined like this:

Code: Select all

\newcommand*\mathpalette[2]{%
  \mathchoice
    {#1\displaystyle{#2}}%
    {#1\textstyle{#2}}%
    {#1\scriptstyle{#2}}%
    {#1\scriptscriptstyle{#2}}}
So if we have

Code: Select all

\newcommand*\foo[1]{\mathpalette\@foo{#1}}
\newcommand*\@foo[2]{\mbox{$#1#2$}}
and use it like this:

Code: Select all

\foo{bla bla}
we get

Code: Select all

\mathpalette\@foo{bla bla}
and then

Code: Select all

\mathchoice
  {\@foo\displaystyle{bla bla}}
  {\@foo\textstyle{bla bla}}
  {\@foo\scriptstyle{bla bla}}
  {\@foo\scriptscriptstyle{bla bla}}
The macro \mathchoice detects which of TeX's math modes (display, text, script or scriptscript) is active and either places its first, second, third or forth argument in the stream. (It is a little more complicated here but we can ignore that.) So if \foo{bla bla} is placed in displaystyle (an {equation}, say) the first argument \@foo\displaystyle{bla bla} is inserted. We then end up with

Code: Select all

\mbox{$\displaystyle bla bla$}
this ensures that inside the math of \@foo we always get the suiting font size for the current math surrounding.

Quintessence: the surrounding macro \foo works like the user expects it to even if it is a superscript $x^{\foo{bla}}$
site moderator & package author
Post Reply