I am making a directed graph, but Mathematica keeps trying to evaluate my labels. I know that I can just use " ", but I have a lot of input. Is there a way to turn evaluation off?

LayeredGraphPlot[{"C1.4.4" -> "T1.5.7", "1.23" -> "L1.5.4", 
  "P1.4.3" -> "C1.4.4", "P1.4.3" -> "C1.1.4", "P1.1.9" -> "P1.5.2", 
  "P1.1.9" -> "L1.5.3", "P1.5.2" -> "T1.5.7", "L1.5.3" -> "T1.5.7", 
  "L1.5.4" -> "T1.5.7", "P1.5.5" -> "T1.5.7", "L1.4.6" -> "L1.4.7"}, 
 VertexLabeling -> True]
  • $\begingroup$ Welcome. Your code works for me as expected. What is the evaluation looking like? $\endgroup$
    – Matariki
    Jul 14 '12 at 10:46
  • $\begingroup$ In principles you could use Map to wrap ToString around the expressions at each side of each rule, but if you take the quote marks off, Mathematica interprets "C1.4.4" as C1 * 0.4 * 0.4, i.e. 0.16 C1. There should be some version of HoldForm that works but it seems almost impossible to define a function that performs the transformation without releasing Hold somewhere along the line. $\endgroup$
    – Verbeia
    Jul 14 '12 at 11:06
  • $\begingroup$ Welcome to Mathematica.SE, by the way! Please consider registering your account so that the upvotes you get on any future questions and answers will be added to the ones you received for this one. $\endgroup$
    – Verbeia
    Jul 14 '12 at 23:11

Not sure how robust this is but instead of putting quotation marks around the individual vertex labels, you could do something like

split[rulelst_String] := Rule @@@ Partition[
   StringTrim /@ StringSplit[rulelst, {"->", "\[Rule]", ",", "{", "}"}], 2]

 split["{C1.4.4 -> T1.5.7, 1.23 -> L1.5.4, P1.4.3 -> C1.4.4, P1.4.3 -> 
C1.1.4, P1.1.9 -> P1.5.2, P1.1.9 -> L1.5.3, P1.5.2 -> T1.5.7, L1.5.3 -> 
T1.5.7, L1.5.4 -> T1.5.7, P1.5.5 -> T1.5.7, L1.4.6 -> L1.4.7}"], 
 VertexLabeling -> True]

Mathematica graphics


If you take the quote marks out, Mathematica interprets "C1.4.4" as C1 * 0.4 * 0.4, i.e. 0.16 C1.

This works for a single label, but doesn't if you create a function that operates on different bits of rules.

StringJoin @@ StringReplace[(ToString /@ First[List @@@ Hold[C1 .4 .4]]), 
  "0" -> ""]

(* returns string "C1.4.4" *)

But it should point you in the right direction.


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.