# How do I typeset a tenfold powering (a tower-of-powers) with LaTeX?

How do I write x^x^x^x^x^x^x^x^x^x?

My LaTeX distribution doesn't wanna eat it, it stops swallowing stuff at the double power x^x^x. What I'm aiming at is essentially building a telescope out of X's stacked on top of each other. Don't ask, that's my sense of humour.

-
$x^{\scriptscriptstyle x^{x^{x^{x^{x^{x^{x^{x^{x}}}}}}}}}$ –  egreg Nov 11 '13 at 21:30
I'm surprised noone has said $$x\uparrow\uparrow10$$ just because. –  You Nov 11 '13 at 23:53
@You It would in fact only be $x \uparrow 10$; two \uparrows and you would have x \uparrow 10 exponentiation steps. (But this is almost entirely beside the point. :)) –  Sean Allred Nov 12 '13 at 0:59
According to mathworld.wolfram.com/PowerTower.html, you would need two arrows. –  Cephalopod Nov 12 '13 at 8:44
LaTeX vs. Viagra. –  Nicholas Hamilton Nov 12 '13 at 13:27

\documentclass{article}

\def\powertower#1#2{#1\ifnum#2>1 ^{\powertower{#1}{\numexpr#2-1\relax}}\fi}

\begin{document}

$\powertower{x}{100}$

\end{document}


-
This is the TeX version of Stairway to heaven. :) Amazing! –  Paulo Cereda Nov 11 '13 at 23:45
@PauloCereda: maybe so, but that x at the bottom left is obviously under a lot of stress, and ready to fall over at any moment. –  wasteofspace Nov 11 '13 at 23:50
@wasteofspace: I think David is inspired because we were singing The London bridge is falling down in the chatroom yesterday. :) –  Paulo Cereda Nov 11 '13 at 23:53
@PauloCereda a minor sin compared to your earlier one of referencing a picture of tower bridge to accompany the lyrics –  David Carlisle Nov 12 '13 at 0:11
You could have called this macro \towerofpower in honor of a certain soul/funk band. :-) –  Mico Nov 12 '13 at 14:23
\documentclass[12pt]{article}
\begin{document}

$x^{x^{x^{x^{x^{x^{x^{x^{x^x}}}}}}}}$

\end{document}


-
I'm curious—how hard would it be to generalize this as a \PowerTower{x}{10} (power tower reference) –  Sean Allred Nov 11 '13 at 21:54
@Sean: done. :) –  Paulo Cereda Nov 11 '13 at 23:25
@PauloCereda Yay!! –  Sean Allred Nov 12 '13 at 1:00

I dedicate this code to Sean. :)

Long live expl3! :)

\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\cs_new:Npn \paulo_epicrecursion:nn #1 #2 {
#1^{ \int_compare:nTF { #2 > 1 } {
\paulo_epicrecursion:nn { #1 } { \int_eval:n { #2 - 1 } }
} {
#1
} }
}

\NewDocumentCommand{ \powertower } { m m } {
\paulo_epicrecursion:nn { #1 } { #2 }
}

\ExplSyntaxOff

\begin{document}

$\powertower{x}{10}$

\end{document}


The output:

Update: egreg brilliantly noted that the above code generates a long list of nested \int_eval:n calls. The optimization is presented as follows:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\cs_new:Npn \paulo_epicrecursion:nn #1 #2
{
#1^{
\int_compare:nTF { #2 > 1 }
{ \paulo_epicrecursion:nx { #1 } { \int_eval:n { #2 - 1 } } }
{ #1 }
}
}

\NewDocumentCommand{ \powertower } { m m }
{
\paulo_epicrecursion:nn { #1 } { #2 }
}
\cs_generate_variant:Nn \paulo_epicrecursion:nn { nx }

\ExplSyntaxOff

\begin{document}

$\powertower{x}{10}$

\end{document}


The trick here is the generated variant. As egreg explained, the first call doesn't need the full expansion, but the recursive ones do.

-
+1 for \powertower and epicrecursion. (Edit: Ah, I saw Sean's comment only after writing this one.) –  Torbjørn T. Nov 11 '13 at 23:26
@TorbjørnT.: :) –  Paulo Cereda Nov 11 '13 at 23:39
superb, absolutely superb :) –  cmhughes Nov 11 '13 at 23:40
The command name is wrong: it should be \paulo_epicrecursion:nn because it has two arguments. –  egreg Nov 11 '13 at 23:49
@egreg: oopsie, I forgot to update it when I added the second argument. Thanks, fixed. :) –  Paulo Cereda Nov 11 '13 at 23:50

Here's a shorter version with LaTeX3 functions:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{ \powertower } { m m }
{
#1
\prg_replicate:nn { #2 - 1 } { ^\c_group_begin_token #1 }
\prg_replicate:nn { #2 - 1 } { \c_group_end_token }
}

\ExplSyntaxOff

\begin{document}

$\powertower{x}{10}$

\end{document}


Limitation: the tower maximum size is 254 because of the maximum nested group level is 255. This limitation on the nested groups is also in the other solutions, of course. Here's a picture of $\powertower{x}{253}$ (I was trying with standalone that adds a level of grouping and forgot to change).

With LuaLaTeX the limit is pushed to 499; at 500 the semantic nest size is overflown.

-
As always, brilliant! :) –  Paulo Cereda Nov 12 '13 at 0:20
Very elegant; it's the simpler solutions that seem to be harder to think of, sometimes. :) –  Sean Allred Nov 12 '13 at 0:56

What? afraid of going beyond 255? not me!

This was obtained as $\epictower{x}{600}$.

I must point out that this reproduces exactly what TeX would have done (were it not for the limitation to 255 group levels) inclusive of what appears as a quite odd feature regarding extra horizontal spaces; these spaces may be seen from using \fboxes, let me demonstrate by these examples, which show boxes.

In the next picture, on top left what is produced by \epictower, on the right and bottom the original thing. To demonstrate horizontal and vertical placement is correct (the font size is set to 60pt) (below also in standard size)

And here is the code:

\documentclass{article}
% use this for 600 x's!
% \usepackage [paperheight=65cm,paperwidth=100cm]{geometry}
\usepackage {geometry}
\pagestyle{empty}

\makeatletter
\newdimen \epic@x
\newdimen \epic@y
\newdimen \epic@extrax
\newdimen \epic@extray
\newbox \epic@one
\newbox \epic@two
\newcommand{\epictower}[2]{\ifcase #2\relax
1\or #1\or {#1}^{#1}\or {#1}^{{#1}^{#1}}\else \epic@tower {#1}{#2}\fi }
\def\epic@tower #1#2{%
\sbox\epic@one{\m@th $\scriptscriptstyle #1$}%
\sbox\epic@two{\m@th $\scriptscriptstyle {#1}^{#1}$}%
\epic@x=\wd\epic@one
\epic@extrax=\wd\epic@two
\epic@y=\ht\epic@two
\epic@extray=\ht\epic@one
\toks@ \expandafter{\the\numexpr #2-3}%
{#1}^{{#1}^{%
\setlength{\unitlength}{1sp}%
\begin{picture}({\numexpr (#2-2)*\epic@x+(#2-3)*\epic@extrax\relax},%
{\numexpr (#2-2)*\epic@y+\epic@extray\relax})
\count@\z@
\loop
\put (\numexpr \count@ * \epic@x\relax,%
\numexpr \count@ * \epic@y\relax){\copy\epic@one}%
\ifnum\the\toks@>\count@
\repeat
\end{picture}}}}
\makeatother

\begin{document}\thispagestyle{empty}

\fboxsep-.4pt
\fontsize{60}{60}

\newcommand{\testtexpowers}[1]{%
\setbox 1 \hbox{$\scriptscriptstyle #1$}%
\setbox 2 \hbox{$\scriptscriptstyle #1^{\fbox{\copy1}}$}%
\setbox 3 \hbox{$\scriptscriptstyle #1^{#1^{\fbox{\copy1}}}$}%
\setbox 4 \hbox{$\scriptscriptstyle #1^{#1^{#1^{\fbox{\copy1}}}}$}%
\setbox 5 \hbox{$\scriptscriptstyle #1^{#1^{#1^{#1^{\fbox{\copy1}}}}}$}%
\setbox 6 \hbox{$\scriptscriptstyle #1^{#1^{#1^{#1^{#1^{\fbox{\copy1}}}}}}$}%
\setbox 7 \hbox{$\scriptscriptstyle #1^{#1^{#1^{#1^{#1^{#1^{\fbox{\copy1}}}}}}}$}%
\setbox 8 \hbox{$\scriptscriptstyle #1^{#1^{#1^{#1^{#1^{#1^{#1^{\fbox{\copy1}}}}}}}}$}%
\setbox 9 \hbox{$\scriptscriptstyle #1^{#1^{#1^{#1^{#1^{#1^{#1^{#1^{\fbox{\copy1}}}}}}}}}$}%
\leavevmode\rlap{\rlap{\rlap{\rlap{\rlap{\rlap{\rlap{\rlap{\fbox{\box1}}\fbox{\box2}}\fbox{\box3}}\fbox{\box4}}\fbox{\box5}}\fbox{\box6}}\fbox{\box7}}\fbox{\box8}}\fbox{\box9}}

\testtexpowers{x}

\testtexpowers{X}

\testtexpowers{xX}

\clearpage

\fbox{$\epictower {x}{1}$}\fbox{$x$}\hrule\fbox{$x$}
\hrule
\fbox{$\epictower {x}{2}$}\fbox{$x^x$}\hrule\fbox{$x^x$}
\hrule
\fbox{$\epictower {x}{3}$}\fbox{$x^{x^x}$}\hrule\fbox{$x^{x^x}$}
\hrule
\fbox{$\epictower {x}{4}$}\fbox{$x^{x^{x^x}}$}\hrule\fbox{$x^{x^{x^x}}$}
\hrule
\fbox{$\epictower {x}{5}$}\fbox{$x^{x^{x^{x^x}}}$}\hrule\fbox{$x^{x^{x^{x^x}}}$}
\hrule
\fbox{$\epictower {x}{6}$}\fbox{$x^{x^{x^{x^{x^x}}}}$}\hrule\fbox{$x^{x^{x^{x^{x^x}}}}$}
\hrule

\clearpage

\fbox{$\epictower {X}{1}$}\fbox{$X$}\hrule\fbox{$X$}
\hrule
\fbox{$\epictower {X}{2}$}\fbox{$X^X$}\hrule\fbox{$X^X$}
\hrule
\fbox{$\epictower {X}{3}$}\fbox{$X^{X^X}$}\hrule\fbox{$X^{X^X}$}
\hrule
\fbox{$\epictower {X}{4}$}\fbox{$X^{X^{X^X}}$}\hrule\fbox{$X^{X^{X^X}}$}
\hrule
\fbox{$\epictower {X}{5}$}\fbox{$X^{X^{X^{X^X}}}$}\hrule\fbox{$X^{X^{X^{X^X}}}$}
\hrule
\fbox{$\epictower {X}{6}$}\fbox{$X^{X^{X^{X^{X^X}}}}$}\hrule\fbox{$X^{X^{X^{X^{X^X}}}}$}
\hrule

\end{document}

% comment-out the preceding, sets page dimensions in geometry
% only limited by TeX's \maxdimen !

$\epictower{x}{600}$

\end{document}

-
the x and X images with rules were obtained after \fontsize{60pt}{60pt} and TeX used this same size in all styles text, script, and scriptscript. The smaller examples are in normal size, and there one does have x then a smaller one then a smaller one with the size stabilizing from then one. If one downloads the first image and zoom on it one sees this is what's happening there too. It is not possible to produce this by standard input, for the reason explained by egreg about the maximal grouping level. –  jfbu Nov 12 '13 at 14:20
it is easy to modify \epictower to print the exponents along an arbitrary algebraically parametrized curve, for nice special effects. Size of the bounding box will need extra coding if the curve zig-zags; or one may just set the bounding box to the space taken by x^{x^x} for simplicity. –  jfbu Nov 12 '13 at 14:23
the image with the 600 x's does not show the (big) extra horizontal space on the right because the figure was clipped by dvipng to the contained ink. In my humble opinion these horizontal spaces when one does many iterated uses of ^ are almost a bug of TeX. They become obvious if use David Carlisle's macro inside an fbox (with fboxsep=0pt or rather -.4pt). –  jfbu Nov 12 '13 at 19:01

As more than three powers get very unpleasant to read, I would really recommend using another notation. As already proposed in the comments (hence a community wiki answer), you can represent power towers by the arrow notation:

(x\uparrow\uparrow10)


You might want to watch Numberphiles YouTube video about Grahams number to understand this notation.

This notation is much easier to read. In fact, you can read it at all in contrast to the big tower of powers notation.

-
The recursion: this notation was introduced by Someone that is already a legend...=). –  Andrea L. Nov 14 '13 at 12:50

This is a version of David's answer, but without fancy etex for those being stuck with Knuth TeX.

\newcount\powercount
\def\powertower#1#2{
#1\powercount=#2
$\powertower{x}{100}$