4

This post in this website proposes the following code.

\documentclass{article}

\usepackage{amsmath,array}

\begin{document}

[ \setlength{\arraycolsep}{0pt} \newcommand{\md}[2][1.45]{\mathbin{\raisebox{-#1ex}[0pt][0pt]{$\displaystyle#2$}}} \md{A + B ={}} \begin{array}[t]{ *{13}{>{\displaystyle{\mathstrut}}c<{{}}} } 1 \ \cline{1-1} \md{7} & \md{+} & 1 \ \cline{3-3} && \md{15} & \md{+} & 1 \ \cline{5-5} &&&& 7 & + & \cfrac{1}{4} \end{array} ]

\end{document}

Could it be possible to obtain a similar output but with a better vertical alignment as if standard fractions have been used? See the red arrow in the picture below.

enter image description here

projetmbc
  • 13,315

3 Answers3

6

I suggest to use fractions-in-fractions concept. I used \smash in the nominators in order to avoid the mentioned spaces.

$$
  \everymath={\displaystyle}
  A + B = {1\over 7 \rlap{${}+ {\smash 1\over 15
                    \rlap{${}+ {\smash 1\over 7 
                    \rlap{${}+ {\smash 1\over 5}$}}$}}$}} 
$$
wipet
  • 74,238
4

Simple user level syntax and correct spacing.

\documentclass{article}
\usepackage{mathtools}

\usepackage{lipsum}

\ExplSyntaxOn

\NewDocumentCommand{\altcfrac}{m} { \projetmbc_altcfrac:n { #1 } }

\tl_new:N \l__projetmbc_altcfrac_tl \tl_new:N \l__projetmbc_altcfrac_last_tl \seq_new:N \l__projetmbc_altcfrac_seq

\cs_new_protected:Nn __projetmbc_frac:nn { \dfrac{#1}{#2} } \cs_new_protected:Nn __projetmbc_smash:n { \mathrlap{\smash[t]{#1}} } \cs_new:Nn __projetmbc_lbrace: { \if_true: { \else: } \fi: } \cs_new:Nn __projetmbc_rbrace: { \if_false: { \else: } \fi: }

\cs_new_protected:Nn \projetmbc_altcfrac:n { \tl_clear:N \l__projetmbc_altcfrac_tl \seq_set_from_clist:Nn \l__projetmbc_altcfrac_seq { #1 } \seq_pop_right:NN \l__projetmbc_altcfrac_seq \l__projetmbc_altcfrac_last_tl \seq_map_inline:Nn \l__projetmbc_altcfrac_seq { \tl_put_right:Nn \l__projetmbc_altcfrac_tl { __projetmbc_frac:nn { 1 } __projetmbc_lbrace: ##1 __projetmbc_smash:n __projetmbc_lbrace: {} + } } \tl_put_right:Nn \l__projetmbc_altcfrac_tl { __projetmbc_frac:nn { 1 } __projetmbc_lbrace: } \tl_put_right:NV \l__projetmbc_altcfrac_tl \l__projetmbc_altcfrac_last_tl \prg_replicate:nn { (\seq_count:N \l__projetmbc_altcfrac_seq + 1) * 2 - 1 } { \tl_put_right:Nn \l__projetmbc_altcfrac_tl { __projetmbc_rbrace: } } \tl_set:Nx \l__projetmbc_altcfrac_tl { \l__projetmbc_altcfrac_tl } \tl_use:N \l__projetmbc_altcfrac_tl \hphantom{\seq_use:Nn \l__projetmbc_altcfrac_seq { + } + \l__projetmbc_altcfrac_last_tl } } \ExplSyntaxOff

\begin{document}

\lipsum[1][1-4] [ A=3+\altcfrac{7,15,7,4} ] \lipsum[1][1-4]

\end{document}

The idea is to build the whole series of

\dfrac{1}{7\mathrlap{\smash[t]{{}+\dfrac{1}{15...

step by step. The braces are hidden in macros that expand to them, but we expand only after the whole structure has been built.

Next we add a phantoms to take care of the width and it's sufficient to sum the denominators.

enter image description here

egreg
  • 1,121,712
2

enter image description here

adding some negative space...

\documentclass{article}

\usepackage{amsmath,array}

\begin{document}

[ \setlength{\arraycolsep}{0pt} \newcommand{\md}[2][1.45]{\mathbin{\raisebox{-#1ex}[0pt][0pt]{$\displaystyle#2$}}} \md{A + B ={}} \begin{array}[t]{ *{13}{>{\displaystyle{\mathstrut}}c<{{}}} } 1 \ \cline{1-1}\noalign{\vspace{-.7ex}} \md{7} & \md{+} & 1 \ \cline{3-3}\noalign{\vspace{-.7ex}} && \md{15} & \md{+} & 1 \ \cline{5-5}\noalign{\vspace{-.7ex}} &&&& 7 & + & \cfrac{1}{4} \end{array} ]

\end{document}

David Carlisle
  • 757,742
  • wipet seems to have found a better solution. – projetmbc Nov 08 '22 at 19:33
  • 3
    @projetmbc well yes (but \frac not \over) but I thought the spirit of the question was to adjust the provided tabular based approach rather than simply setting nested fractions by nesting a fraction construct. But I can survive if you give wipet the tick:-) – David Carlisle Nov 08 '22 at 19:39
  • Sorry! :-) You are a kind of wizard to me, so I totally respect you. :-) – projetmbc Nov 08 '22 at 20:30