
이그나시10진수 블록에 대한 몇 가지 훌륭한 코드를 만들었고 이를 사용하여 자동으로 2자리 숫자 표현을 생성하려고 했습니다.
%%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}
비슷한 그림을 자동으로 생성하는 그런 코드를 갖고 싶습니다 . (더 많은 자릿값 위치를 허용하기 위해 1의 값이 먼저 오는 경우, \basetenpic{2}{0}{3}
300과 2의 1로 그림을 만드는 것처럼 ...)
누구든지 어떤 아이디어가 있습니까?
답변1
적응
- 정의된 길이
\unitsize
와\unitsep
foreach
노드를 그리는 데 사용- 등을 사용하여
\ifnumgreater{\tens}{0}{}{}
어떤 블록을 그려야 하는지 확인합니다. - 또한 숫자만 하나의 매개변수, 확장으로 사용하는 방법도 생각했지만 Jasper의 답변에서 이것이 이미 수행되었음을 확인했습니다. 그래서 방금 래퍼 함수를 추가했습니다
\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개의 단일 상자가 10개의 상자 높이와 일치하는 것은 불가능합니다. .)
다음은 그러한 상자를 출력합니다. 예를 들어, 노란색 등으로 채우는 등 취향에 따라 다양한 상자의 스타일을 지정할 수 있습니다. 매크로는 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}
링크한 원래 답변은 를 생성하는 pic
반면, 이 솔루션은 전체 tikzpicture
. 하지만 서로 변환하는 것은 그리 복잡하지 않습니다.
간격을 제거하면 상자의 면적은 서로 정확한 관계를 갖게 됩니다(즉, 10개의 상자는 작은 상자 하나의 면적의 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,
}
}
}
결과 예: