Quantcast
Channel: Active questions tagged string-manipulation - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 186

Convert tikz (in tikzit) code to a graph in Mathematica

$
0
0

I want to convert a TikZ graph from TikZiT into a graph in Mathematica. I want to retain the vertices, edges, and the positions of the vertices. As for the color of the vertices or the style of the edges, I don't need to worry about that for now. Then I used the code written by GPT (with some minor adjustments on my part). When the edge style is not set, everything works fine.

The information of a graph in TikZIt is explained as follows.

\node [style=square] (1) at (10, -10) {};

The node, named 1, is located at (10, -10).

\draw [style=blackedge] (5) to (11);

or

 \draw  (5) to (11);

This means an egde (5,11) (edge style is like to style=..).

A graph code is stored in test.tikz:

\begin{tikzpicture}[scale=1.0]    \begin{pgfonlayer}{nodelayer}        \node [style=square] (1) at (10, -10) {};        \node [style=whitenode] (2) at (5.29412, -3.33333) {};        \node [style=square] (3) at (10, 10) {};        \node [style=square] (4) at (-10, -10) {};        \node [style=whitenode] (5) at (3.33333, -5.29412) {};        \node [style=whitenode] (6) at (2.54902, -1.37255) {};        \node [style=whitenode] (7) at (5.29412, 3.33333) {};        \node [style=whitenode] (8) at (3.33333, 5.29412) {};        \node [style=square] (9) at (-10, 10) {};        \node [style=whitenode] (10) at (-5.29412, -3.33333) {};        \node [style=whitenode] (11) at (-3.33333, -5.29412) {};        \node [style=whitenode] (12) at (1.37255, -2.54902) {};        \node [style=square] (13) at (0.980392, -0.980392) {};        \node [style=whitenode] (14) at (2.54902, 1.37255) {};        \node [style=whitenode] (15) at (1.37255, 2.54902) {};        \node [style=whitenode] (16) at (-3.33333, 5.29412) {};        \node [style=whitenode] (17) at (-5.29412, 3.33333) {};        \node [style=whitenode] (18) at (-2.54902, -1.37255) {};        \node [style=whitenode] (19) at (-1.37255, -2.54902) {};        \node [style=square] (20) at (-0.980392, -0.980392) {};        \node [style=square] (21) at (0.980392, 0.980392) {};        \node [style=whitenode] (22) at (-1.37255, 2.54902) {};        \node [style=whitenode] (23) at (-2.54902, 1.37255) {};        \node [style=square] (24) at (-0.980392, 0.980392) {};    \end{pgfonlayer}    \begin{pgfonlayer}{edgelayer}        \draw (1) to (2);        \draw (1) to (3);        \draw (1) to (4);        \draw (1) to (5);        \draw (2) to (5);        \draw (2) to (6);        \draw (2) to (7);        \draw (3) to (7);        \draw (3) to (8);        \draw (3) to (9);        \draw (4) to (9);        \draw (4) to (10);        \draw (4) to (11);        \draw (5) to (11);        \draw (5) to (12);        \draw (6) to (12);        \draw (6) to (13);        \draw (6) to (14);        \draw (7) to (8);        \draw (7) to (14);        \draw (8) to (15);        \draw (8) to (16);        \draw (9) to (16);        \draw (9) to (17);        \draw (10) to (11);        \draw (10) to (17);        \draw (10) to (18);        \draw (11) to (19);        \draw (12) to (13);        \draw (12) to (19);        \draw (13) to (20);        \draw (13) to (21);        \draw (14) to (15);        \draw (14) to (21);        \draw (15) to (21);        \draw (15) to (22);        \draw (16) to (17);        \draw (16) to (22);        \draw (17) to (23);        \draw (18) to (19);        \draw (18) to (20);        \draw (18) to (23);        \draw (19) to (20);        \draw (20) to (24);        \draw (21) to (24);        \draw (22) to (23);        \draw (22) to (24);        \draw (23) to (24);        \draw (16) to (15);        \draw (8) to (22);        \draw (17) to (22);        \draw (16) to (23);        \draw (23) to (10);        \draw (17) to (18);        \draw (18) to (11);        \draw (10) to (19);        \draw (19) to (5);        \draw (12) to (11);        \draw (6) to (5);        \draw (12) to (2);        \draw (2) to (14);        \draw (7) to (6);        \draw (15) to (7);        \draw (8) to (14);        \draw (22) to (21);        \draw (15) to (24);        \draw (24) to (13);        \draw (21) to (20);        \draw (20) to (12);        \draw (13) to (19);        \draw (23) to (20);        \draw (24) to (18);        \draw (13) to (14);        \draw (6) to (21);        \draw (9) to (10);        \draw (17) to (4);        \draw (4) to (5);        \draw (11) to (1);        \draw (1) to (7);        \draw (3) to (2);        \draw (9) to (8);        \draw (16) to (3);        \draw (4) to (3);        \draw (9) to (1);    \end{pgfonlayer}\end{tikzpicture}
ImportTikZGraph[file_String] :=  Module[{tikzCode, lines, nodes, edges, line, parts, nodeID, xCoord,    yCoord, startNode, endNode,    g},(*Import the TikZ code from the file*)  tikzCode = Import[file, "Text"];  (*Initialize lists for nodes and edges*)nodes = {};  edges = {};  (*Split the TikZ code into lines*)  lines = StringSplit[tikzCode, "\n"];  (*Extract node information*)  For[i = 1, i <= Length[lines], i++, line = StringTrim[lines[[i]]];   If[StringStartsQ[line, "\\node"],(*Extract node ID and coordinates*)    parts = StringCases[line, "\\node [" ~~ ___ ~~ "] (" ~~ nodeID : DigitCharacter .. ~~ ") at (" ~~ x : (DigitCharacter | "." | "-" | "E") .. ~~ ", " ~~ y : (DigitCharacter | "." | "-" | "E") .. ~~ ") {};" :> {nodeID, x, y}];    If[Length[parts] > 0, nodeID = ToExpression[parts[[1, 1]]];     xCoord = ToExpression[parts[[1, 2]]];     yCoord = ToExpression[parts[[1, 3]]];     AppendTo[nodes, nodeID -> {xCoord, yCoord}];];];];  (*Extract edge information*)  For[i = 1, i <= Length[lines], i++, line = StringTrim[lines[[i]]];   If[StringStartsQ[line, "\\draw"],(*Extract the start and end nodes of edges*)    parts = StringCases[line, "\\draw (" ~~ startNode : DigitCharacter .. ~~ ") to (" ~~         endNode : DigitCharacter .. ~~ ");" :> {startNode, endNode}];    If[Length[parts] > 0, startNode = ToExpression[parts[[1, 1]]];     endNode = ToExpression[parts[[1, 2]]];     AppendTo[edges, UndirectedEdge[startNode, endNode]];];];];  (*Create the graph from extracted edges and node positions*)  g = Graph[edges, VertexCoordinates -> nodes,     VertexStyle -> GrayLevel[1], VertexSize -> Medium];  (*Return the graph*)g]graph = ImportTikZGraph["test.tikz"]

