Math & Sciencegetting spacing right in complicated continued fraction

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
dsquared
Posts: 4
Joined: Wed Jul 08, 2009 6:37 am

getting spacing right in complicated continued fraction

Post by dsquared »

I have a somewhat unusual continued fraction and the vertical spacing is just not working properly. Here's my fraction:

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\cfrac{1}
  {1 - \cfrac{z^{t+1}}
    {\left(1 - \cfrac{z^{t+2}}
      {\left(1 - \cfrac{z^{t+3}}
        {(1 - \cdots)^{t}}\right)^{t}}\right)^{t}}}
\]
\end{document}
The biggest problem is with the fraction whose numerator is z^{t+2}. How can I reduce the vertical space it uses? I know it's using the height of the denominator and centering it on the line -- but the fraction with numerator z^{t+1} didn't do that. I've tried various combinations of \dfrac and \cfrac, as well as \smash and \vphantom, and nothing quite works properly. Any suggestions?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

getting spacing right in complicated continued fraction

Post by localghost »

You could do manual adjustment of the delimiters.

Code: Select all

\[
\cfrac{1}
  {1 - \cfrac{z^{t+1}}
    {\Biggl(1 - \cfrac{z^{t+2}}
      {\biggl(1 - \cfrac{z^{t+3}}
        {(1 - \cdots)^{t}}\biggr)^{t}}\Biggr)^{t}}}
\]
Details can be found in the amsmath manual.


Best regards and welcome to the board
Thorsten
dsquared
Posts: 4
Joined: Wed Jul 08, 2009 6:37 am

Re: getting spacing right in complicated continued fraction

Post by dsquared »

Huh, I didn't think it was the delimiters that were making the fractions so big. That's a very good start, I'll keep experimenting and see if I can improve it. Thanks for the help.
dsquared
Posts: 4
Joined: Wed Jul 08, 2009 6:37 am

getting spacing right in complicated continued fraction

Post by dsquared »

Thanks to localghost's suggestion, I have something that looks pretty good now, so I'll share in case anyone else finds this: I used a savebox. I took the z^{t+2} fraction part and separated it out:

Code: Select all

\newsavebox{\foo}
\savebox{\foo}{$1 - \dfrac{z^{t+2}}
      {\bigg(1 - \dfrac{z^{t+3}}
         {(1 - \cdots)^{t}}\bigg)^{t}}$}
You can then use the box with \usebox{\foo}, but the box remembers that the first 1 sits on the line and the fraction part extends far below it -- this is probably the cause of the original problem. So we raise it up with a \raisebox. The final code, including some struts to further fiddle with the spacing, is this:

Code: Select all

\newsavebox{\foo}
\savebox{\foo}{\raisebox{.75em}{$1 - \dfrac{z^{t+2}}
      {\bigg(1 - \dfrac{z^{t+3} \rule{0pt}{1.15em}}
         {(1 - \cdots)^{t}}\bigg)^{t}}
$}}

\[
\cfrac{1}
  {1 - \cfrac{z^{t+1}\rule{0pt}{1.05em}}
    {\left( \usebox{\foo} \right)^{t}}}
\]
I just changed the distances for the raisebox and the struts until things looked right. The spacing is still not entirely what I'd like, and I dislike doing hackish things instead of actually understanding exactly what's going on, but this looks good and will work for the paper I'm writing.
dsquared
Posts: 4
Joined: Wed Jul 08, 2009 6:37 am

getting spacing right in complicated continued fraction

Post by dsquared »

I said I hate doing hackish things instead of understanding what's happening, so of course eventually I did go figure it out...

The problem is that TeX, when placing things, has an idea of a baseline, and things have a height (how far do I extend above the baseline?) and a depth (how far do I extend below the baseline?).

The source of my problem is that continued fractions have a large depth and pretty small height, but the vertical center of \left( and \right) brackets is always about .25em above the baseline -- so the brackets have to be very tall to accomodate the large depth of the fraction, and hence extend way too far above the fraction. (I'm probably misunderstanding some of the details here.)

So, I wrote a macro that saves the fraction in a box and raises the box up so that it will have the same vertical center as the \left( and \right) and look nice. We need to make the vertical center .25em above the baseline, so what we want to do is this:

Code: Select all

\raisebox{(\depthof{#1} - \heightof{#1})/2 + .25em}{#1}
But TeX won't do that, so we have to create a savebox, set some lengths, and do the arithmetic step-by-step. Here's my solution:

Code: Select all

\newsavebox{\fracbox}
\newlength{\fracraise}
\newlength{\myht}
\newlength{\mydp}

\newcommand{\mylr}[1]{%
\sbox{\fracbox}{\ensuremath{#1}}%
\settoheight{\myht}{\usebox{\fracbox}}%
\settodepth{\mydp}{\usebox{\fracbox}}%
\setlength{\fracraise}{\mydp}%
\addtolength{\fracraise}{-\myht}%
\setlength{\fracraise}{.5\fracraise}%
\addtolength{\fracraise}{.25em}%
\left(\raisebox{\fracraise}{\usebox{\fracbox}}\right)}
You can see the results here, in equations (13), (18) and (27).
Post Reply