18

Is there a way to draw a tree (as shown below) in Stack Overflow using any LaTeX package or anything else?

Tree:

 1 
 \
  \
  3
 / \
/   \
2    5
   /   \
  /     \
  4      7
       /   \
      /     \
      6     11
           / \
          /   \
          9   12
         / \ 
         8 10
4
  • 4
    Plenty of tools out there you can use, like asciiflow.com
    – user1228
    Commented Oct 25, 2016 at 14:46
  • 2
    @Will asciiflow doesn't actually do that kind of tree.
    – simbabque
    Commented Oct 27, 2016 at 10:29
  • 3
    Tree (and table!) visualization is something that should probably be supported directly without resorting to manual ASCII drawings or third party sites.
    – canon
    Commented Oct 27, 2016 at 15:11
  • 1
    chris.com/ascii/index.php?art=plants/trees
    – j08691
    Commented Oct 27, 2016 at 21:36

4 Answers 4

26

Edit:

The faster way I found to draw graphs on linux is to use tree command:

mkdir test && cd test
mkdir -p 1/3/2 1/3/5/4 1/3/5/7/6 1/3/5/7/11
tree && cd .. && rm -rf test
.
└── 1
    └── 3
        ├── 2
        └── 5
            ├── 4
            └── 7
                ├── 11
                └── 6

You can use Unicode box drawing characters:

1─3┬2
   └5┬4
     └7┬6
       └11┬9┬8
          │ └10
          └12

or alternatively:

 1
 │
 3
┌┴┐
2 5
 ┌┴┐
 4 7
  ┌┴┐
  6 11
   ┌┴┐
   9 12
  ┌┴┐
  8 10

Or you can draw a tree with graphviz, ie:

echo "graph{size=\"2\"; 1--3; 3--2; 3--5; 5--4; 5--7; 1,2,3,4,5,7[shape=plaintext]}" | dot -Tpng > tree.png

then put the picture on SO:

tree

1
  • For the crazy people running Windows, there is also a tree command that does the same thing.
    – Louis
    Commented Mar 14, 2019 at 10:09
16

TeX is not supported on Stack Overflow (though it is on several other Stack Exchange sites with MathJax).

Instead of using the syntax you have, you can use the html <pre> element - white space inside the tag will not collapse.

Alternatively - upload an image.

5
  • OH, that's why my mathjax wasn't working there... bummer.
    – BenPen
    Commented Oct 25, 2016 at 16:45
  • 7
    Can we get it for SO too? Please mom. Commented Oct 27, 2016 at 10:51
  • 1
    @GillBates: iirc, that has been requested before, and rejected.
    – Cerbrus
    Commented Oct 27, 2016 at 12:02
  • @Cerbrus ya: meta.stackoverflow.com/q/252282/958689 Commented Oct 27, 2016 at 15:10
  • 2
    But even in sites with MathJax, that's only for simple math, isn't it? There is no TikZ.
    – Oriol
    Commented Oct 27, 2016 at 15:21
7

SO's "insert image" accepts images from the clipboard. So another thing you can do is:

  1. Open your favorite image editor (like mspaint on Windows).
  2. Draw a tree.
  3. Select + copy image.
  4. Click the add image button and just paste it right in that window.

enter image description here

It's very convenient and you don't have to save files, browse for files, or visit other sites. It's also a heck of a lot faster than drawing it with characters.

1
  • Make a directory hierarchy in a temporary folder
  • Use the tree command
  • Copy and paste the text

Thusly

rep /tmp $ mkdir -p a/b/c
rep /tmp $ mkdir -p a/b/d
rep /tmp $ mkdir -p a/e/
rep /tmp $ tree a
a
|-- b
|   |-- c
|   `-- d
`-- e

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .