Latex challenge: expand (a+b)^n

In summary, the conversation discusses various methods of creating whitespace in LaTeX and TeX, with a specific focus on aligning mathematical expressions using the + sign. The conversation also delves into the use of glue, categories of mathematical symbols, and commands for creating horizontal boxes.
  • #1
Evgeny.Makarov
Gold Member
MHB
2,436
4
There is a famous picture.

View attachment 2963

Could you write in LaTeX something similar to this:

View attachment 2964

without using explicit commands that insert whitespace such as \, \: \; \enskip \quad \hskip \mskip \hspace \kern and \mkern?
 

Attachments

  • pic11337.jpg
    pic11337.jpg
    19.6 KB · Views: 118
  • expand.png
    expand.png
    967 bytes · Views: 103
Physics news on Phys.org
  • #2
$$ (a+b)^n $$
$$ (a\phantom{z}+\phantom{z}b)^n$$
$$ (a\phantom{zzz}+\phantom{zzz}b)^n$$
$$ (a\phantom{zzzzz}+\phantom{zzzzz}b)^n$$
 
  • #3
Ackbach, the command you used can also be considered as whitespace-inserting, but nice try.
 
  • #4
\(\displaystyle \begin{array}{c} & & & & & (a+b)^n & & & & & \\ & & & & (a & + & b)^n & & & & \\ & & & (a & & + & & b)^n & & & \\ & & (a & & & + & & & b)^n & & \\ & (a & & & & + & & & & b)^n & \\ (a & & & & & + & & & & & b)^n \end{array}\)
 
  • #5
First of all, I must apologize for not checking if my solution works on this site. Apparently, it does not, but it is nevertheless normal LaTeX.

Mark, this is also nice, and formally it satisfies the description. One drawback is that "$(a$" on one line cannot vertically overlap with "$(a$" on the following line since they belong to different columns.

There is a trick in TeX (which uses one of the core features) that allows doing this very easily and flexibly so that the width of the line can be arbitrary.

By the way, I don't think one needs to put LaTeX output inside a spoiler since the code is not visible without additional manipulation (right-clicking and selecting "Show Math As" or responding with a quote).
 
  • #6
This is harder than it looks! I think that a good solution ought to have these two features: (1) the + signs must be exactly aligned vertically, (2) there should be exactly the same amount of white space on each side of the +. The reason this is hard to achieve is that the expression $(a+b)^n$ is not symmetric about the + (the exponent ^n makes the right side wider than the left side).

I can't find any way to do this using commands that MathJax will accept. Using TeXShop (which automatically centres displayed equations), the input:
Latex Code:

\def\expand#1{$$\begin{array}{r@{\extracolsep{#1pt}}c@{\extracolsep{#1pt}}l} (a&+&b)^n \end{array}$$}
\expand {2}
\expand {17}
\expand {32}
\expand
{47}produces this output:

But that contains the explicit whitespace command \extracolsep{#1pt} in the expand macro.
 

Attachments

  • expand.png
    expand.png
    1.6 KB · Views: 86
  • #7
\(\displaystyle (a+b)^n\)
\(\displaystyle \begin{array}{c}(a & + & b)^n \end{array}\)
\(\displaystyle \begin{array}{c}(a && + && b)^n \end{array}\)
\(\displaystyle \begin{array}{c}(a &&& + &&& b)^n \end{array}\)
\(\displaystyle \begin{array}{c}(a &&&& + &&&& b)^n \end{array}\)​

This is not done entirely with $\LaTeX$ though...:D
 
  • #8
Small improvement that is completely $\LaTeX$:

\(\displaystyle \begin{array}{rcl}
\begin{array}{}(a\end{array} & +& \begin{array}{}b)^n\end{array} \\
\begin{array}{}(a&\end{array} & +& \begin{array}{}&b)^n\end{array} \\
\begin{array}{}(a&&\end{array} & +& \begin{array}{}&&b)^n\end{array} \\
\begin{array}{}(a&&&\end{array} & +& \begin{array}{}&&&b)^n\end{array} \\
\end{array}\)
 
  • #9
Fun with HTML...

[expand][/expand]

edit: I have removed the temporary custom BBCode I used to animate the expansion.
 
  • #10
MarkFL said:
[latexs]\begin{array}{c}(a & + & b)^n \end{array}[/latexs]
It is strange that this code works in MathJax. In regular LaTeX it produces an error due to the presence of &. Indeed, "a & + & b" is supposed to make three columns, while the preamble of the array environment declares only one.

I did not intend for the lines to be aligned on +. That would be nice, but I don't know how to do it using my idea. I used glue, which is a really fundamental part of TeX. In fact, it is more like a spring because it can both shrink and expand. When specifying whitespace, one can provide not only its natural width, but also the ability of the space to shrink and stretch. For example,
[latexs]
1cm plus .2cm minus .1cm
[/latexs]
denotes a glue whose natural width is 1 cm and which can shrink by at most .1 cm. In contrast, it can stretch arbitrarily much, but stretching beyond .2 cm will be assigned a high score of "badness". This score is used in deciding how to split a paragraph into lines, and the goal is to minimize badness in the whole paragraph.

In more detail, if a box has total stretchability (the sum of "plus" components of all spaces) of $x$, but it has to be stretched by $y$, then the glue set ratio is defined as $r=y/x$. Then each space that has the plus component $x_i$ is stretched by $rx_i$. The badness of $r$ is defined as $\min(100r^3,10000)$. For example, if a space is stretched twice its plus component, the badness is 800.

Mathematical glue is measured in mu, which is $1/18$ of em (approximately the width of M or an em-dash). For example, thin space \, medium space \: and thick space \; equal 3, 4 and 5 mu, respectively. more precisely, medium space is
[latexs]
4mu plus 2mu minus 4mu.
[/latexs]
Each symbol in math has one of several categories (ordinary symbol, binary operator, etc.), and spacing between symbols is based by their categories. In particular, TeX inserts medium space between $a$ (ordinary symbol) and $+$ (binary operator). In my LaTeX setup, 1 em equals 10 points, so the stretchability of the medium space is $(2/18)\cdot10=10/9$ of a point.

The LaTeX commands for creating horizontal boxes (LR-boxes in LaTeX terminology or hboxes in plain TeX terminology) are \mbox and \makebox (Wikibooks). The latter command takes two optional arguments: width and position. Thus, it is possible to specify a larger than natural width and make the box stretch. This also requires giving s (spread) as the position argument. For example,
[latexs]
\makebox[3cm]{$(a+b)^n$}
[/latexs]
produces a box 3 cm wide.

Plain TeX analogs are
[latexs]
\hbox to dimen { contents of box }
\hbox spread dimen { contents of box }
[/latexs]
The first specifies the total box width, while the second specifies by how much the box must be stretched. For example,
[latexs]
\hbox spread 10pt {$(a+b)^n$}
[/latexs]
adds an additional em to the natural width. There are two medium spaces (no space is inserted before the exponent), so the glue set ratio is $\dfrac{10}{2(10/9)}=\dfrac{9}{2}=4.5$, so each space becomes $\dfrac{9}{2}\cdot\dfrac{10}{9}=5$ pt. This is confirmed by the trace that one can print in the log file.
Code:
\hbox(7.5+2.5)x45.02081, glue set 4.5001
.\mathon
.\OT1/cmr/m/n/10 (
.\OML/cmm/m/it/10 a
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 +
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OML/cmm/m/it/10 b
.\OT1/cmr/m/n/10 )
.\hbox(3.01389+0.0)x5.44333, shifted -3.62892
..\OML/cmm/m/it/7 n
.\mathoff
It shows that the glue set ratio is 4.5, and each of the two \medmuskip's is multiplied by this value. The badness of this box is $100(4.5)^3\approx9113$, which is close to the maximum 10,000. For some reason, LaTeX wrote
Code:
Underfull \hbox (badness 10000) detected at line 14
probably because badness calculation is approximate.

All in all, my code was
[latexs]
\begin{tabular}{c}
\makebox{$(a+b)^n$}\\[2ex]
\makebox[2cm]{$(a+b)^n$}\\[2ex]
\makebox[3cm]{$(a+b)^n$}\\[2ex]
\makebox[4cm]{$(a+b)^n$}
\end{tabular}
[/latexs]
 
  • #11
Evgeny.Makarov said:
All in all, my code was
[latexs]
\begin{tabular}{c}
\makebox{$(a+b)^n$}\\[2ex]
\makebox[2cm]{$(a+b)^n$}\\[2ex]
\makebox[3cm]{$(a+b)^n$}\\[2ex]
\makebox[4cm]{$(a+b)^n$}
\end{tabular}
[/latexs]


Your code does not parse in MiKTeX due to badness 10000.
It cannot parse in a math environment like MathJax.
What does it take to parse it? :confused:

Edit: Hmm, after some twiddling with MiKTeX (that I had just installed), it did parse.
Not sure why though.
 
  • #12
I like Serena said:
Your code does not parse in MiKTeX due to badness 10000.
But an underfull hbox is a warning, not an error. It does not stop compilation. Overfulls and underfulls happen all the time (though the latter are more rare). What output do you get from LaTeX?

I like Serena said:
It cannot parse in a math environment like MathJax.
Yes, because MathJax does not recognize the \makebox command. It does accept \hbox{...}, but not "\hbox to 1cm {...}".
 
  • #13
Evgeny.Makarov said:
But an underfull hbox is a warning, not an error. It does not stop compilation. Overfulls and underfulls happen all the time (though the latter are more rare). What output do you get from LaTeX?

I didn't get any output from MiKTeX.
Not even from a simple text line.

That is, until I parsed an old document that used a couple of packages.
That triggered MiKTeX to download and install a series of packages.
After that everything worked.
So it appears it was only some sort of configuration issue.

I still get the warnings though. :)

