44

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.

einpoklum
  • 12,311
asdaw
  • 441

7 Answers7

69
\documentclass{article}

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

\begin{document}

$\powertower{x}{100}$

\end{document}

enter image description here

David Carlisle
  • 757,742
35
\documentclass[12pt]{article}
\begin{document}

\[ x^{x^{x^{x^{x^{x^{x^{x^{x^x}}}}}}}} \]

\end{document}

enter image description here

29

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:

Quack

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.

Paulo Cereda
  • 44,220
28

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}

enter image description here

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.

enter image description here

egreg
  • 1,121,712
21

What? afraid of going beyond 255? not me!

epictower1

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

This reproduces exactly what TeX would have done (were it not for the limitation to 255 group levels) inclusive of a quite odd feature of TeX regarding extra horizontal space on the right.

update 2015. I forgot to say in 2013 what I was referring too: the horizontal whitespace extending to the the right of the power towers, on the side of the character glyphs. The image with the 600 x's does not show the resulting (big) extra horizontal space because the figure was clipped to the contained ink. The images below with shorter staircases display using fboxes the extra whitespace, look on right of the rightmost characters the distance to the fbox frame. You can also experiment with David Carlisle's macro inside an fbox (with \fboxsep=0pt or rather -0.4pt).

epictower2

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

epictowerx

epictower3 epictowersmallx epictowersmallX

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
    \advance\epic@extrax by -\epic@x
    \advance\epic@extrax by -\epic@x
    \epic@y=\ht\epic@two
    \advance\epic@y by -\ht\epic@one
    \epic@extray=\ht\epic@one
    \advance\epic@extray by -\epic@y
    \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@
    \advance\count@ \@ne
    \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}
  • 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. –  Nov 12 '13 at 14:23
11

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)

enter image description here

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.

Martin Thoma
  • 18,799
7

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
  \advance\powercount by -1
  \ifnum\powercount>1
  ^{\powertower{#1}{\the\powercount}}
  \fi
}
$\powertower{x}{100}$
\bye

enter image description here

Henri Menke
  • 109,596