4

I am typing elementary arithmetics, is there any one knows how to type this ?

enter image description here

I know perhaps there's a package "polynom" could solve this problem, but is there any method that could work without calling external packages?

Thank you.

3

Not going into deep explanations, but this uses only Plain TeX macros. ;-)

\documentclass{article}

\makeatletter

\newcommand{\shortdiv}[1]{%
  \vcenter{\offinterlineskip\m@th
    \setbox\z@=\hbox{$\big|$}%
    \def\short@div@strut{\vrule width0pt height\ht\z@ depth\dp\z@}%
    \dimen@=-.5em
    \short@div#1;\short@div;
  }%
}

\def\short@div#1;{%
  \ifx\short@div#1%
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {\short@@div#1,\@nil}%
}

\def\short@@div#1,#2\@nil{%
  \advance\dimen@ .5em
  \if\relax\detokenize{#2}\relax
    % last step
    \hbox{\kern\dimen@$\short@div@strut#1$}%
    \expandafter\@gobble
  \else
    \short@@@div#1,#2%
    \expandafter\@firstofone
  \fi
  \short@div
}
\def\short@@@div#1,#2,{%
  \hbox{\kern\dimen@$\short@div@strut#1\,$\short@@@@div{#2}}%
}
\def\short@@@@div#1{%
  \setbox\z@=\hbox{$\,\short@div@strut#1$}%
  \dimen@=1em \ifdim\wd\z@>1em \dimen@=\wd\z@ \advance\dimen@ .5em \fi
  \vrule\kern-0.4pt\vtop{\hbox to \dimen@{$\,\short@div@strut#1$\hss}\hrule}%
}
\makeatother

\begin{document}
\[
\shortdiv{2,a;2,b;2,c;3,d;3,e;5,33333;5,g;7}
\]
\end{document}

enter image description here

1

Adapting my answer at Division with remainders displayed on side. EDITED to adapt its use to arguments with descenders AND to extend the horizontal rule length AND use math mode for the arguments AND to take box measurements in math mode AND extended the strutheight to the above line.

EDITED again to automate the indents. The indent is reset to 0pt following each invocation of \remainder{} or alternately by passing an optional [0] to the first invocation of \mydiv (the optional argument is the indent level).

EDITED to remove version that uses packages.

Here, I took the \stackengine macro, and condensed it down by hardwiring all the parameters to fit the current problem. Thus, I call it \dumbstackengine.

\documentclass{article}
\newcounter{divline}
\def\rlwd{.5pt} \def\rlht{\dimexpr\dp\strutbox+\ht\strutbox} \def\rldp{.75ex}
\newcommand\mydiv[3][\relax]{%
  \ifx\relax#1\stepcounter{divline}\else\setcounter{divline}{#1}\fi%
  \mbox{}\hspace{\thedivline\dimexpr1ex}#2~\setbox0=\hbox{~$#3$}%
  \dumbstackengine{-\rlwd}{\rule[-\rldp]{\rlwd}{\rlht}~#3}{\rule{\dimexpr4pt+\wd0}{\rlwd}}%
}
\def\remainder#1{\stepcounter{divline}%
  \mbox{}\hspace{\dimexpr1ex+\thedivline\dimexpr1ex}~#1\setcounter{divline}{0}}
\makeatletter
\global\newlength\@stackedboxwidth
\newlength\@boxshift
\newsavebox\@addedbox
\newsavebox\@anchorbox
\newcommand*\dumbstackengine[3]{%
    \sbox{\@anchorbox}{$#2$}%
    \sbox{\@addedbox}{$#3$}%
    \setlength{\@stackedboxwidth}{\wd\@anchorbox}%
      \ifdim\wd\@addedbox>\@stackedboxwidth%
        \setlength{\@stackedboxwidth}{\wd\@addedbox}%
      \fi%
        \setlength{\@boxshift}{\dimexpr-\dp\@anchorbox -\ht\@addedbox -#1}%
        \usebox{\@anchorbox}%
        \hspace{-\wd\@anchorbox}%
        \raisebox{\@boxshift}{\usebox{\@addedbox}}%
        \hspace{-\wd\@addedbox}%
        \hspace{\@stackedboxwidth}%
}
\makeatother
\begin{document}
\noindent%
\mydiv{2}{a} \\
\mydiv{2}{b} \\
\mydiv{2}{c} \\
\mydiv{3}{d} \\
\mydiv{3}{e} \\
\mydiv{5}{34689} \\
\mydiv{5}{g} \\
\remainder{7}            
\end{document}

enter image description here

1

Depending on your exact spacing needs some variation of the following might work.

\parindent=0pt
\newbox\leftbox
\newbox\rightbox
\newbox\skipbox
\def\div#1#2{\hskip\wd\skipbox\setbox\skipbox\hbox{\unhcopy\skipbox#1}
\setbox\leftbox\hbox{#1}\unhcopy\leftbox\vrule height 2ex depth 1pt width .5pt
\setbox\rightbox\hbox{#2}\vrule height-.5pt depth1pt width\wd\rightbox\hskip-\wd\rightbox\unhcopy\rightbox\par}
\div{2}{a}
\div{3}{b}
\div{123}{abcd}
\bye
  • This throws an error: ! Undefined control sequence. \div ...th\wd \rightbox \hskip -\wd \rightbox \un \^^Mhcopy\rightbox \par l.9 \div{2}{a} ? And the output is strange: 2Lhcopyj 3Lhcopyj 123Lhcopyj – AboAmmar Jul 10 '15 at 17:10
  • AboAmmar: fixed now – hkBst Jul 10 '15 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.