Generate TeX to typeset Sierpinski Triangle Fractal

30

1

Challenge

Write code that outputs TeX (LaTeX) math-equation code (given below) that will typeset Sierpinski Triangle Fractal of 5 levels. Shortest code wins.

Details

TeX (and friends like LaTeX, etc.) is a sophisticated typesetting system. It can render arbitrary nested complex expressions for mathematical formulas. Coincidentally this "nested complex" is also descriptive of fractals. The following is rendered with MathJaX

$${{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}^{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}_{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}}$$

by the following plain-text math-equation code consisting of nested super- and sub-scripts:

{{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}^{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}_{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}}

Note this is just a 5-level nesting. You do not need to generate $...$ or $$...$$ or other markup required to start/end a math equation in TeX & Co. You can preview generated TeX in many online editors, for instance: http://www.hostmath.com but you can find many others too. This question was inspired by a discussion with friends.

Update

There is a similar question but it much more general and will produce different solutions. I wanted to see really kolmogorov-complexity for a very fixed simple code that in one system (TeX) is completely explicit while in another compressed. This also address the n instead of 5 levels comment.

Vitaliy Kaurov

Posted 2017-09-20T12:38:02.413

Reputation: 1 561

Why exactly 5 levels? It would be interesting (and probably easier) to implement arbitrary number of levels and to take level number as a parameter. – None – 2017-09-20T13:00:29.850

@ThePirateBay yes, sure, but i did not want to generalize, see my update. – Vitaliy Kaurov – 2017-09-20T13:18:50.623

2Hello; I closed your question as a duplicate because I believe that answers can be too trivially modified from the other question to answer this question. However, I like the idea and I think it looks pretty cool! :) – HyperNeutrino – 2017-09-20T13:20:25.220

2For what it's worth, I reopened this question as I do not see the code as being trivially modifiable to translate from one to the other. – AdmBorkBork – 2017-09-20T13:36:46.993

4That's far too quick to be accepting a solution! – Shaggy – 2017-09-20T13:38:10.497

@HyperNeutrino was the question open again? – Vitaliy Kaurov – 2017-09-20T13:44:56.487

@Shaggy I removed acceptance for now because there is some confusion with opening and closing the question. Sorry. – Vitaliy Kaurov – 2017-09-20T13:45:41.237

@VitaliyKaurov Yes, AdmBorkBork hammered it: For what it's worth, I reopened this question as I do not see the code as being trivially modifiable to translate from one to the other.. – Mr. Xcoder – 2017-09-20T13:45:54.780

@AdmBorkBork thank you for your consideration – Vitaliy Kaurov – 2017-09-20T13:46:59.070

3

Relevant Meta: https://codegolf.meta.stackexchange.com/q/5056/32352

– Sanchises – 2017-09-20T14:09:54.947

2

When I saw this challenge, this answer came to my mind... https://codegolf.stackexchange.com/a/6830/67961 and... it was yours

– J42161217 – 2017-09-20T21:26:11.237

@Jenny_mathy yeah guilty as charged ;-) – Vitaliy Kaurov – 2017-09-20T21:31:58.373

Is the challenge just to generate that TeX string, or to generate any TeX string which will have the same output? – TRiG – 2017-09-21T10:46:35.080

@TRiG "any TeX string which will have the same output" – Vitaliy Kaurov – 2017-09-21T10:49:02.803

Answers

8