Yes, because MathJax does not recognize the \makebox command. It does accept \hbox{...}, but not "\hbox to 1cm {...}".

MathJax also does not accept the tabular environment.
I guess we need an array instead.
 

FAQ: Latex challenge: expand (a+b)^n

What is the purpose of the "Latex challenge: expand (a+b)^n"?

The purpose of this challenge is to practice expanding binomial expressions using the binomial theorem and learn how to use LaTeX to format mathematical expressions.

What is the binomial theorem?

The binomial theorem is a mathematical formula that states the expansion of a binomial expression raised to a positive integer power. It is written as (x+y)^n = x^n + nx^(n-1)y + n(n-1)/2! x^(n-2)y^2 + ... + y^n, where n is a positive integer and x and y are any real or complex numbers.

How do you expand (a+b)^n using the binomial theorem?

To expand (a+b)^n, you can use the binomial theorem formula: (a+b)^n = a^n + na^(n-1)b + n(n-1)/2! a^(n-2)b^2 + ... + b^n. Simply plug in the value of n and the values of a and b, and simplify the expression.

What is LaTeX and how is it used in this challenge?

LaTeX is a typesetting system commonly used in academia to format mathematical equations, symbols, and formulas. In this challenge, LaTeX is used to format the binomial expression (a+b)^n and its expansion in a clear and organized way.

Are there any tips for successfully completing this challenge?

Yes, here are a few tips: make sure to follow the correct order of operations, remember to simplify any terms that can be simplified, and double check your final answer to ensure it is fully expanded and simplified. It may also be helpful to practice with simpler binomial expressions before attempting more complex ones.

Similar threads

Replies
4
Views
3K
Replies
2
Views
2K
Replies
2
Views
5K
Replies
7
Views
2K
Replies
4
Views
4K
Replies
5
Views
3K
Replies
3
Views
881
Replies
15
Views
25K
Replies
9
Views
15K
Back
Top