enter image description here

However, when I set the edge styles, it seems to fail. I've tried a few times.

\begin{tikzpicture}[scale=1.0]    \begin{pgfonlayer}{nodelayer}        \node [style=square] (1) at (10, -10) {};        \node [style=whitenode] (2) at (5.29412, -3.33333) {};        \node [style=square] (3) at (10, 10) {};        \node [style=square] (4) at (-10, -10) {};        \node [style=whitenode] (5) at (3.33333, -5.29412) {};        \node [style=whitenode] (6) at (2.54902, -1.37255) {};        \node [style=whitenode] (7) at (5.29412, 3.33333) {};        \node [style=whitenode] (8) at (3.33333, 5.29412) {};        \node [style=square] (9) at (-10, 10) {};        \node [style=whitenode] (10) at (-5.29412, -3.33333) {};        \node [style=whitenode] (11) at (-3.33333, -5.29412) {};        \node [style=whitenode] (12) at (1.37255, -2.54902) {};        \node [style=square] (13) at (0.980392, -0.980392) {};        \node [style=whitenode] (14) at (2.54902, 1.37255) {};        \node [style=whitenode] (15) at (1.37255, 2.54902) {};        \node [style=whitenode] (16) at (-3.33333, 5.29412) {};        \node [style=whitenode] (17) at (-5.29412, 3.33333) {};        \node [style=whitenode] (18) at (-2.54902, -1.37255) {};        \node [style=whitenode] (19) at (-1.37255, -2.54902) {};        \node [style=square] (20) at (-0.980392, -0.980392) {};        \node [style=square] (21) at (0.980392, 0.980392) {};        \node [style=whitenode] (22) at (-1.37255, 2.54902) {};        \node [style=whitenode] (23) at (-2.54902, 1.37255) {};        \node [style=square] (24) at (-0.980392, 0.980392) {};    \end{pgfonlayer}    \begin{pgfonlayer}{edgelayer}        \draw [style=blackedge] (1) to (2);        \draw [style=blackedge] (1) to (3);        \draw [style=blackedge] (1) to (4);        \draw [style=blackedge] (1) to (5);        \draw [style=blackedge] (2) to (5);        \draw [style=blackedge] (2) to (6);        \draw [style=blackedge] (2) to (7);        \draw [style=blackedge] (3) to (7);        \draw [style=blackedge] (3) to (8);        \draw [style=blackedge] (3) to (9);        \draw [style=blackedge] (4) to (9);        \draw [style=blackedge] (4) to (10);        \draw [style=blackedge] (4) to (11);        \draw [style=blackedge] (5) to (11);        \draw [style=blackedge] (5) to (12);        \draw [style=blackedge] (6) to (12);        \draw [style=blackedge] (6) to (13);        \draw [style=blackedge] (6) to (14);        \draw [style=blackedge] (7) to (8);        \draw [style=blackedge] (7) to (14);        \draw [style=blackedge] (8) to (15);        \draw [style=blackedge] (8) to (16);        \draw [style=blackedge] (9) to (16);        \draw [style=blackedge] (9) to (17);        \draw [style=blackedge] (10) to (11);        \draw [style=blackedge] (10) to (17);        \draw [style=blackedge] (10) to (18);        \draw [style=blackedge] (11) to (19);        \draw [style=blackedge] (12) to (13);        \draw [style=blackedge] (12) to (19);        \draw [style=blackedge] (13) to (20);        \draw [style=blackedge] (13) to (21);        \draw [style=blackedge] (14) to (15);        \draw [style=blackedge] (14) to (21);        \draw [style=blackedge] (15) to (21);        \draw [style=blackedge] (15) to (22);        \draw [style=blackedge] (16) to (17);        \draw [style=blackedge] (16) to (22);        \draw [style=blackedge] (17) to (23);        \draw [style=blackedge] (18) to (19);        \draw [style=blackedge] (18) to (20);        \draw [style=blackedge] (18) to (23);        \draw [style=blackedge] (19) to (20);        \draw [style=blackedge] (20) to (24);        \draw [style=blackedge] (21) to (24);        \draw [style=blackedge] (22) to (23);        \draw [style=blackedge] (22) to (24);        \draw [style=blackedge] (23) to (24);        \draw [style=blueedge] (16) to (15);        \draw [style=blueedge] (8) to (22);        \draw [style=blueedge] (17) to (22);        \draw [style=blueedge] (16) to (23);        \draw [style=blueedge] (23) to (10);        \draw [style=blueedge] (17) to (18);        \draw [style=blueedge] (18) to (11);        \draw [style=blueedge] (10) to (19);        \draw [style=blueedge] (19) to (5);        \draw [style=blueedge] (12) to (11);        \draw [style=blueedge] (6) to (5);        \draw [style=blueedge] (12) to (2);        \draw [style=blueedge] (2) to (14);        \draw [style=blueedge] (7) to (6);        \draw [style=blueedge] (15) to (7);        \draw [style=blueedge] (8) to (14);        \draw [style=blueedge] (22) to (21);        \draw [style=blueedge] (15) to (24);        \draw [style=blueedge] (24) to (13);        \draw [style=blueedge] (21) to (20);        \draw [style=blueedge] (20) to (12);        \draw [style=blueedge] (13) to (19);        \draw [style=blueedge] (23) to (20);        \draw [style=blueedge] (24) to (18);        \draw [style=blueedge] (13) to (14);        \draw [style=blueedge] (6) to (21);        \draw [style=blueedge] (9) to (10);        \draw [style=blueedge] (17) to (4);        \draw [style=blueedge] (4) to (5);        \draw [style=blueedge] (11) to (1);        \draw [style=blueedge] (1) to (7);        \draw [style=blueedge] (3) to (2);        \draw [style=blueedge] (9) to (8);        \draw [style=blueedge] (16) to (3);        \draw [style=blueedge, bend left=60, looseness=2.25] (4) to (3);        \draw [style=blueedge, bend left=60, looseness=2.25] (9) to (1);    \end{pgfonlayer}\end{tikzpicture}

It looks the following:enter image description here


Viewing all articles
Browse latest Browse all 186

Trending Articles