How to put "+" and "x" after \hline in tabular, and reducing space?
TikZ is overkilling here, just simply use multirow
s and \cline
s.
\documentclass[a4paper,12pt,oneside]{letter}
\usepackage[left=2.00cm, right=2.00cm, bottom=2.00cm, top=2.00cm]{geometry}
\usepackage{multirow}
\begin{document}
\begin{center}
\setlength\tabcolsep{1pt}
\begin{tabular}{*{8}{c}}
&&&6&0&0&1\\
&&&&2&7&2&\multirow{2}{*}[-.2pt]{$\times$}\\
\cline{1-7}
&&1&4&0&0&2\\
&5&2&0&0&7&$\cdot$\\
1&4&0&0&2&$\cdot$&$\cdot$&\multirow{2}{*}[-.2pt]{$+$}\\
\cline{1-7}
2&1&3&4&2&7&2
\end{tabular}
\end{center}
\end{document}
Not that I like this from a mathematical point of view, but…
\documentclass{article}
\usepackage{array}
\newenvironment{multiplication}[1]
{% #1 = number of digits in the result
\setlength{\arraycolsep}{1pt}%
\renewcommand{\arraystretch}{0}%
\newcommand{\size}{#1}%
\mathcode`*=\cdot
\begin{array}{*{#1}{>{\strut}c} c}
}
{\end{array}}
\newcommand{\dividerline}[1]{% #1 = symbol
\multicolumn{\size}{c}{%
\leaders\hrule height \dimexpr\fontdimen22\textfont2+0.2pt\relax
depth -\dimexpr\fontdimen22\textfont2-0.2pt\relax
\hfill\kern0pt
}%
& \smash{#1} \\
}
\begin{document}
\[
\begin{multiplication}{7}
& & & 6 & 0 & 0 & 1 \\
& & & & 2 & 7 & 2 \\ \dividerline{\times}
& & 1 & 4 & 0 & 0 & 2 \\
& 5 & 2 & 0 & 0 & 7 & * \\
1 & 4 & 0 & 0 & 2 & * & * \\ \dividerline{+}
2 & 1 & 3 & 4 & 2 & 7 & 2
\end{multiplication}
\]
\end{document}
A “slightly” more complicated version that allows you not to do calculations error like in the above scheme. ;-)
\documentclass{article}
\usepackage{array,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\multiplication}{O{c}mm}
{
\group_begin:
% some initializations
\setlength{\arraycolsep}{1pt}
\renewcommand{\arraystretch}{0}
\mathcode`*=\cdot
% the main command
\ensuremath { \ongky_multiply:nnn { #1 } { #2 } { #3 } }
% end the group
\group_end:
}
\tl_new:N \l__ongky_multiply_first_tl
\tl_new:N \l__ongky_multiply_second_tl
\tl_new:N \l__ongky_multiply_result_tl
\tl_new:N \l__ongky_multiply_output_tl
\seq_new:N \l__ongky_multiply_summands_seq
\int_new:N \l__ongky_multiply_size_int
\cs_new_protected:Nn \ongky_multiply:nnn
{
\tl_set:Nx \l__ongky_multiply_first_tl { \int_eval:n { #2 } }
\tl_set:Nx \l__ongky_multiply_second_tl { \int_eval:n { #3 } }
\tl_set:Nx \l__ongky_multiply_result_tl { \int_eval:n { (#2)*(#3) } }
\int_set:Nn \l__ongky_multiply_size_int { \tl_count:N \l__ongky_multiply_result_tl }
% create the numbers to sum up (padding with * on the right)
\int_step_inline:nn { \tl_count:N \l__ongky_multiply_second_tl }
{
\seq_put_right:Nx \l__ongky_multiply_summands_seq
{
\int_eval:n
{
\l__ongky_multiply_first_tl *
\tl_item:Nn \l__ongky_multiply_second_tl { -##1 }
}
\prg_replicate:nn { ##1 - 1 } { * }
}
}
% start building the body of the array
\__ongky_multiply_put:V \l__ongky_multiply_first_tl
\__ongky_multiply_put:V \l__ongky_multiply_second_tl
\tl_put_right:Nn \l__ongky_multiply_output_tl { \__ongky_divider:n { \times } }
\seq_map_inline:Nn \l__ongky_multiply_summands_seq
{
\__ongky_multiply_put:n { ##1 }
}
\tl_put_right:Nn \l__ongky_multiply_output_tl { \__ongky_divider:n { + } }
\__ongky_multiply_put:V \l__ongky_multiply_result_tl
% print the array
\begin{array}[#1]{ *{\l__ongky_multiply_size_int}{>{\strut}c} c }
\l__ongky_multiply_output_tl
\end{array}
}
\cs_new_protected:Nn \__ongky_multiply_put:n
{
\tl_set:Nx \l__ongky_multiply_temp_tl
{
\prg_replicate:nn { \l__ongky_multiply_size_int - \tl_count:n { #1 } } { {} }
#1
}
\tl_map_function:NN \l__ongky_multiply_temp_tl \__ongky_multiply_put_item:n
\tl_put_right:Nn \l__ongky_multiply_output_tl { \\ }
}
\cs_generate_variant:Nn \__ongky_multiply_put:n { V }
\cs_new_protected:Nn \__ongky_multiply_put_item:n
{
\tl_put_right:Nn \l__ongky_multiply_output_tl { #1 & }
}
\cs_new:Nn \__ongky_divider:n
{
\multicolumn{\l__ongky_multiply_size_int}{c}{%
\leaders\hrule height \dimexpr\fontdimen22\textfont2+0.2pt\relax
depth -\dimexpr\fontdimen22\textfont2-0.2pt\relax
\hfill\kern0pt
}%
& \smash{#1} \\
}
\ExplSyntaxOff
\begin{document}
\multiplication{6001}{272}
\bigskip
\multiplication[t]{32}{444}\qquad
\multiplication[t]{444}{32}
\end{document}
A more efficient code using \tl_build
experimental features:
\documentclass{article}
\usepackage{array,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\multiplication}{O{c}mm}
{
\group_begin:
% some initializations
\setlength{\arraycolsep}{1pt}
\renewcommand{\arraystretch}{0}
\mathcode`*=\cdot
% the main command
\ensuremath { \ongky_multiply:nnn { #1 } { #2 } { #3 } }
% end the group
\group_end:
}
\tl_new:N \l__ongky_multiply_first_tl
\tl_new:N \l__ongky_multiply_second_tl
\tl_new:N \l__ongky_multiply_result_tl
\tl_new:N \l__ongky_multiply_output_tl
\seq_new:N \l__ongky_multiply_summands_seq
\int_new:N \l__ongky_multiply_size_int
\cs_new_protected:Nn \ongky_multiply:nnn
{
\tl_set:Nx \l__ongky_multiply_first_tl { \int_eval:n { #2 } }
\tl_set:Nx \l__ongky_multiply_second_tl { \int_eval:n { #3 } }
\tl_set:Nx \l__ongky_multiply_result_tl { \int_eval:n { (#2)*(#3) } }
\int_set:Nn \l__ongky_multiply_size_int { \tl_count:N \l__ongky_multiply_result_tl }
% create the numbers to sum up (padding with * on the right)
\int_step_inline:nn { \tl_count:N \l__ongky_multiply_second_tl }
{
\seq_put_right:Nx \l__ongky_multiply_summands_seq
{
\int_eval:n
{
\l__ongky_multiply_first_tl *
\tl_item:Nn \l__ongky_multiply_second_tl { -##1 }
}
\prg_replicate:nn { ##1 - 1 } { * }
}
}
% start building the body of the array
\tl_build_begin:N \l__ongky_multiply_output_tl
\__ongky_multiply_put:V \l__ongky_multiply_first_tl
\__ongky_multiply_put:V \l__ongky_multiply_second_tl
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { \__ongky_divider:n { \times } }
\seq_map_inline:Nn \l__ongky_multiply_summands_seq
{
\__ongky_multiply_put:n { ##1 }
}
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { \__ongky_divider:n { + } }
\__ongky_multiply_put:V \l__ongky_multiply_result_tl
\tl_build_end:N \l__ongky_multiply_output_tl
% print the array
\begin{array}[#1]{ *{\l__ongky_multiply_size_int}{>{\strut}c} c }
\l__ongky_multiply_output_tl
\end{array}
}
\cs_new_protected:Nn \__ongky_multiply_put:n
{
\tl_set:Nx \l__ongky_multiply_temp_tl
{
\prg_replicate:nn { \l__ongky_multiply_size_int - \tl_count:n { #1 } } { {} }
#1
}
\tl_map_function:NN \l__ongky_multiply_temp_tl \__ongky_multiply_put_item:n
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { \\ }
}
\cs_generate_variant:Nn \__ongky_multiply_put:n { V }
\cs_new_protected:Nn \__ongky_multiply_put_item:n
{
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { #1 & }
}
\cs_new:Nn \__ongky_divider:n
{
\multicolumn{\l__ongky_multiply_size_int}{c}{%
\leaders\hrule height \dimexpr\fontdimen22\textfont2+0.2pt\relax
depth -\dimexpr\fontdimen22\textfont2-0.2pt\relax
\hfill\kern0pt
}%
& \smash{#1} \\
}
\ExplSyntaxOff
\begin{document}
\multiplication{6001}{272}
\bigskip
\multiplication[t]{32}{444}\qquad
\multiplication[t]{444}{32}
\end{document}
Season’s gift: a version that allows any base from 2 to 36
\documentclass{article}
\usepackage{array,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\multiplication}{O{c}mmO{10}}
{
\group_begin:
% some initializations
\setlength{\arraycolsep}{1pt}
\renewcommand{\arraystretch}{0}
\mathcode`*=\cdot
% dirty trick for letters
\sbox0{$\mathtt{\int_gset:Nn \g_tmpa_int {\mathgroup}}$}
\int_step_inline:nnn { `A } { `Z }
{
\mathcode ##1=\int_eval:n { \g_tmpa_int * 16 * 16 + ##1 }
}
% the main command
\ensuremath { \ongky_multiply:nnnn { #1 } { #2 } { #3 } { #4 } }
% end the group
\group_end:
}
\tl_new:N \l__ongky_multiply_first_tl
\tl_new:N \l__ongky_multiply_second_tl
\tl_new:N \l__ongky_multiply_result_tl
\tl_new:N \l__ongky_multiply_output_tl
\seq_new:N \l__ongky_multiply_summands_seq
\int_new:N \l__ongky_multiply_size_int
\cs_new:Nn \__ongky_multiply_base:nnn
{
\int_to_Base:nn { \int_from_base:nn { #1 } { #3 } * \int_from_base:nn { #2 } { #3 } } { #3 }
}
\cs_generate_variant:Nn \__ongky_multiply_base:nnn { ff }
\cs_new_protected:Nn \ongky_multiply:nnnn
{
\tl_set:Nx \l__ongky_multiply_first_tl { #2 }
\tl_set:Nx \l__ongky_multiply_second_tl { #3 }
\tl_set:Nx \l__ongky_multiply_result_tl
{
\__ongky_multiply_base:nnn { #2 } { #3 } { #4 }
}
\int_set:Nn \l__ongky_multiply_size_int { \tl_count:N \l__ongky_multiply_result_tl }
% create the numbers to sum up (padding with * on the right)
\int_step_inline:nn { \tl_count:N \l__ongky_multiply_second_tl }
{
\seq_put_right:Nx \l__ongky_multiply_summands_seq
{
\__ongky_multiply_base:ffn
{ \l__ongky_multiply_first_tl }
{ \tl_item:Nn \l__ongky_multiply_second_tl { -##1 } }
{ #4 }
\prg_replicate:nn { ##1 - 1 } { * }
}
}
% start building the body of the array
\tl_build_begin:N \l__ongky_multiply_output_tl
\__ongky_multiply_put:V \l__ongky_multiply_first_tl
\__ongky_multiply_put:V \l__ongky_multiply_second_tl
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { \__ongky_divider:n { \times } }
\seq_map_inline:Nn \l__ongky_multiply_summands_seq
{
\__ongky_multiply_put:n { ##1 }
}
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { \__ongky_divider:n { + } }
\__ongky_multiply_put:V \l__ongky_multiply_result_tl
\tl_build_end:N \l__ongky_multiply_output_tl
% print the array
\begin{array}[#1]{ *{\l__ongky_multiply_size_int}{>{\strut}c} c }
\l__ongky_multiply_output_tl
\end{array}
}
\cs_new_protected:Nn \__ongky_multiply_put:n
{
\tl_set:Nx \l__ongky_multiply_temp_tl
{
\prg_replicate:nn { \l__ongky_multiply_size_int - \tl_count:n { #1 } } { {} }
#1
}
\tl_map_function:NN \l__ongky_multiply_temp_tl \__ongky_multiply_put_item:n
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { \\ }
}
\cs_generate_variant:Nn \__ongky_multiply_put:n { V }
\cs_new_protected:Nn \__ongky_multiply_put_item:n
{
\tl_build_put_right:Nn \l__ongky_multiply_output_tl { #1 & }
}
\cs_new:Nn \__ongky_divider:n
{
\multicolumn{\l__ongky_multiply_size_int}{c}{%
\leaders\hrule height \dimexpr\fontdimen22\textfont2+0.2pt\relax
depth -\dimexpr\fontdimen22\textfont2-0.2pt\relax
\hfill\kern0pt
}%
& \smash{#1} \\
}
\ExplSyntaxOff
\begin{document}
\multiplication{6001}{272}\qquad
\multiplication{6001}{272}[8]\qquad
\multiplication{6001}{272}[16]\qquad
\multiplication{6001}{272}[36]
\bigskip
\multiplication[t]{32}{444}\qquad
\multiplication[t]{444}{32}\qquad
\multiplication[t]{32}{444}[5]\qquad
\multiplication[t]{444}{32}[5]\qquad
\end{document}
In the first row the multiplications are in base 10, 8, 16 and 36 respectively.
In the second row the first two are in base 10, the last two in base 5.
Here is a proposal. Decreasing the distance is standard. In order to place the symbols, I am loading TikZ. Loading TikZ for only this may be overkill.
\documentclass[a4paper,12pt,oneside]{letter}
\usepackage[left=2.00cm, right=2.00cm, bottom=2.00cm, top=2.00cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}
\begin{center}
\begin{tabular}{*{8}{@{\,}c}}
&&&&6&0&0&1\\
&&&&&2&7&\tikzmarknode{2a}{2}\\
\hline %$\times$
&&&1&4&0&0&\tikzmarknode{2b}{2}\\
&&5&2&0&0&7&$\cdot$\\
&1&4&0&0&2&$\cdot$&$\tikzmarknode{dot}{\cdot}$\\
\hline%$+$
&2&1&3&4&2&7&\tikzmarknode{2c}{2}
\end{tabular}
\begin{tikzpicture}[overlay,remember picture]
\node at ([xshift=5mm]$(2a.south)!0.6!(2b.north)$) {$\times$};
\node at ([xshift=5mm]$(dot.south)!0.6!(2c.north)$) {$+$};
\end{tikzpicture}
\end{center}
\end{document}