17

How to make the traditional division? Like that:

a |b_
r  c
2
  • possible duplicate of Decimal Long Division
    – Werner
    Commented May 13, 2013 at 2:37
  • 1
    @Werner I don't think this is really a duplicate; in a comment to my answer Fabricio specified that he needed a manual way to represent the division, so the xlop option (which is the suggestion in the possible duplicate) was not really an option in this case. Commented May 13, 2013 at 2:59

2 Answers 2

20

One option would be to use the xlop package:

\documentclass{report}
\usepackage{xlop}

\begin{document} 

\opidiv{25}{7}\qquad\opdiv{25}{7}

\end{document}

enter image description here

Manually, you can use an array:

\documentclass{article}

\newcommand\myrule[1]{\multicolumn{1}{| l}{#1}}

\begin{document}

\[
\begin{array}{rl}
478 & \myrule{7} \\
\cline{2-2}
58 & 68 \\
2
\end{array}
\]

\end{document}

enter image description here

If you are only interested in the dividend, divisor, quotient and remainder, you can define a two-argument command (quotient and remainder can be automatically calculated) along the lines of

\documentclass{article}
\usepackage{intcalc}


\newcommand\mydiv[2]{%
\ifnum#2>0
\[
\renewcommand\arraystretch{1.2}
\begin{array}{@{}r | l}
#1 & #2 \\
\cline{2-2}
\multicolumn{1}{r}{\intcalcMod{#1}{#2}} 
& 
\ifnum#1>0\relax
  \intcalcDiv{#1}{#2}
\else
  \number\numexpr\intcalcDiv{#1}{#2}-1\relax
\fi
\end{array}
\]
\else
\GenericWarning{}{Division Warning: "Please provide a positive integer as divisor"}
\fi
}


\begin{document}

\mydiv{478}{7}

\end{document}

enter image description here

5
  • 1
    I actualy need the manual way. Because i'm using an online latex editor to generate my outputs.
    – Fabricio
    Commented May 13, 2013 at 1:25
  • 1
    @Fabricio please see my updated answer. Commented May 13, 2013 at 1:33
  • 1
    @Fabricio You're welcome! I did another update to my answer taht could be of interest to you. Commented May 13, 2013 at 1:42
  • @GonzaloMedina \numexpr division rounds: try \mydiv{478/8}
    – egreg
    Commented May 13, 2013 at 8:22
  • @Fabricio There was a mistake in the last code that I've corrected in my updated answer. Commented May 13, 2013 at 17:34
8

For the simple diagrams you can do in this way:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\division}{smm}
 {
  \IfBooleanTF{#1}
   { \fabricio_division_inline:nn { #2 } { #3 } }
   { \fabricio_division:nn { #2 } { #3 } }
 }

\cs_new:Npn \fabricio_division_inline:nn #1 #2
 {
  #1=#2\cdot\int_div_truncate:nn { #1 } { #2 }
     + \int_mod:nn { #1 } { #2 }
 }
\cs_new_protected:Npn \fabricio_division:nn #1 #2
 {
  \begin{array}{r | l}
  #1 & #2 \\
  \cline{2-2}
  \multicolumn{1}{r}{\int_mod:nn { #1 } { #2 }} &
    \int_div_truncate:nn { #1 } { #2 }
  \end{array}
 }
\ExplSyntaxOff

\begin{document}
\[
\division*{1234}{42}
\qquad
\division{1234}{42}
\]
\end{document}

The *-form just prints the division in the form a=bq+r.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .