# How do I write a repeating 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'
Commented Oct 4, 2012 at 11:41
• @tohecz like 0.9 with a dot on top of the 9. Commented Oct 4, 2012 at 11:42
• Is this what you want: 0.\dot{9}? Commented Oct 4, 2012 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'
Commented Oct 4, 2012 at 11:50
• Commented Aug 12, 2019 at 13:40

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}


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}

\ExplSyntaxOn

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

\cs_new_protected:Npn \repdec_initial_final_dots:n #1
{
\tl_if_single:nTF { #1 }
{ \dot{#1} } % just one digit
{
\dot{\tl_range:nnn { #1 } { 1 } { 1 } } % first digit
\tl_range:nnn { #1 } { 2 } { -2 } % middle digits
\dot{\tl_range:nnn { #1 } { -1 } { -1 } } % last digit
}
}

%% 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:nnF { \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}


• OMG the first two look so confusing!
– yo'
Commented Oct 4, 2012 at 12:21
• @TRiG I don't find that mixing dots and bars is sound, but I've added \periodmixed. Commented Oct 4, 2012 at 14:35
• @Random832: I would advise against using parenthesis, as they are used for uncertainty. en.wikipedia.org/wiki/Uncertainty#Measurements Commented Oct 5, 2012 at 13: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. Commented Oct 5, 2012 at 13:33
• @egreg You removed the part of your answer prefixed by "Just for completeness, here is...". I returned back this part because without it readers cannot know to what I responded in my answer and my answer then misses the point. Commented May 3 at 5:01

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

\def\afterfi#1#2\fi{\fi#1}

\def\periodfl#1{\pflA#1.}
\def\pflA#1#2{\dot#1\ifx.#2\else\afterfi{\pflB#2}\fi}
\def\pflB#1#2{\ifx.#2\dot#1\else#1\afterfi{\pflB#2}\fi}

\let\periodbar=\overline

\def\periodparens#1{(#1)}

\def\periodmixed#1{\pmiA#1.}
\def\pmiA#1#2{\ifx.#2\dot#1\else\afterfi{\pmiB#1#2}\fi}
\def\pmiB#1.{\overline{#1}}

$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}$

\bye


Below is a comparable option using LaTeX (originally posted by egreg):

\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

• I contend that my current definition of \periodfl is simpler than yours. The method used now exploits methods not available at the time the original answer was written. Commented May 2 at 20:56
• When we try {\tracingall $1.2\periodfl{34567}$} with ergreg's expl3 code then we get 2169 lines in the log file about expansion of the macro \periodfl. If we do the same in my code, we get 71 lines, i.e. 30 times less than expl3 code. It doesn't seem that egreg's current definition is simpler from this point of view. Expl3 solution is certainly much more time-consuming and doing \tracingall is of little use because user is lost in tons of output. It means that the macro is practically untraceable. This is typical for LaTeX macros. Commented May 3 at 5:10
• As usual, this is like comparing Assembler with C. Commented May 3 at 6:36
• Ad comparison with Assembler: it is very difficult to read or write assembler code and computers are designed to use higher level of programming (for example tracing programs). But plain TeX code is comfortable to read and write and TeX was designed to creating and tracing such codes. So, the comparison falters. Commented May 3 at 8:30
• I didn't want to discredit anyone. Egreg's answer is good, he got about 40 votes for it and then deleted it. It is misunderstood that I want to discredit someone. I want to offer readers different perspectives on how to write macros in TeX. So the comparison is useful here. Commented May 3 at 16:53

The xlop package can be used to underline or highlight the period during a Euclidean division.

\documentclass{article}

\usepackage{xlop}

\begin{document}

The \textbf{vruleperiod} parameter (which defaults to -0.2) indicates the period by a line below it.

\opdiv[period,style=text]{1}{7}
\bigskip

It can be positioned above the period with \textbf{vruleperiod=0.7}.

\opdiv[period,style=text,vruleperiod=0.7]{1}{7}
\bigskip

The thickness of the period line is set here with hrulewidth =0.2pt (default value 0.4pt).

\opdiv[period,style=text,equalsymbol=$\approx$,
hrulewidth=0.2pt,vruleperiod=0.7,
afterperiodsymbol=]{1}{7}
\bigskip

The thickness of the period line is set here with hrulewidth=2pt:

\opdiv[period,style=text,equalsymbol=$\approx$,
hrulewidth=2pt,vruleperiod=0.7,
afterperiodsymbol=]{1}{7}
\bigskip

xlop can also be used to set divisions as they are in France.

\opdiv[period]{1}{7}
\end{document}


these answers are very kind but you lot are way over complicating it, literally just do \.{your number} no need to use any packages, it's built in. or other accents, see https://en.wikibooks.org/wiki/LaTeX/Special_Characters

• This doesn't work inside math mode though. Commented May 2 at 15:27
• \d{...} ("dot-over accent") is a text-mode command. It doesn't work in math mode. Even in text mode, its usefulness is rather limited if there's more than 1 recurring decimal. E.g., what do you think readers will "see" if you write \d{142857}?
– Mico
Commented May 2 at 15:41