我在数学模式下尝试排版以下内容时遇到困难。我无法获得正确的间距。
0100 [十进制 4] x 1100 [十进制为 12] ------ + 0000 [0100 x 0] + 0000 [0100x 0,左移] + 0100 [0100x 1,左移两位] + 0100 [0100x 1,左移三位] ========= 0110000 [48 十进制]
这可以在数学模式下实现吗,或者坚持使用预格式化的文本更好/更容易?
答案1
\hphantom
已经被提议了,但让它看起来真的很好看是一种有趣的消遣。
\documentclass{article}
\usepackage{amsmath}
\newcommand{\0}{\phantom{0}}
\begin{document}
\begin{alignat*}{4}
&& 0100 & \quad && \text{[\,4 in decimal]}\\
&& \underline{\strut{}\times 1100} &&& \text{[\,12 in decimal]}\\
& + & 0000 &&& \text{[\,$0100\times 0$]}\\
& + & 0000\0 &&& \text{[\,$0100\times 0$, shifted left]}\\
& + & 0100\0\0 &&& \text{[\,$0100\times 1$, shifted left two positions]}\\
& \underline{\strut+} & \underline{\strut\0 0100\0\0\0} &&&
\text{[\,$0100\times 1$, shifted left three positions]}\\
&& \0 0110000 &&& \text{[\,48 decimal]}
\end{alignat*}
\end{document}
答案2
\documentclass{article}
\usepackage{array}
\setlength\extrarowheight{2pt}
\begin{document}
\[
\begin{array}{@{}*{9}{c@{}}>{\quad$[}l<{]$}@{}}
& && & &0&1&0&0& 4 in decimal\\
& &&\times & &1&1&0&0& 12 in decimal\\
\cline{5-9}
+& & & & &0&0&0&0& 0100 x 0\\
+& & & &0&0&0&0& & 0100x 0, shifted left\\
+& & &0&1&0&0& & & 0100x 1, shifted left two positions\\
+&\phantom{0} &0&1&0&0& & &&0100x 1, shifted left three positions\\
\cline{3-9}
\noalign{\kern2pt}
\cline{3-9}
& &0&1&1&0&0&0&0& 48 decimal
\end{array}
\]
\end{document}
答案3
只是为了好玩!;-)
表格由
\binmult{4}{12}
这是代码。
\documentclass{article}
\usepackage{xparse,booktabs}
\ExplSyntaxOn
\NewDocumentCommand{\binmult}{mm}
{
\david_binmult:nn { #1 } { #2 }
}
\int_new:N \l_david_binmult_first_int
\int_new:N \l_david_binmult_second_int
\int_new:N \l_david_binmult_bits_int
\int_new:N \l__david_binmult_cycle_int
\tl_new:N \l_david_binmult_first_tl
\tl_new:N \l_david_binmult_second_tl
\tl_new:N \l_david_binmult_second_rev_tl
\tl_new:N \l_david_binmult_product_tl
\tl_new:N \l_david_binmult_tablebody_tl
\seq_new:N \l_david_binmult_partial_products_seq
\seq_new:N \l_david_binmult_partial_descr_seq
\cs_new_protected:Nn \__david_binmult_pad:Nn
{% #1 should be a tl, #2 an integer denomination
\prg_replicate:nn { \int_max:nn { #2 - \tl_count:N #1 } { 0 } }
{
\tl_put_left:Nn #1 { 0 }
}
}
\cs_new_protected:Nn \david_binmult:nn
{
% store the data
\int_set:Nn \l_david_binmult_first_int { #1 }
\int_set:Nn \l_david_binmult_second_int { #2 }
\tl_set:Nx \l_david_binmult_first_tl { \int_to_bin:n { #1 } }
\tl_set:Nx \l_david_binmult_second_tl { \int_to_bin:n { #2 } }
\tl_set:Nx \l_david_binmult_product_tl { \int_to_bin:n { #1 * #2 } }
% pad the factors
\int_set:Nn \l_david_binmult_bits_int
{
\int_max:nn { \tl_count:N \l_david_binmult_first_tl }
{ \tl_count:N \l_david_binmult_second_tl }
}
% pad the product
\__david_binmult_pad:Nn \l_david_binmult_product_tl
{
2*\l_david_binmult_bits_int-1
}
\__david_binmult_pad:Nn \l_david_binmult_first_tl { \l_david_binmult_bits_int }
\__david_binmult_pad:Nn \l_david_binmult_second_tl { \l_david_binmult_bits_int }
\tl_set_eq:NN \l_david_binmult_second_rev_tl \l_david_binmult_second_tl
\tl_reverse:N \l_david_binmult_second_rev_tl
% compute the partial products
\seq_clear:N \l_david_binmult_partial_products_seq
\seq_clear:N \l_david_binmult_partial_descr_seq
\int_zero:N \l__david_binmult_cycle_int
\tl_map_inline:Nn \l_david_binmult_second_rev_tl
{
\int_compare:nTF { ##1 = 0 }
{
\seq_put_right:Nx \l_david_binmult_partial_products_seq
{
\prg_replicate:nn { \l_david_binmult_bits_int } { 0 }
\prg_replicate:nn { \l__david_binmult_cycle_int } { \exp_not:N \hphantom {0} }
}
}
{
\seq_put_right:Nx \l_david_binmult_partial_products_seq
{
\tl_use:N \l_david_binmult_first_tl
\prg_replicate:nn { \l__david_binmult_cycle_int } { \exp_not:N \hphantom {0} }
}
}
\seq_put_right:Nx \l_david_binmult_partial_descr_seq
{
[
$\tl_use:N \l_david_binmult_first_tl \times ##1$
\int_case:nnF { \l__david_binmult_cycle_int }
{
{0}{}
{1}{,~shifted~left}
}
{,~shifted~left~\david_binmult_number:n { \l__david_binmult_cycle_int }~positions}
]
}
\int_incr:N \l__david_binmult_cycle_int
}
% build the tabular
\tl_clear:N \l_david_binmult_tablebody_tl
\tl_put_right:Nn \l_david_binmult_tablebody_tl
{
& $\tl_use:N \l_david_binmult_first_tl$ & [$#1$~in~decimal] \\
& ${\times}\;\tl_use:N \l_david_binmult_second_tl$ & [$#2$~in~decimal] \\
\cmidrule(r){2-2}
}
\int_step_inline:nnnn { 1 } { 1 } { \seq_count:N \l_david_binmult_partial_products_seq }
{
\tl_put_right:Nx \l_david_binmult_tablebody_tl
{
$+$ &
\seq_item:Nn \l_david_binmult_partial_products_seq { ##1 } &
\seq_item:Nn \l_david_binmult_partial_descr_seq { ##1 } \exp_not:N \\
}
}
\tl_put_right:Nn \l_david_binmult_tablebody_tl
{
\cmidrule(r){1-2}
& $\tl_use:N \l_david_binmult_product_tl$ & [$\int_to_arabic:n { #1 * #2 }$~in~decimal]
}
% print the table
\begin{tabular}{@{} c r l @{}}
\tl_use:N \l_david_binmult_tablebody_tl
\end{tabular}
}
\cs_new:Nn \david_binmult_number:n
{
\int_case:nn { #1 }
{
{0}{zero}
{1}{one}
{2}{two}
{3}{three}
{4}{four}
{5}{five}
{6}{six}
{7}{seven}
{8}{eight}
}
}
\ExplSyntaxOff
\begin{document}
\binmult{4}{12}
\bigskip
\binmult{12}{12}
\bigskip
\binmult{13}{35}
\end{document}