6

How can I lower the last term of a continued fraction when it follows a diagonal row of dots?

10

Here's how I'm inputting continued fractions. The dots must be input with a \cfrac prefix, so \cfracdots for the effect shown below, \cfraccdots for centered dots, \cfracldots for low dots or \cfracddots for standard diagonal dots.

\documentclass{article}
\usepackage{xparse,amsmath}

\ExplSyntaxOn
\NewDocumentCommand{\cfracdots}{ }
  {
   \rule{0pt}{1.5\baselineskip}
   \raisebox{.5\baselineskip}{\enspace$\ddots$\enspace}
  }
\NewDocumentCommand{\cfraccdots}{}{\cdots}
\NewDocumentCommand{\cfracddots}{}{\ddots}
\NewDocumentCommand{\cfracldots}{}{\ldots}

\NewDocumentCommand{\xcontfrac}{ s O{c} >{\SplitArgument{1}{;}}m }
  { 
   \IfBooleanTF{#1}
     { \cfrac_inline:nn #3 }
     { \cfrac_map:nnn { #2 } #3 }
  }

\cs_new:Npn \cfrac_inline:nn #1 #2
  {
   \IfNoValueTF { #2 }
     {
      \tl_use:N \c_cfrac_message_tl
      \xcontfrac*{;#1}
     }
     {
      \group_begin:
      \cs_set_eq:NN \cfracdots \dots
      [\, \tl_if_empty:nTF { #1 } { 0 } { #1 } ; #2 \,]
      \group_end:
     }
  }

\tl_const:Nn \c_cfrac_lbrace_tl { \if_true:  { \else: } \fi: }
\tl_const:Nn \c_cfrac_rbrace_tl { \if_false: { \else: } \fi: }
\tl_const:Nn \c_cfrac_strut_tl { \vrule width 0pt depth .3\baselineskip }
\tl_new:N \l_cfrac_left_tl
\tl_new:N \l_cfrac_right_tl
\msg_new:nnn { cfrac } { wrong-syntax }
  {
   Wrong~syntax~for~\token_to_str:N \xcontfrac,~
   assuming~0~in~the~integer~part,~on~line~\msg_line_number:.
  }

\cs_new:Npn \cfrac_map:nnn #1 #2 #3
  {
   \tl_clear:N \l_cfrac_left_tl \tl_clear:N \l_cfrac_right_tl
   \IfNoValueTF { #3 }
     { 
      \msg_warning:nn { cfrac } { wrong-syntax }
      \xcontfrac[#1]{;#2}
     }
     {
      \tl_if_empty:nTF { #2 }
        { \cfrac_map_aux:nn { #1 } { \exp_not:N \use_none:n , #3 } }
        { \cfrac_map_aux:nn { #1 } { #2 , #3 } }
     }
  }
\cs_new:Npn \cfrac_map_aux:nn #1 #2
  {
   \clist_map_inline:nn { #2 }
     {
      \tl_put_right:Nn \l_cfrac_left_tl { \cfrac_begin:nn { #1 } { ##1 } }
      \tl_put_right:Nn \l_cfrac_right_tl { \exp_not:N \c_cfrac_rbrace_tl }
     }
   \tl_set:Nx \l_cfrac_left_tl
     { \l_cfrac_left_tl \c_cfrac_strut_tl \l_cfrac_right_tl }
   \tl_set:Nx \l_cfrac_left_tl { \l_cfrac_left_tl }
   \exp_after:wN \use_none:nnnnnn \l_cfrac_left_tl
  }
\cs_new:Npn \cfrac_begin:nn #1 #2
  {
   \exp_not:n
     { + \exp_not:N \cfrac[#1] { 1 } \c_cfrac_lbrace_tl \exp_not:N \mathstrut #2 }
  }
\ExplSyntaxOff

\begin{document}
\[
x=\xcontfrac*{;a_1,a_2,\cfracdots,a_n}=
\xcontfrac{;a_1,a_2,\cfracdots,a_n}
\]
\end{document}

As you can see, the same input can be used to typeset the continued fraction in in-line form or expanded, just adding the *. This allows to avoid counting braces and indenting the input.

The integer part can be omitted if zero; it won't be typeset in the expanded form (but the semicolon is mandatory).

enter image description here

  • 1
    Great answer, although I'd prefer using the msg module for complaining about input – Joseph Wright Feb 4 '12 at 17:02
  • 5
    Of course you do. I'll try and be a good boy. – egreg Feb 4 '12 at 17:05
4

I'm answering my own question here, in the hope that other people might find this useful. The question is similar, but not identical, to the one here, which asks how to precede the last term in a continued fraction with a diagonal ellipsis. I'm interested in lowering the final term, which comes after the ellipsis, very slightly, so that it looks as though the ellipsis is pointing towards it.

To do this, simply use raisebox:

\begin{equation}
x=
\cfrac{1}{a_1+
 \cfrac{1}{a_2+
  \cfrac{1}{\ddots \raisebox{-2mm}{$+\cfrac{1}{a_n}$}}}}
\end{equation}
  • 2
    Just better put the measure in ex instead of mm, to make it more scale-invariant. – yo' Feb 4 '12 at 15:25
  • 4
    Better to go ${}+\cfrac on that last line, otherwise the + gets prefix spacing intended for +1 rather than infix spacing intended for 1 + 2 – David Carlisle Feb 4 '12 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.