![带框的长除法](https://linux22.com/image/263283/%E5%B8%A6%E6%A1%86%E7%9A%84%E9%95%BF%E9%99%A4%E6%B3%95.png)
我需要为一位患有注意力缺陷多动症的学生制作一张除法工作表。他经常对数字的位置感到困惑,所以这张工作表上的方框必须非常精确地对齐。目前没有可用的印刷工作表可以满足我的需要。我自己尝试过一个带方框的长除法代码,但它没有给我想要的图像。我需要创建一个类似于下面的图像,带有水平线和减号。
我迄今为止尝试过:
\documentclass[11pt]{article}
\begin{document}
\newcommand\divspace{\,}
\[ \arraycolsep=0em
\begin{array}{r@{\divspace}c@{\divspace}lllll}
& & \multicolumn{1}{r}{15} & \\ \cline{2-6}
15 & \big) & \multicolumn{4}{l}{225} & \\
& & \fbox{1} \fbox{5} & & & & \\ \cline{3-4}
& & \fbox{7}\fbox{5} & & & \\
& & \fbox{7}\fbox{5} & & & \\ \cline{3-5}
& & & 0 & & \\
\end{array}
\]
\end{document}
答案1
\documentclass{article}
\usepackage{color}
\newsavebox\wbox
\savebox\wbox{\kern2pt{\color{white}\rule{.5cm}{.5cm}}\kern2pt}
\begin{document}
\colorbox[rgb]{0,0,.3}{\parbox{5cm}{%
\baselineskip0pt
\lineskip4pt
\def\n#1{\b\raisebox{3pt}{\llap{\large#1\kern7pt}}}
\def\b{\usebox\wbox}
\def\,{\kern4pt}
\def\_{\mbox{}\kern4pt\kern.5cm}
\def\r#1{\par\kern3pt\mbox{}\_\kern1pt{%
\color{white}\smash{\vrule height1pt width 1pt depth \dimexpr 1cm+7pt\relax}%
\vrule height1pt depth0pt width\dimexpr .5cm*#1+4pt*#1+4pt\relax}\par\kern1pt}
\_\,\b\b\b\b\,\b\\
\r4
\n5\,\n3\n3\n8\n8\\
\_\,\b\\
\_\,\b\b\\
\_\,\b\b\\
\_\,\_\b\b\\
\_\,\_\b\b\\
\_\,\_\_\b\b\\
\_\,\_\_\b\b\\
\_\,\_\_\_\b
}}
\end{document}
答案2
半自动化 TikZ 解决方案。
样式是根据按键divisor length
和设置的dividend length
。但实际输入仍由用户提供。
重要的是,应获得指定样式的单元格必须具有文本(可以是空格,例如\␣
或~
)。
额外的行和列分隔也是如此。\\
不应输入 TikZ 矩阵的最后一个结束行命令。longdivision
环境会自动执行此操作。
界面
可以通过以下方式修改该行:
\ldset{every line/.tyle={<style options for the line>}}
% or
\ldset{every line/.append style={<options to add to the existing line>}}
可以通过 删除该行\ldset{every line/.style={}}
。
所有框均可通过 键进行修改boxes
。不同类型框的样式可通过divisors
、dividends
、results
、rests
键进行设置others
。
代码
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{matrix,calc}
\newcommand*{\ldset}[1]{\pgfkeys{/ld/.cd,#1}}
\ldset{
every long division/.style={
fill=teal,
column sep=.5em,
row sep=.5em,
},
every box/.style={
shape=rectangle,
draw=none,
fill=white,
text width=1.3em,
align=center,
text height=0.95em,
text depth=0.35em,
rounded corners,
execute at begin node=,%
execute at end node=,
name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn%
},
every divisor/.style={
/ld/every box,
fill=red!50,
},
every dividend/.style={
/ld/every box,
fill=yellow!30,
},
every result/.style={
/ld/every box,
fill=gray!20,
},
every rest/.style={
/ld/every box,
fill=teal!20,
},
every other/.style={
/ld/every box,
},
boxes/.style={/ld/every box/.append style={#1}},
divisors/.style={/ld/every divisor/.append style={#1}},
dividends/.style={/ld/every dividend/.append style={#1}},
results/.style={/ld/every result/.append style={#1}},
rests/.style={/ld/every rest/.append style={#1}},
others/.style={/ld/every other/.append style={#1}},
divisor length/.code={%
\def\qrrLDdivisorLength{#1}%
\def\qrrLDrestLength{#1}%
},
dividend length/.code={%
\def\qrrLDdividendLength{#1}%
\def\qrrLDresultLength{#1}%
},
result length/.estore in=\qrrLDresultLength,
rest length/.estore in=\qrrLDrestLength,
every line/.style={
draw=black,
rounded corners=2pt,
thick,
line cap=round,
}
}
\tikzset{
execute style/.style={#1},
invisible text/.style={text opacity=0},
}
\newenvironment{longdivision}[2][]{%
\ldset{#2}%
\def\QrrLDdivisorStyle{}%
\foreach \i in {1,...,\qrrLDdivisorLength}{%
\xdef\QrrLDdivisorStyle{\QrrLDdivisorStyle, row 2 column \i/.style={every node/.style={/ld/every divisor}}}%
}%
\pgfmathtruncatemacro\QrrLDfirstRestColumn{\qrrLDdivisorLength+\qrrLDresultLength+1}%
\edef\QrrLDrestStyle{}%
\foreach \i in {\QrrLDfirstRestColumn,...,\numexpr\QrrLDfirstRestColumn+\qrrLDrestLength\relax}{%
\xdef\QrrLDrestStyle{\QrrLDrestStyle, row 1 column \i/.style={every node/.style={/ld/every rest}}}%
}%
\tikzpicture[#1]
\matrix[
name=m,
/ld/every long division,
every node/.style={/ld/every other},
row 1/.style={every node/.style={/ld/every result}},
row 2/.style={every node/.style={/ld/every dividend}},
execute style/.expand once={\QrrLDdivisorStyle},
execute style/.expand once={\QrrLDrestStyle},
matrix of nodes,
] \bgroup
}{
\pgfmatrixendrow\egroup;
\pgfmathtruncatemacro\QrrLDbeforeFirstRestColumn{\QrrLDfirstRestColumn-1}
\pgfmathtruncatemacro\QrrLDafterLastDivisorColumn{\qrrLDdivisorLength+1}
\path[/ld/every line] ($(m-1-\QrrLDfirstRestColumn.south west)!.5!(m-2-\QrrLDbeforeFirstRestColumn.north east)$) -|
($(m-2-\qrrLDdivisorLength.south east)! .5!(m-3-\QrrLDafterLastDivisorColumn.north west)$);
\endtikzpicture%
}
\begin{document}
\begin{longdivision}{
divisor length=1,
dividend length=4,
others={invisible text},
results={invisible text},
rests={invisible text},
}
&[1em] 0 & 6 & 7 & 7 &[1em] 3\\[1em]
5 & 3 & 3 & 8 & 8 & \\
& 0 \\
& 3 & 3 \\
& 3 & 0 \\
& & 3 & 8 \\
& & 3 & 5 \\
& & & 3 & 8 \\
& & & 3 & 5 \\
& & & & 3
\end{longdivision}
\begin{longdivision}{
divisor length=1,
dividend length=4,
}
&[1em] 0 & 6 & 7 & 7 &[1em] 3\\[1em]
5 & 3 & 3 & 8 & 8 & \\
& 0 \\
& 3 & 3 \\
& 3 & 0 \\
& & 3 & 8 \\
& & 3 & 5 \\
& & & 3 & 8 \\
& & & 3 & 5 \\
& & & & 3
\end{longdivision}
\end{document}
输出