For example, I want to plot a graph whose adjacency matrix is [0,1,0;1,0,1;0,1,0]. I first plot three points labeled by p1,p2,p3, and link the points p1,p2, p2,p3. For a general case, how to link the points according to the adjacency matrix(which may be stored in a .txt file)?

  • To clarify: Are you choosing the coordinates for p1,p2,p3 yourself, or do you expect that to be handled automatically? – Charles Staats Mar 11 '14 at 12:36
  • Thanks! Choose the coordinates for p1,p2,p3 oneself. – Eden Harder Mar 11 '14 at 12:40

Here's my solution, which uses Python to parse the adjacency matrix into something TikZ can read. I'm using a Mac, so take that into account when I declare my directories. The steps I'm showing are just an example, but I think that's better than to do it very abstractly. So suppose that your main .tex file is located in /Users/Me/Desktop/latex/.

Step 1: Create a file in /Users/Me/Desktop/scripts/ called adjmat.py with the following contents:

F = file('/Users/Me/Desktop/latex/adjmat.txt','r')
L = map(lambda x: x.split(';'),F.readlines())[0]
L = [L[0][1:]] + L[1:-1] + [L[-1][:-1]]
S = ''
for i in range(len(L)):
    tempL = map(lambda x: x.split(','),[L[i]])[0]
    for j in range(i+1,len(tempL)):
        if tempL[j] == '1':
            S += '\\draw (p%g)--(p%g);\n'%(i+1,j+1)
G = file('/Users/Me/Desktop/latex/adjmat-tikz.txt','w')

Step 2: Write your adjacency matrix to /Users/Me/Desktop/latex/ called adjmat.txt with (for example) only the following text (the main thing is to have lines separated by ; and cells by ,):


Step 3: In Terminal, run python /Users/Me/Desktop/scripts/adjmat.py.

Step 4: In your main .tex file, have the following tikzpicture environment:

\node[circle,fill=black,inner sep=1pt] (p1) at (0,0) {};
\node[circle,fill=black,inner sep=1pt] (p2) at (1,0) {};
\node[circle,fill=black,inner sep=1pt] (p3) at (1,1) {};

Now you said that your vertices will be indicated manually, so that's what I worked with. I also assume they are named p1, p2, and so on. This generalizes to an adjacency matrix of any size, as long as there are no loops and no multiple edges. In this case, this is the result you get:

enter image description here

  • 1
    This is not really a TeX solution. If you are going to attack this problem with Python, I would at least recommend incorporating your scripts into the TeX document via PythonTex. – Chris Chudzicki Apr 15 '14 at 3:24
  • 1
    @mrc I thought of doing the parsing inside TeX, but as much as I've searched (which isn't too far) there's no clean, versatile text parser in TeX. It seems using PythonTex would be similar to my approach, but longer - judging by the documentation, the .tex file has to be compiled, then a .py file has to be executed, and then the .tex file has to be compiled again. Although the Python script would then be inside the .tex file, I (personally) don't think that's better, because if another language must be used, why mix it up with LaTeX. – Jānis Lazovskis Apr 15 '14 at 3:44

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