SOGL V0.12, 16 12 bytes

 x5{"{^_}”;∑

Try it Here!

Port of Erik The Outgolfer's Python 2 answer

dzaima

Posted 2017-09-20T12:38:02.413

Reputation: 19 048

21

Python 2, 32 bytes

exec"print'x'"+".join('{^_}')"*5

Try it online!

Erik the Outgolfer

Posted 2017-09-20T12:38:02.413

Reputation: 38 134

14

plain TeX, 29 bytes

\def~#1x{{#1x_#1x^#1x}}~~~~~x

That outputs what others have output. But if we need the code to be compilable it would be 6 bytes more

\def~#1x{{#1x_#1x^#1x}}$~~~~~x$\bye

Explanation

~ is an active character in TeX, so we can give it a (new) definition.

\def~#1x{{#1x_#1x^#1x}} defines ~ as a macro, so that when TeX sees ~, it does the following:

  • Read everything up to the next x, and call that #1 (pattern-matching).
  • Replace the whole thing with {#1x_#1x^#1x}

For example, ~ABCx would get replaced with {ABCx_ABCx^ABCx}.

When ~~~~~x is used, #1 is ~~~~, so the whole thing gets replaced with {~~~~x_~~~~x^~~~~x}. And so on.

Once we have the long string, we can print it out to terminal with \message (and ending with a \bye so TeX stops), so \message{~~~~~x}\bye. Or typeset the resulting expression (as a mathematical formula), by surrounding it in $s : so $~~~~~x$\bye.

Manuel

Posted 2017-09-20T12:38:02.413

Reputation: 291

Sorry if there's anything wrong, first answer here. – Manuel – 2017-09-21T11:04:03.827

For a big n (rather than 5) it could be more efficient to create a macro that outputs a list of n tildes ~ rather than writing ~~~~~. Plus it would look better if the whole expression is typeset under \scriptscriptstyle. – Manuel – 2017-09-21T11:08:54.130

Nice trick… can you add an explanation, or mind if I add one? This illustrates a nice feature of pattern-matching in TeX macros, which is not a feature common to many languages (that I know of). – ShreevatsaR – 2017-09-21T20:05:25.303

I will add it, but feel free to edit. – Manuel – 2017-09-21T20:17:09.663

Oops, didn't see your comment… added a very similar explanation; feel free to reject. +1 for the nice answer! – ShreevatsaR – 2017-09-21T20:30:07.797

Almost word for word :) – Manuel – 2017-09-21T20:33:29.390

4

PowerShell,  44  35 bytes

"'x'"+"-replace'x','{x^x_x}'"*5|iex

Try it online!

Uses string multiplication to repeatedly -replace xes with the sub- and super-scripts, then output.

Saved 9 bytes thanks to Joey.

AdmBorkBork

Posted 2017-09-20T12:38:02.413

Reputation: 41 581

"'x'"+"-replace'x','{x^x_x}'"*5|iex is a bit easier, no? – Joey – 2017-09-20T15:48:51.217

@Joey Oh, that's a clever way of doing it. Thanks! – AdmBorkBork – 2017-09-20T16:47:26.457

4

05AB1E, 17 bytes

'x5F'x¡"{x^x_x}"ý

Try it online!

Explanation

'x                  # push "x"
  5F                # 5 times do
    'x¡             # split on "x"
       "{x^x_x}"ý   # join on "{x^x_x}"

Other programs at the same byte-count include

"{x^x_x}"©4F'x¡®ý
'x5F'x"{x^x_x}".:

Emigna

Posted 2017-09-20T12:38:02.413

Reputation: 50 798

I feel like "{x^x_x}" can be reduced ._. – Magic Octopus Urn – 2017-09-21T16:37:29.547

2

JavaScript (ES6), 45 42 37 bytes

f=n=>n>4?'x':[...'{^_}'].join(f(-~n))

Edit: Saved 3 2 bytes thanks to @Arnauld. Specifying 5 still costs me 2 bytes; this 41 40 35-byte version takes a parameter instead:

f=n=>n?[...'{^_}'].join(f(n-1)):'x'

Neil

Posted 2017-09-20T12:38:02.413

Reputation: 95 035

2

Japt, 21 20 18 bytes

5Æ="\{^_}"¬qUª'xÃÌ

Test it


Explanation

5Æ             Ã

Generate an array of length 5 and map over it.

"\{^_}"¬

Split a string to an array of characters

qUª'x

Rejoin (q) to a string using the current value of U or (ª) "x".

=

Assign the result of that to U.

Ì

Get the last element in the array.


Alternatives, 18 bytes

Same as above but reducing the array after it's been created.

5o r@"\{^_}"¬qX}'x

