# How do I write a recurring decimal in LaTeX?

Title says it all. Since I am a newbie, I don't know how to do this.

Any help?

-
Do you have an example of what you want to write? Because it is unclear what you mean by "recurring decimal" – yo' Oct 4 '12 at 11:41
@tohecz like 0.9 with a dot on top of the 9. – think123 Oct 4 '12 at 11:42
Is this what you want: 0.\dot{9}? – Alexander Oct 4 '12 at 11:45
and how do you write 17/99 then? This way? 0.\dot{1}\dot{7}. We used to write 0.\overline{17}, recently I use 0.(17)^\omega and my friends 0.(17)^{\mathbb{N}}. This just shows that there're many ways how to write it and you have to specify which exactly you want to typeset. – yo' Oct 4 '12 at 11:50

If you're thinking of using a horizontal bar over a recurring group of decimals, you could use the \overline command:

\documentclass{article}
\begin{document}
$\frac{1}{7}=0.\overline{142857}$
\end{document}


-
For people using this with siunitx, see also tex.stackexchange.com/a/97329/90237 – arekolek Jun 9 at 8:41

There are at least four representations; here is a way to produce all of them, take your pick (the macro names can of course be modified). I strongly suggest to use a special macro name, even if you decide to use the overline, so you can change your mind later and choose another realization.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn

%% Dots on the first and last digit
\NewDocumentCommand{\periodfl}{m}
{
\repdec_initial_final_dots:n { #1 }
}

\seq_new:N \l__repdec_digits_seq
\tl_new:N \l__repdec_first_tl
\tl_new:N \l__repdec_last_tl

\cs_new_protected:Npn \repdec_initial_final_dots:n #1
{
\seq_set_split:Nnn \l__repdec_digits_seq {} { #1 }
\seq_pop_left:NN \l__repdec_digits_seq \l__repdec_first_tl
\seq_pop_right:NN \l__repdec_digits_seq \l__repdec_last_tl
\quark_if_no_value:VF \l__repdec_first_tl { \dot{\l__repdec_first_tl} }
\seq_use:Nnnn \l__repdec_digits_seq {}{}{}
\quark_if_no_value:VF \l__repdec_last_tl { \dot{\l__repdec_last_tl} }
}
\cs_generate_variant:Nn \quark_if_no_value:nF { V }

%% Dots on all digits
\NewDocumentCommand{\periodalldots}{m}
{
\repdec_initial_all_dots:n { #1 }
}

\cs_new_protected:Npn \repdec_initial_all_dots:n #1
{
\tl_map_inline:nn { #1 } { \dot{##1} }
}

%% Bar over period
\NewDocumentCommand{\periodbar}{m}
{
\overline{ #1 }
}

%% Parentheses around period
\NewDocumentCommand{\periodparens}{m}
{
(#1)
}

%% Dot on unique digit, bar on several digits
\NewDocumentCommand{\periodmixed}{m}
{
\repdec_mixed:n { #1 }
}
\cs_new_protected:Npn \repdec_mixed:n #1
{
\int_case:nnn { \tl_count:n { #1 } }
{
{ 0 } { }
{ 1 } { \dot{#1} }
}
{
\overline{#1}
}
}

\ExplSyntaxOff

\begin{document}
$1.2\periodfl{3}$ --- $1.2\periodfl{34}$ --- $1.2\periodfl{345}$ ---
$1.2\periodfl{3456}$ --- $1.2\periodfl{34567}$

\medskip
$1.2\periodalldots{3}$ --- $1.2\periodalldots{34}$ --- $1.2\periodalldots{345}$ ---
$1.2\periodalldots{3456}$ --- $1.2\periodalldots{34567}$

\medskip
$1.2\periodbar{3}$ --- $1.2\periodbar{34}$ --- $1.2\periodbar{345}$ ---
$1.2\periodbar{3456}$ --- $1.2\periodbar{34567}$

\medskip
$1.2\periodparens{3}$ --- $1.2\periodparens{34}$ --- $1.2\periodparens{345}$ ---
$1.2\periodparens{3456}$ --- $1.2\periodparens{34567}$

\medskip
$1.2\periodmixed{3}$ --- $1.2\periodmixed{34}$ --- $1.2\periodmixed{345}$ ---
$1.2\periodmixed{3456}$ --- $1.2\periodmixed{34567}$

\end{document}


For \periodbar and \periodparens the overhead of xparse is not necessary and they can be realized with the standard

\newcommand{\periodbar}[1]{\overline{#1}}
\newcommand{\periodparens}[1]{(#1)}


A different definition of \repdec_initial_final_dots:n can be as follows

\int_new:N \l__repdec_digits_int
\int_new:N \l__repdec_count_int
\cs_new_protected:Npn \repdec_initial_final_dots:n #1
{
\int_set:Nn \l__repdec_digits_int { \tl_count:n { #1 } }
\int_zero:N \l__repdec_count_int
\tl_map_inline:nn { #1 } { \__repdec_add_dot:n { ##1 } }
}
{
\int_incr:N \l__repdec_count_int
\int_case:nnn { \l__repdec_count_int }
{
{ 1 } { \dot{#1} }
{ \l__repdec_digits_int } { \dot{#1} }
}
{
#1
}
}


However, the sequence method seems more general.

Just for completeness, here is a possible approach for this macro in the traditional LaTeX programming:

\makeatletter
\DeclareRobustCommand{\periodfl}[1]{\@periodflold#1\@nil\relax}
\def\@periodflold#1#2{%
\ifx#2\relax
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\@periodflold@i#1#2}%
}
\def\@periodflold@i#1#2{%
\dot{#1}%
\ifx#2\@nil
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\@periodflold@ii#2}%
}
\def\@periodflold@ii#1#2{%
\ifx#2\@nil
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\dot{#1}}{#1\@periodflold@ii#2}%
}
\makeatother

-
OMG the first two look so confusing! – yo' Oct 4 '12 at 12:21
@TRiG I don't find that mixing dots and bars is sound, but I've added \periodmixed. – egreg Oct 4 '12 at 14:35
There is even another one, which is quite easy, but it takes more space: 1.233... -- 1.23434... -- 1.2345345... -- 1.234563456... -- etc. – Egon Oct 4 '12 at 17:01
@Egon How would you write 1.23453(45)? As "1.234534545..."? – Random832 Oct 4 '12 at 19:29
@Hans-PeterE.Kristiansen Where repeating decimals are used it's quite improbable to need also uncertainty measurements. Mathematics is full of contradictory notations, also in one and the same book, sometimes. One should always pay attention to notation, and so your comment is certainly to be taken into consideration. – egreg Oct 5 '12 at 13:33