66

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

Any help?

Zerium
  • 12,559
  • Do you have an example of what you want to write? Because it is unclear what you mean by "recurring decimal" – yo' Oct 04 '12 at 11:41
  • @tohecz like 0.9 with a dot on top of the 9. – Zerium Oct 04 '12 at 11:42
  • 2
    Is this what you want: 0.\dot{9}? – Alexander Oct 04 '12 at 11:45
  • 4
    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 04 '12 at 11:50
  • See also https://tex.stackexchange.com/a/451455/128553. – CampanIgnis Aug 12 '19 at 13:40

2 Answers2

80

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}

enter image description here

Mico
  • 506,678
39

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

enter image description here


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 } }
 }
\cs_new_protected:Npn \__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
egreg
  • 1,121,712
  • 2
    OMG the first two look so confusing! – yo' Oct 04 '12 at 12:21
  • @tohecz According to the Wikipedia page they are used. I don't find the first particularly confusing. – egreg Oct 04 '12 at 12:22
  • @tohecz I'm used to the first one :-) – Joseph Wright Oct 04 '12 at 12:22
  • I would use the dot (first or second option) for a single digit, and either the bar or multiple dots (second or third option) for multiple digits. So the second option would work for me, but I'd prefer to use the bar for multiple digits and the dot for a single digit. – TRiG Oct 04 '12 at 14:25
  • 2
    @TRiG I don't find that mixing dots and bars is sound, but I've added \periodmixed. – egreg Oct 04 '12 at 14:35
  • @egreg. Quick work! I'm impressed. – TRiG Oct 04 '12 at 14:43
  • 1
    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 04 '12 at 17:01
  • 1
    @Egon How would you write 1.23453(45)? As "1.234534545..."? – Random832 Oct 04 '12 at 19:29
  • @Random832: indeed, "1.234534545..." (so it's always: fixed part, repeating part twice and ellipsis) – Egon Oct 05 '12 at 04:45
  • 2
    @Random832: I would advise against using parenthesis, as they are used for uncertainty. http://en.wikipedia.org/wiki/Uncertainty#Measurements – hpekristiansen Oct 05 '12 at 13:29
  • 1
    @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 05 '12 at 13:33