1

I want to make something like this. Quadratic Sequence

I tried using tree diagrams and got this code,

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{tasks}
\usepackage{geometry}
% \usepackage{poker}
\usepackage{array}
\usepackage{indentfirst}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{arrows}
\usetkzobj{all}
\usetikzlibrary{trees}
 \geometry{
 a4paper,
 total={170mm,257mm},
 left=20mm,
 top=20mm,
 bottom=20mm
 }
 \settasks{
  style = enumerate ,
  counter-format = tsk),
  label-align = right,
  label-width = 2em,
  label-offset = .75em
  %label-format = \bfseries
}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcommand{\N}{\mathbb N}
\newcommand{\Q}{\mathbb Q}
\newcommand{\C}{\mathbb C}
\newcommand{\Z}{\mathbb Z}
\setcounter{secnumdepth}{0}
\begin{document}

% Set the overall layout of the tree
\tikzstyle{level 2}=[level distance=2cm, sibling distance=6cm]
\tikzstyle{level 3}=[level distance=2cm, sibling distance=3cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

\begin{tikzpicture}[grow=up, sloped]
\node {}
    child {
        node[bag] {$2a$}
        child {
            node[bag] {$5a+b$}
            child {
                node {$16a+4b+c$}
            }
            child {
                node {$9a+3b+c$}
            }
        }
        child {
            node[bag] {$3a+b$}
            child {
                node {$4a+2b+c$}
            }
            child {
                node {$a+b+c+d$}
            }
        }
    };
\end{tikzpicture}
\end{document}

which yields this result:my version

The problem with this is if I do another set that includes another 2a, those two 2a's will branch into one node. How should I change my code?

  • Welcome! Can you remove irrelevant stuff from your preamble? I'm not really sure what you mean by branching into one node. Generally, tree-drawing packages are designed for trees, which have a single root. It might be easier to use the matrix library. – cfr Sep 1 '17 at 12:46
  • You can use a phantom root (and the standard trees doesn't care anyway), but the real problem is that tree nodes have at most one parent each and not, as in the case of biological organisms, two. – cfr Sep 1 '17 at 12:50
3

Trees have a single root and every non-root node has one parent. Generally, packages designed to draw trees are specialised software for drawing trees in this sense and not, say, more generalised graphs. The TikZ library doesn't mind multiple roots, but it is still tricky to deal with multiple parents and so on.

I think it would be easier to think of the diagram as a matrix with some edges drawn between some of the nodes.

For example,

\documentclass[border=10pt]{standalone}
\usepackage{tikz,amsmath}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
  \matrix (M) [matrix of math nodes]
  {
    & n=1 && n=2 && n=3 && n=4\\
   T_n & a+b+c && 4a+2b+c && 9a+3b+c && 16a+4b+c\\
   \text{1\textsuperscript{st} difference} & & 3a+b && 5a+b && 7a+b & \\
   \text{2\textsuperscript{nd} difference} & & & 2a && 2a &&\\
  };
  \draw (M-2-2) -- (M-3-3) -- (M-4-4) -- (M-3-5) edge (M-2-4) edge (M-2-6) -- (M-4-6) -- (M-3-7) edge (M-2-6) -- (M-2-8) ;
\end{tikzpicture}

\end{document}

matrix

  • i would draw line as \draw (M-2-2) -- (M-3-3) -- (M-2-4) -- (Mm-3-5) -- (M-2-6) -- (M-3-7) -- (M-2-8) (M-3-3) -- (M-4-4) -- (M-3-5) -- (M-4-6) -- (M-3-7); ... you were faster than me for few minutes :( – Zarko Sep 1 '17 at 13:45
  • @Zarko Too much typing :-). – cfr Sep 1 '17 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.