# How to automatically determine the minimum number of filling dots with the following constraint?

I want to display a fraction

but with an overbrace saying number 7 appears n times and an underbrace saying number 8 appears n times.

I want the minimum number of dots to be automatically determined such that

• the most right character of the upper label, 7, 5, and the right end of the horizontal line are all aligned with the same vertical line.

• the most left character of the lower label, 8, 1, and the left end of the horizontal line are all aligned with the same vertical line.

# MWE

\documentclass[border=12pt,12pt]{standalone}
\usepackage{amsmath}
\begin{document}
$\displaystyle\frac{1\overbrace{7\dots 7}^{\text{number 7 appears$n$times}}}{\underbrace{8\dots 8}_{\text{number 8 appears$n$times}}5}$
\end{document}


How to automatically determine the minimum number of filling dots with the constraint given above?

# Edit

By a trial and error, I got the following but I want a smarter way.

\documentclass[border=12pt,12pt]{standalone}
\usepackage{amsmath}
\begin{document}
$\displaystyle\frac{1\overbrace{7..........................7}^{\text{number 7 appears$n$times}}}{\underbrace{8..........................8}_{\text{number 8 appears$n$times}}5}$
\end{document}


I'm not sure I understood the constraints but perhaps

\documentclass[border=12pt,12pt]{standalone}
\usepackage{amsmath}
\def\zz#1#2#3#4{%
\setbox0\hbox{$\scriptstyle#4$}%
#1{\hbox to \wd0{$#2$}}#3{#4}%
}
\begin{document}
$\displaystyle \frac {1\zz\overbrace{7\dotfill7}^{\text{number 7 appears$n$times}}} {\zz\underbrace{8\dotfill 8}_{\text{number 8 appears$n$times}}5}$
\end{document}


or as requested by jfbu with aligned dots (needs luatex)

\RequirePackage{luatex85}
\documentclass[border=12pt,12pt]{standalone}
\usepackage{amsmath}
\def\zz#1#2#3#4{%
\setbox0\hbox{$\scriptstyle#4$}%
#1{\hbox to \wd0{$\let\cleaders\gleaders#2$}}#3{#4}%
}
\begin{document}
$\displaystyle \frac {1\zz\overbrace{7\dotfill7}^{\text{number 7 appears$n$times}}} {\zz\underbrace{8\dotfill 8}_{\text{number 8 appears$n$times}}5}$
\end{document}


not sure it looks better

• wouldn't it be cooler with aligned dots for 8's and 7's ? (egreg did the math and he gets the dots almost aligned... isn't that challenging?) – user4686 Sep 30 '17 at 22:10
• @jfbu it's easy to align in luatex:-) see update – David Carlisle Sep 30 '17 at 23:38
• you are the boss :-) – user4686 Oct 1 '17 at 7:21

Here are some options to play with:

\documentclass{article}

\usepackage{mathtools}

\begin{document}

$\frac {1\overbrace{\makebox[5em]{7\dotfill 7}}^{\clap{\scriptsize\begin{tabular}{c} number 7 appears \\ n times \end{tabular}}}} {\underbrace{\makebox[5em]{8\dotfill 8}}_{\clap{\scriptsize\begin{tabular}{c} number 8 appears \\ n times \end{tabular}}}5}$

$\frac {1\overbrace{\makebox[5em]{7\dotfill 7}}^{\mathclap{\text{number 7 appears n times}}}} {\underbrace{\makebox[5em]{8\dotfill 8}}_{\mathclap{\text{number 8 appears n times}}}5}$

$\frac {1\makebox[5em]{7\dotfill 7}\mathllap{\overbrace{\rule{5em}{0pt}\vphantom{7}}^{\mathclap{\text{number 7 appears n times}}}}} {\mathrlap{\underbrace{\rule{5em}{0pt}\vphantom{8}}_{\mathclap{\text{number 8 appears n times}}}}\makebox[5em]{8\dotfill 8}5}$

\end{document}


You can adjust the width 5em I've chosen to space out the content more. The difference between options 2 and 3 is in the spacing beside the non-braced numbers.

I believe.

\documentclass{article}
\usepackage{amsmath}

\newlength{\ntimeslen}
\newcommand{\ntimes}[2]{%
\settowidth{\ntimeslen}{$\scriptstyle\text{number$#2$appears$n$times}$}%
#1{\makebox[\ntimeslen][s]{$#2\dotfill#2$}}%
\ifx#1\overbrace^\else_\fi
{\text{number $#2$ appears $n$ times}}%
}

\begin{document}

\begin{equation*}
\frac{1\!\ntimes{\overbrace}{7}}{\ntimes{\underbrace}{8}\!5}=
\frac{10^n+7\dfrac{10^n-1}{9\mathstrut}}{80\dfrac{10^n-1\mathstrut}{9}+5}=
\frac{16\cdot 10^n-7}{80\cdot 10^n-35}=\frac{1}{5}
\end{equation*}

\end{document}


• @jfbu Nice exercise… ;-) – egreg Sep 30 '17 at 22:13