Test it

The recursive option.

>4©'xª"\{^_}"¬qßUÄ

Test it

Shaggy

Posted 2017-09-20T12:38:02.413

Reputation: 24 623

2

MATL, 21 20 bytes

'x'XJ5:"J'{x^x_x}'Zt

-1 byte thanks to Giuseppe

Try it online!

Cinaski

Posted 2017-09-20T12:38:02.413

Reputation: 1 588

120 bytes with 'x'XJ5:"J'{x^x_x}'Zt or even 5pc5:"5pc'{x^x_x}'Zt – Giuseppe – 2017-09-20T14:18:45.603

@Giuseppe Thanks! – Cinaski – 2017-09-20T14:23:22.000

2

05AB1E, 13 bytes

'x5F"{^_}"Ssý

Try it online!

Port of my Python 2 answer.

Erik the Outgolfer

Posted 2017-09-20T12:38:02.413

Reputation: 38 134

2

Jelly, 12 bytes

”x“{^_}”j$5¡

Try it online!

Port of my Python 2 answer.

Erik the Outgolfer

Posted 2017-09-20T12:38:02.413

Reputation: 38 134

1

Java (OpenJDK 8), 179 167 bytes

@Neil port

interface Y{static void main(String[]a){System.out.println(t.apply(1));}java.util.function.Function<Integer,String>t=N->N>0?Y.t.apply(N-1).replace("x","{x^x_x}"):"x";}

Try it online!

Roberto Graham

Posted 2017-09-20T12:38:02.413

Reputation: 1 305

I think you it is shorter to write t as a real functio instead of a lambda – Roman Gräf – 2017-09-20T15:03:51.923

If you use an entire program, t.apply(1) should be t.apply(new Integer(a[0])) instead. But why not just post a method? String t(int n){return n>0?t(n-1).replace("x","{x^x_x}"):"x";} And if the requirement of the challenge would be a full program (which isn't), using a Java 7 recursive method would be shorter than a lambda: interface Y{static void main(String[]a){System.out.print(t(new Integer(a[0])));}static String t(int n){return n>0?t(n-1).replace("x","{x^x_x}"):"x";}}

– Kevin Cruijssen – 2017-09-20T15:03:57.573

0

Wolfram Language (Mathematica) - 40 characters

Summarizing 3 best answers here:

40 bytes:

Nest["{"<>#<>"_"<>#<>"^"<>#<>"}"&,"x",5]

41 bytes:

Nest[StringReplace["x"->"{x^x_x}"],"x",5]

44 bytes:

Last@SubstitutionSystem["x"->"{x^x_x}","x",5]

Vitaliy Kaurov

Posted 2017-09-20T12:38:02.413

Reputation: 1 561

3It is not recommended to answer your own challenge, without leaving others a couple of days first. – Mr. Xcoder – 2017-09-20T12:59:47.810

1Does your first code snippet not require 41 bytes? – Jonathan Frech – 2017-09-20T13:05:03.053

@Mr.Xcoder apologies, the editor form suggested as an option to post my own answer. Should I delete my answer? – Vitaliy Kaurov – 2017-09-20T13:07:52.200

@VitaliyKaurov I think you should, other users will probably receive this badly. – Mr. Xcoder – 2017-09-20T13:08:31.170

0

Pyth, 17 16 13 bytes

jF+\x*5]"{^_}

Try it online!

Python 3 translation:
from functools import*;print(reduce(lambda x,y:x.join(y),["x"]+5*["{^_}"]))

hakr14

Posted 2017-09-20T12:38:02.413

Reputation: 1 295

0

C (gcc), 82 bytes

O(o){o--?_(125,O(o,_(95,O(o,_(94,O(o,_(123))))))):_(120);}main(){O(5);}

Try it online!

Jonathan Frech

Posted 2017-09-20T12:38:02.413

Reputation: 6 681