share
TeX - LaTeXHow do I write a repeating decimal in LaTeX?
[+71] [5] Zerium
[2012-10-04 11:36:45]
[ math-mode ]
[ https://tex.stackexchange.com/questions/75284/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'
@tohecz like 0.9 with a dot on top of the 9. - Zerium
(3) Is this what you want: 0.\dot{9}? - Alexander
(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'
See also tex.stackexchange.com/a/451455/128553. - CampanIgnis
[+85] [2012-10-04 11:52:31] Mico [ACCEPTED]

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


For people using this with siunitx, see also tex.stackexchange.com/a/97329/90237 - arekolek
1
[+43] [2012-10-04 12:20:05] egreg

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}

enter image description here


(2) OMG the first two look so confusing! - yo'
@tohecz According to the Wikipedia page they are used. I don't find the first particularly confusing. - egreg
@tohecz I'm used to the first one :-) - Joseph Wright
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
(2) @TRiG I don't find that mixing dots and bars is sound, but I've added \periodmixed. - egreg
@egreg. Quick work! I'm impressed. - TRiG
(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
(1) @Egon How would you write 1.23453(45)? As "1.234534545..."? - Random832
@Random832: indeed, "1.234534545..." (so it's always: fixed part, repeating part twice and ellipsis) - Egon
(2) @Random832: I would advise against using parenthesis, as they are used for uncertainty. en.wikipedia.org/wiki/Uncertainty#Measurements - hpekristiansen
(2) @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
(2) @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. - wipet
(1) @wipet Please, avoid trying to read somebody else’s mind. I didn’t want to challenge your answer, but took the occasion for cleaning up mine. - egreg
@wipet You shouldn't try to read my mind. Please,, stop. - egreg
(1) Maybe that wasn't your intention. Maybe you didn't notice that by deleting it, you accidentally invalidated my answer and thus the time I spent on it. - wipet
(1) @wipet On this site you answer questions, not show off how cleverer you are than other users. - egreg
@wipet In case you need to reference a specific revision of an answer, copy the link address of the link under that answer which is labeled "edited...ago". This link adress is of pattern: "h‍t‍t‍p‍s:/‍/tex.stackexchange.com/posts/⟨post id⟩/revisions". Change it to fit the pattern: "h‍t‍t‍ps:/‍/tex.stackexchange.com/revisions/⟨post id⟩/⟨revision number⟩". - Ulrich Diez
@wipet For example copying the link address of the link under your answer which is labeled "edited...ago" gets me the address "h‍t‍t‍ps:/‍/tex.stackexchange.com/posts/717056/revisions" . In order to link revision 2 of your answer, I change this to "h‍t‍t‍p‍s:/‍/‍tex.stackexchange.com/revisions/717056/2". (Copying the string of this comment, however, does not work as I had to insert some zero-width-word joiners, U+200D in unicode, to have the string displayed in this comment rather than have it transformed into a link and displayed in a form where "h‍t‍t‍p‍s:" is removed. ) - Ulrich Diez
2
[+7] [2024-05-02 20:23:09] wipet

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}

\def\periodalldots#1{\padotsA#1.}
\def\padotsA#1#2{\dot#1\ifx.#2\else\afterfi{\padotsA#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

enter image description here

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

\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
[1] https://tex.stackexchange.com/revisions/75289/3

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. - egreg
(2) 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. - wipet
(2) As usual, this is like comparing Assembler with C. - egreg
Please, remove that addition. Why would you present it? Just to show off? - egreg
(2) 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. - wipet
Remember, answers serve the entire community. And there is little benefit to the community for an answer to reference (via a note) others' answers in an attempt to discredit it, unless it is completely wrong. This is not the case here from what I could see, so I removed the edit/addition. If that were the case, one can flag the answer for review by the rest of the community (like the Low Quality Review Queue) and/or use voting to express your support for/against the answer and/or through a comment to answerer. - Werner
(2) 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. - wipet
@wipet: Good. I've updated the reference in your answer to state things more simply/plainly. I think it removes any confusion and points to egreg's original revision that hosted the code. - Werner
@egreg ad showing off: Everyone who answers here is showing off at the moment that he/she something knows. Without this human emotion, a project like stackexchange wouldn't work at all. You show yourself here every day without ceasing, several times a day. Maybe almost everyone (including you) would benefit from your taking a break from staxkechange for a few months. Unfortunately, I can't wait for something like that to happen. - wipet
@wipet You're wrong. I added the “legacy version” because the expl3 one wasn't very satisfying. When the question was bumped I realized that the expl3 could be made much simpler with features that weren't available at the time, so the legacy version was no longer relevant. And the new version is simpler than yours, because it is completely straightforward, needing no trick whatsoever: if the argument is a single digit, dot it; otherwise dot the first, print the mddle ones and dot the last one. - egreg
3
[+1] [2024-05-03 18:07:15] AndréC

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}

enter image description here


4
[-1] [2024-05-02 15:09:20] Sea Monkey

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


(2) This doesn't work inside math mode though. - Marijn
(2) \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
5