我编辑了一个功能关于垂直的乘法来实现水平的两者的放置乘数。
+
我需要在 6942 和 4628 之间居中的黑色标志。我尝试过\begin{align}
,但是功能不起作用\multiplication
。
\documentclass{article}
\usepackage{fp,intcalc}
\begin{document}
\def\multiplication#1#2{
\def\answer{\FPmul\temp{#1}{#2}
\parindent=0pt
\FPround\temp{\temp}{0}\temp}
\def\lineFirst{#1.#2}
\def\linez##1##2##3{
\intcalcMul{#1}{##2}##3}
\def\Rule{\rule{1.3cm}{0.3pt}}
\begin{minipage}[t]{1.5cm}
\begin{flushright}
\lineFirst \vspace{-3mm}
\Rule \\
\def\Z{\phantom{0}}
\linez{#1}{2}\\
\linez{#1}{1}{\Z}\\[-8pt]
\Rule
\answer
\end{flushright}
\end{minipage}}
\multiplication{2314}{12}
$\; \;$
\multiplication{2314}{12}
\end{document}
答案1
我会用它expl3
来做这项工作。你真的用句号来表示乘法吗?
\documentclass{article}
\usepackage{xparse,booktabs}
\ExplSyntaxOn
\NewDocumentCommand{\multiplication}{sO{c}mm}
{
\IfBooleanTF { #1 }
{
\ensuremath{ #3.#4=\int_eval:n { #3 * #4 } }
}
{
\simeon_multiplication:nnn { #2 } { #3 } { #4 }
}
}
\seq_new:N \l__simeon_multiplication_b_seq
\seq_new:N \l__simeon_multiplication_c_seq
\int_new:N \l__simeon_multiplication_step_int
\cs_new_protected:Nn \simeon_multiplication:nnn
{
% store the second factor in reverse order
\seq_set_split:Nnn \l__simeon_multiplication_b_seq { } { #3 }
\seq_reverse:N \l__simeon_multiplication_b_seq
% store the partial products with a padding
\seq_clear:N \l__simeon_multiplication_c_seq
\int_zero:N \l__simeon_multiplication_step_int
\seq_map_inline:Nn \l__simeon_multiplication_b_seq
{
\seq_put_right:Nx \l__simeon_multiplication_c_seq
{
&\int_eval:n { #2 * ##1 }
\prg_replicate:nn { \l__simeon_multiplication_step_int } { \__simeon_zero: }
}
\int_incr:N \l__simeon_multiplication_step_int
}
\begin{tabular}[#1]{@{}r@{}r@{}}
\multicolumn{2}{@{}r@{}}{#2.#3} \\
\midrule
\seq_use:Nn \l__simeon_multiplication_c_seq { \__simeon_plus: } \\
\midrule
& \int_eval:n { #2 * #3 }
\end{tabular}
}
\cs_new_protected:Nn \__simeon_zero: { \hphantom{0} }
\cs_new_protected:Nn \__simeon_plus:
{
\\
\raisebox{0.6667\normalbaselineskip}[0pt][0pt]{+\,}
\\[-\normalbaselineskip]
}
\ExplSyntaxOff
\begin{document}
% center align
\multiplication{2314}{12}
\multiplication{9999}{99}
\multiplication{9999}{9999}
\bigskip
% top align
\multiplication[t]{2314}{12}
\multiplication[t]{9999}{99}
\multiplication[t]{9999}{9999}
\bigskip
% bottom align
\multiplication[b]{2314}{12}
\multiplication[b]{9999}{99}
\multiplication[b]{9999}{9999}
\bigskip
% only multiplication
\multiplication*{2314}{12}\par
\multiplication*{9999}{99}\par
\multiplication*{9999}{9999}
\end{document}
如果第一个数字小于第二个数字,则交换进行计算的数字。它也适用于浮点数(不过,在这种情况下,对齐目前并不完美)。
\documentclass{article}
\usepackage{xparse,siunitx,booktabs,multirow}
\sisetup{output-decimal-marker={,}}
\ExplSyntaxOn
\NewDocumentCommand{\multiplication}{sO{c}mm}
{
\group_begin:
\sisetup { group-separator = {} }
\tl_set:Nn \l__simeon_multiplication_do_tl { \num{#3}.\num{#4} }
\IfBooleanTF { #1 }
{
\simeon_multiplication_inline:nn { #3 } { #4 }
}
{
\simeon_multiplication:nnn { #2 } { #3 } { #4 }
}
\group_end:
}
\tl_new:N \l__simeon_multiplication_do_tl
\tl_new:N \l__simeon_multiplication_a_tl
\tl_new:N \l__simeon_multiplication_b_tl
\seq_new:N \l__simeon_multiplication_b_seq
\seq_new:N \l__simeon_multiplication_c_seq
\int_new:N \l__simeon_multiplication_step_int
\fp_new:N \l__simeon_multiplication_result_fp
\cs_new_protected:Nn \simeon_multiplication_inline:nn
{
\tl_set:Nn \l__simeon_multiplication_a_tl { #1 }
\tl_set:Nn \l__simeon_multiplication_b_tl { #2 }
\tl_replace_once:Nnn \l__simeon_multiplication_a_tl { , } { . }
\tl_replace_once:Nnn \l__simeon_multiplication_b_tl { , } { . }
\ensuremath
{
\l__simeon_multiplication_do_tl =
\num
{
\fp_eval:n
{
\l__simeon_multiplication_a_tl * \l__simeon_multiplication_b_tl
}
}
}
}
\cs_new_protected:Nn \simeon_multiplication:nnn
{
% store the second factor in reverse order
\tl_set:Nn \l__simeon_multiplication_a_tl { #2 }
\tl_set:Nn \l__simeon_multiplication_b_tl { #3 }
\tl_replace_once:Nnn \l__simeon_multiplication_a_tl { , } { . }
\tl_replace_once:Nnn \l__simeon_multiplication_b_tl { , } { . }
\fp_set:Nn \l__simeon_multiplication_result_fp
{
\l__simeon_multiplication_a_tl * \l__simeon_multiplication_b_tl
}
\fp_compare:nTF { \l__simeon_multiplication_a_tl > \l__simeon_multiplication_b_tl }
{
\__simeon_multiplication:nNN { #1 } \l__simeon_multiplication_a_tl \l__simeon_multiplication_b_tl
}
{
\__simeon_multiplication:nNN { #1 } \l__simeon_multiplication_b_tl \l__simeon_multiplication_a_tl
}
}
\cs_new_protected:Nn \__simeon_multiplication:nNN
{
% find the number of final zeros
\tl_clear:N \l__simeon_multiplication_zeros_tl
\__simeon_multiplication_zeros:NN #2 #3
% remove the decimal separator
\tl_remove_once:Nn #2 { . }
\tl_remove_once:Nn #3 { . }
% store the second factor in reverse order
\seq_set_split:NnV \l__simeon_multiplication_b_seq { } #3
\seq_reverse:N \l__simeon_multiplication_b_seq
% store the partial products with a padding
\seq_clear:N \l__simeon_multiplication_c_seq
\int_zero:N \l__simeon_multiplication_step_int
\seq_map_inline:Nn \l__simeon_multiplication_b_seq
{
\seq_put_right:Nx \l__simeon_multiplication_c_seq
{
&\int_eval:n { #2 * ##1 }
\prg_replicate:nn { \l__simeon_multiplication_step_int } { \__simeon_zero: }
}
\int_incr:N \l__simeon_multiplication_step_int
}
\begin{tabular}[#1]{@{}r@{}r@{}}
\multicolumn{2}{@{}r@{}}{\l__simeon_multiplication_do_tl} \\
\midrule
\multirow{\tl_count:N #3}{*}{\raisebox{\depth}{+}\,}
\seq_use:Nn \l__simeon_multiplication_c_seq { \\ } \\
\midrule
& \num{ \fp_use:N \l__simeon_multiplication_result_fp }
\l__simeon_multiplication_zeros_tl
\end{tabular}
}
\cs_new_protected:Nn \__simeon_zero: { \hphantom{0} }
\cs_new_protected:Nn \__simeon_plus:
{
\\
\raisebox{0.66667\normalbaselineskip}[0pt][0pt]{+\,}
\\[-\normalbaselineskip]
}
\cs_new_protected:Nn \__simeon_multiplication_zeros:NN
{
\seq_set_split:NnV \l_tmpa_seq { . } #1
\seq_set_split:NnV \l_tmpb_seq { . } #2
\tl_set:Nx \l_tmpa_tl
{
\seq_item:Nn \l_tmpa_seq { 2 }
\seq_item:Nn \l_tmpb_seq { 2 }
}
\tl_set:Nx \l_tmpb_tl { \fp_eval:n { #1 * #2 } }
\seq_set_split:NnV \l_tmpa_seq { . } \l_tmpb_tl
\tl_set:Nx \l_tmpb_tl { \seq_item:Nn \l_tmpa_seq { 2 } }
\tl_set:Nx \l__simeon_multiplication_zeros_tl
{
\prg_replicate:nn { \tl_count:N \l_tmpa_tl - \tl_count:N \l_tmpb_tl } { 0 }
}
}
\ExplSyntaxOff
\begin{document}
% center align
\multiplication{2314}{12}\quad
\multiplication{12}{2314}\quad
\multiplication{9999}{99}\quad
\multiplication{9999}{9999}\quad
\multiplication{23,14}{1,2}\quad
\multiplication{235}{388}\quad
\multiplication{2,35}{38,8}
\bigskip
% top align
\multiplication[t]{2314}{12}\quad
\multiplication[t]{12}{2314}\quad
\multiplication[t]{9999}{99}\quad
\multiplication[t]{9999}{9999}\quad
\multiplication[t]{23,14}{1,2}
\bigskip
% bottom align
\multiplication[b]{2314}{12}\quad
\multiplication[b]{12}{2314}\quad
\multiplication[b]{9999}{99}\quad
\multiplication[b]{9999}{9999}\quad
\multiplication[b]{23,14}{1,2}
\bigskip
% only multiplication
\multiplication*{2314}{12}\par
\multiplication*{12}{2314}\par
\multiplication*{9999}{99}\par
\multiplication*{9999}{9999}\par
\multiplication*{23,14}{1,2}
\end{document}