
伊格納西為十進制區塊編寫了一些很棒的程式碼,我一直在嘗試使用它來自動產生兩位數字的表示。
%%Base 10 Blocks
\tikzset {
node distance=.1cm,
hundred/.style={
draw,
minimum size=1cm,
inner sep=0pt
},
tenv/.style={
line width=0.1mm,
fill=red,
draw,
minimum height=1cm,
minimum width=0.1cm,
inner sep=0pt,
},
tenthh/.style={
draw,
minimum height=0.1cm,
minimum width=1cm,
inner sep=0pt,
},
unitone/.style={
line width=0.1mm,
draw,
minimum size=0.1cm,
inner sep=0pt,
},
base graph/.pic={
\node[hundred] (00) {};
\node[hundred, below=of 00] (10) {};
\node[tenthh, below=of 10] (20) {};
\foreach \i [count=\xi, remember=\xi as \lasti (initially 0)] in {1,2,3,4}
\node[tenv, right=of 0\lasti] (0\xi) {};
\path (00.north west) -- (20.south west) node[midway, left] {2.1} ;
\path (00.north west) -- (04.north east) node[midway, above] {1.4} ;
}
}
這是我用來產生數字 37 的一些程式碼
\begin{center} \begin{tikzpicture}[transform canvas={scale=3}]
\foreach \y in {-0.95,-0.85,...,-0.35}{
\node[unitone] at (0.1,\y){};}
\foreach \x in {-0.5,-0.3,...,-0.1}{
\node[tenv] at (\x,-0.5){};}
\end{tikzpicture}\end{center}
看起來這部分...
雖然非常繁瑣並且是在 Excel 中建立的。
我希望有這樣的程式碼可以\basetenpic{7}{3}
自動建立類似的圖片。 (其中個位值首先出現,以允許更多的位值位置,這樣\basetenpic{2}{0}{3}
就可以製作一張包含 3 個百的圖片,後面跟著 2 個個......)
有人有什麼想法嗎?
答案1
改編
- 定義的長度
\unitsize
和\unitsep
- 用於
foreach
繪製節點 - 使用
\ifnumgreater{\tens}{0}{}{}
etc.檢查是否應該繪製哪些塊 - 我還考慮過僅使用數字作為一個參數,作為擴展,但後來我看到賈斯珀的答案中已經完成了這一點。所以我只是添加了一個包裝函數
\basetenpicx{<number>}
。
結果
程式碼
\documentclass{article}
\usepackage{etoolbox}
\usepackage{tikz}
\newlength{\unitsize}
\setlength{\unitsize}{3mm}
\newlength{\unitsep}
\setlength{\unitsep}{3mm}
% Base 10 Blocks
\tikzset {
node distance=\unitsep,
hundred/.style={
draw,
fill=yellow,
anchor=south west,
minimum size=10*\unitsize,
inner sep=0pt
},
tenv/.style={
anchor=south west,
line width=0.1mm,
fill=red,
draw,
minimum height=10*\unitsize,
minimum width=\unitsize,
inner sep=0pt,
},
tenthh/.style={
anchor=south west,
draw,
minimum height=\unitsize,
minimum width=10*\unitsize,
inner sep=0pt,
},
unitone/.style={
anchor=south west,
line width=0.1mm,
draw,
minimum size=\unitsize,
inner sep=0pt,
},
}
\newcommand{\basetenpic}[3]{
\edef\ones{#1}
\edef\tens{#2}
\edef\hundreds{#3}
\begin{tikzpicture}
% uncomment this to have the same height
%\node[inner sep=0pt] at (0,10*\unitsize) {};
% one
\ifnumgreater{\ones}{0}{
\foreach \i in {1, ..., \ones}{
\node[unitone] at ({\hundreds*(10*\unitsize+\unitsep) + \tens*(\unitsize+\unitsep)}, {(\i-1)*\unitsize}) {};
}
}{}
% ten
\ifnumgreater{\tens}{0}{
\foreach \i in {1, ..., \tens}{
\node[tenv] at ({\hundreds*(10*\unitsize+\unitsep) + (\i-1)*(\unitsize+\unitsep)}, 0) {};
}
}{}
% hundret
\ifnumgreater{\hundreds}{0}{
\foreach \i in {1, ..., \hundreds}{
\node[hundred] at ({(\i-1)*(10*\unitsize+\unitsep)}, 0) {};
}
}{}
\end{tikzpicture}
}
\newcommand{\basetenpicx}[1]{
\pgfmathtruncatemacro{\ones}{mod(#1, 10)}
\pgfmathtruncatemacro{\tens}{mod(#1 - \ones, 100)/10}
\pgfmathtruncatemacro{\hundreds}{mod(#1 - \tens - \ones, 1000)/100}
\basetenpic{\ones}{\tens}{\hundreds}
}
\begin{document}
\obeylines
\verb|\basetenpic{7}{0}{0}|:
\basetenpic{7}{0}{0}
\verb|\basetenpic{7}{3}{0}|:
\basetenpic{7}{3}{0}
\verb|\basetenpicx{203}|:
\basetenpicx{203}
\verb|\basetenpicx{60}|:
\basetenpicx{60}
\end{document}
答案2
我認為,比使用接受一個、兩個或三個參數的巨集更好的方法是讓 PGF 進行數學計算。這樣,您只需在巨集的單一參數中鍵入 1 到 999 之間的任意數字\basetenpic
,框的數量就會自動計算。
我也可能會調整盒子和間隙的大小,以便 10 個較小的盒子(包括間隙)與一個較大的盒子具有相同的寬度或高度。 (當然,正如 @dexteritas 所指出的那樣,不可能匹配不同盒子的面積及其代表的值,同時讓 10 個單盒子(包括間隙)與 1 個十盒子的高度匹配.)
下面將輸出這樣的框框。您可能想根據自己的口味設計不同的盒子,例如,將它們填充為黃色或其他任何顏色。此巨集採用一個可選參數來將自訂選項新增至tikzpicture
樣式:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
basetenpic/.style={
node distance=0.05cm,
every node/.style={
draw,
line width=0.01cm,
inner sep=0pt,
},
hundreds/.style={
% = 10 x height of boxes + 9 x line width + 9 x gap
% = 10 x 0.1cm + 9 x 0.01cm + 9 x 0.05cm
minimum size=1.54cm,
},
tens/.style={
minimum height=1.54cm,
minimum width=0.1cm,
},
ones/.style={
minimum size=0.1cm,
}
}
}
\newcounter{boxcount}
\newcommand{\basetenpic}[2][]{
\pgfmathtruncatemacro{\ones}{mod(#2, 10)}
\pgfmathtruncatemacro{\tens}{mod(#2 - \ones, 100)/10}
\pgfmathtruncatemacro{\hundreds}{mod(#2 - \tens - \ones, 1000)/100}
\setcounter{boxcount}{0}
\begin{tikzpicture}[basetenpic, #1]
\coordinate (n0) at (0,0);
\ifnum\hundreds>0\relax
\foreach \i [evaluate={\theboxcount as \lastboxcount}] in {1,...,\hundreds} {
\stepcounter{boxcount}
\node[right=of n\lastboxcount, hundreds] (n\theboxcount) {};
}
\fi
\ifnum\tens>0\relax
\foreach \i [evaluate={\theboxcount as \lastboxcount}] in {1,...,\tens} {
\stepcounter{boxcount}
\node[right=of n\lastboxcount, tens] (n\theboxcount) {};
}
\fi
\ifnum\ones>0\relax
\foreach \i [evaluate={\theboxcount as \lastboxcount}] in {1,...,\ones} {
\stepcounter{boxcount}
\ifnum\i=1\relax
\node[right=of n\lastboxcount.south east, anchor=south west, ones] (n\theboxcount) {};
\else
\node[above=of n\lastboxcount, ones] (n\theboxcount) {};
\fi
}
\fi
\end{tikzpicture}
}
\begin{document}
\basetenpic{26}
\basetenpic{302}
\basetenpic[hundreds/.append style={fill=yellow}]{137}
\end{document}
您連結到的原始答案創建了 a pic
,而此解決方案輸出了完整的tikzpicture
.但將一種轉換為另一種並不太複雜。
如果消除間隙,盒子的面積將彼此具有精確的關係(即十位的盒子將是一個小盒子的面積的 10 倍,等等)。然而,為了保持一致性,我會把所有的空白都消除掉。您可以\tikzset
透過以下方式更改上述MWE的部分:
\tikzset{
basetenpic/.style={
% = minus half the line width
node distance=-0.005cm,
every node/.style={
draw,
line width=0.01cm,
inner sep=0pt,
},
hundreds/.style={
minimum size=1cm,
},
tens/.style={
minimum height=1cm,
minimum width=0.1cm,
},
ones/.style={
minimum size=0.1cm,
}
}
}
結果範例: