I just read that this formula was discovered by Ramanujan in the early 20th century.

Not really. He may have rediscovered it - does anyone know if he did, and where? - but it goes back to Lambert in 1768. The history is here:

https://en.wikipedia.org/wiki/Gauss%27s_continued_fraction

And in fact the appearance of π in this formula is a complete red herring! It's true with any number replacing π.

Let's dig into it a little more.

(1/n)

Gauss's continued fraction - Wikipedia

First, notice that

$$ \frac{e^z - 1}{e^z + 1} =
\frac{e^{z/2} - e^{-z/2}}{e^{z/2} + e^{-z/2}} = \tanh(z/2) $$

so what we're really talking about here is a continued fraction for the hyperbolic tangent function \(\tanh z\), with a factor of 1/2 thrown in to spice things up.

Let's get rid of the 1/2. Here is Lambert's formula for \(\tanh z\), in all its glorious simplicity:

(2/n)

Lambert's formula for \(\tanh z\) may have been rediscovered by Lagrange and Euler - I'm not sure. I know Gauss found a very general machine for creating such formulas in 1812.

This formula also gives a similar formula for \(\tan z\), since

$$ \tanh z = - i \tanh iz $$

See below!

But how can we prove this sort of formula?

(3/n)

In Lorentzen and Waadeland's book "Continued Fractions with Applications", they derive the continued fraction expansion for \(\tan z\). The basic strategy is this:

First find a differential equation for either this function or some related function \(y\). Then, use this to get formulas expressing

\(y\) in terms of \(y'\) and \(y'\),
\(y'\) in terms of \(y''\) and \(y'''\),

etc. Then, use these to solve for \(y\) and get a continued fraction! It's like black magic.

(4/n)

It's like black magic - because you're solving the differential equation for \(y\) not by integrating, but by expressing \(y\) in terms of higher and higher derivatives of \(y\), ad infinitum!

I don't have the energy to derive Lambert's formula for \(\tan z \) this way right now, but here:

https://golem.ph.utexas.edu/category/2020/09/chasing_the_tail_of_the_gaussi_1.html

you can see me use it to solve this (fairly easy) problem posed by Ramanujan:

(5/n, n = 5)

Chasing the Tail of the Gaussian (Part 2) | The n-Category Café

@johncarlosbaez Thank you very much. I find infinite sums somewhat easy, infinite products really hard, and continued fractions nothing less than black magic. This thread helps by motivating it, but I can only say I learnt it after trying a few.
@johncarlosbaez I love how you're still posting pictures of equations instead of just inlining the LaTeX -- hard to get over Twitter habits...