LoginSignup
2
2

More than 5 years have passed since last update.

TikZの木構造でパスカルの三角形

Last updated at Posted at 2017-07-02

TikZの木構造を利用してパスカルの三角形を描く。
木構造を利用すれば,ノードの座標のことを考える必要がなくなるのが嬉しい。
さて,うまくいくのか?

0段目と1段目

sample.tex
\documentclass[dvipdfmx]{jsarticle}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node {1}
  child { node {1} }
  child { node {1} };
\end{tikzpicture}
\end{document}

20170624-1.png

2段目を追加

sample.tex
\documentclass[dvipdfmx]{jsarticle}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node{1}
  child { node {1}
    child { node {1} }
    child { node {2} }
  }
  child { node {1}
    child { node {2} }
    child { node {1} }
  };
\end{tikzpicture}
\end{document}

20170624-1.png

「2」が重なっていて,というより微妙にずれていて,微妙に濃い。
そこで,一方を空にしてみると,

sample.tex
\documentclass[dvipdfmx]{jsarticle}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node{1}
  child { node {1}
    child { node {1} }
    child { node {2} }
  }
  child { node {1}
    child { node {} }
    child { node {1} }
  };
\end{tikzpicture}
\end{document}

20170624-1.png

枝の長さがおかしくなってしまった。

よく考えてみると,phantomを適用するのが“正解”か。

sample.tex
\documentclass[dvipdfmx]{jsarticle}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node{1}
  child { node {1}
    child { node {1} }
    child { node {2} }
  }
  child { node {1}
    child { node {\phantom{2}} }
    child { node {1} }
  };
\end{tikzpicture}
\end{document}

20170624-1.png

こんな調子で3段目追加

phantomを適用したノードには子ノードは設けない。(コードが)冗長になるので。

sample.tex
\documentclass[dvipdfmx]{jsarticle}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node{1}
  child { node {1}
    child { node {1}
      child { node {1} }
      child { node {3} }
    }
    child { node {2}
      child { node {\phantom{3}} }
      child { node {3} }
    }
  }
  child { node {1}
    child { node {\phantom{2}} }
    child { node {1}
      child { node {\phantom{3}} }
      child { node {1} }
    }
  };
\end{tikzpicture}
\end{document}

20170624-1.png

大きさの変更

ノードの間隔を変えることで図の大きさを変えるには,
縦はlevel distance,横はsibling distanceを指定すればよい。初期設定はどちらも15mmのようだ。

sample.tex
\documentclass[dvipdfmx]{jsarticle}
\usepackage{tikz}

\newcommand{\mypascal}{%
\node{1}
  child { node {1}
    child { node {1}
      child { node {1} }
      child { node {3} }
    }
    child { node {2}
      child { node {\phantom{3}} }
      child { node {3} }
    }
  }
  child { node {1}
    child { node {\phantom{2}} }
    child { node {1}
      child { node {\phantom{3}} }
      child { node {1} }
    }
  };
}

\begin{document}
\begin{tikzpicture}
\mypascal
\end{tikzpicture}
%
\begin{tikzpicture}[
  level distance=8mm
]
\mypascal
\end{tikzpicture}
%
\begin{tikzpicture}[
  level distance=8mm, sibling distance=10mm
]
\mypascal
\end{tikzpicture}
\end{document}

20170624-1.png


おしまい

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2