Skip to content

Instantly share code, notes, and snippets.

@k16shikano
Last active November 18, 2018 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k16shikano/05d95106e0b2a802afdf0206cebc3ab1 to your computer and use it in GitHub Desktop.
Save k16shikano/05d95106e0b2a802afdf0206cebc3ab1 to your computer and use it in GitHub Desktop.
TeXでさくらんぼ計算
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,calc}
\makeatletter
\def\sakurambo#1{%
\parse@expression#1
}
\def\draw@sakurambo@expression#1#2#3#4#5{%
$$#1 + \draw@sakurambo{#2}{#3}{#4} = #5$$
}
\def\draw@expression#1#2#3{%
$$#1 + #2 = #3$$
}
\def\parse@expression#1+#2{%
\@tempcnta=#1 \@tempcntb=#2
\advance \@tempcnta by -10
\advance \@tempcntb by \@tempcnta
\multiply \@tempcnta by -1
\ifnum\@tempcntb>0
\expandafter%
\draw@sakurambo@expression
{#1}{#2}{\the\@tempcnta}{\the\@tempcntb}{%
\@tempcnta=#1
\advance \@tempcnta by #2
\the\@tempcnta}
\else
\expandafter
\draw@expression
{#1}{#2}{%
\@tempcnta=#1
\advance \@tempcnta by #2
\the\@tempcnta}
\fi
}
\tikzset{
cherrynode/.style = {circle, white, draw=black, fill=red},
cherrybend/.style = {edge from parent path={(\tikzparentnode.south) edge [bend left=15] (\tikzchildnode.north)},
very thick},
no edge/.style = {edge from parent/.append style={draw=none}}
}
\def\draw@sakurambo#1#2#3{%
\makebox[1em][c]{%
\begin{tikzpicture}[
draw=green,
cherrybend,
sibling distance=1.5em,
level distance=3em,
baseline={([yshift={-\dp\strutbox+0.5pt}]current bounding box.base)}]
\node {#1}
child { node [cherrynode] {#2} }
child { node [cherrynode] {#3} }
;
\end{tikzpicture}
}
}
\makeatother
\begin{document}
\sakurambo{5+9}
\sakurambo{9+9}
\sakurambo{7+9}
\sakurambo{1+4}
\sakurambo{1+9}
\sakurambo{7+4}
\makeatletter
%\parse@expression7+8
%\temp
%\draw@sakurambo@expression{7}{8}{3}{5}{15}
\makeatother
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment