![如何表示将十进制转换为二进制的方法](https://linux22.com/image/379191/%E5%A6%82%E4%BD%95%E8%A1%A8%E7%A4%BA%E5%B0%86%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9A%84%E6%96%B9%E6%B3%95.png)
我们如何才能在 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}