如何表示将十进制转换为二进制的方法

如何表示将十进制转换为二进制的方法

我们如何才能在 LaTeX 中绘制将十进制转换为二进制的阶梯式方法,如下图所示,

在此处输入图片描述

我尝试使用以下代码,但它不符合我的目的:

\documentclass{article} 
\usepackage{colortbl} 

\begin{document}
$
\setlength\extrarowheight{1pt}
\begin{array}{@{}l|l@{}l@{}}
2 & 29 & \qquad \mbox{Remainder} \\ \cline{2-2} 
2 & 14 & \qquad 1\\  \cline{2-2}
2 & 7 & \qquad 0\\  \cline{2-2}
2 & 3 & \qquad 1\\ \cline{2-2}
\multicolumn{1}{c}{} & 1
\end{array}
$
\end{document}

答案1

这是一个开始...

\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%
\begin{tabular}{lcc}
\mydiv{2}{29} &&Remainders\\
\mydiv{2}{14} &---& 1\\
\mydiv{2}{7} &---& 0\\
\mydiv{2}{3} &---& 1\\
\remainder{1} &---& 1 
\end{tabular}          
\end{document}

在此处输入图片描述

...以及非常手动的完成:

\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
\newcommand\trule{\smash{\rule[-\dp\strutbox]{.4pt}{1.2\normalbaselineskip}}}
\begin{document}
\noindent%
\begin{tabular}{lccc}
\mydiv{2}{29} &&Remainders&$\uparrow$\\
\mydiv{2}{14} &---& 1&\trule\\
\mydiv{2}{7} &---& 0&\trule\\
\mydiv{2}{3} &---& 1&\trule\\
\remainder{1} &---& 1&\trule\\[-8pt]
\rlap{\kern30pt\rule{108pt}{.4pt}}&&&
\end{tabular}          
\end{document}

在此处输入图片描述

相关内容