6
$\begingroup$

The Graph command displays edge labels in TraditionalForm. I instead want to use StandardForm.

I tried

EdgeLabelStyle -> Directive[StandardForm, Background -> White], but of course it does not work.

So how to do it, acting on EdgeLabelStyle?

Edit: Comment to @Sjoerd examples Your Binomial example is somewhat misleading, since Binomial has an intrinsic TraditionalForm version. What we are seeing in the graph is a sort of hybrid of Standardform (for n and k) and TraditionalForm for the parenthesis (I think). This is the proof:

Mathematica graphics

As you see, even though I used braces in the notation, I still get the parenthesis in the graph (from the internal TraditionalForm), but the n, k are indeed in StandardForm.

This shows that the "StandardForm" in EdgelabelStyle does something, but it is not enough. Perhaps it is possible to play with BaseStyle?

The same thing happens if you use MakeBoxes (without ToString).

$\endgroup$

4 Answers 4

6
$\begingroup$

The style names should be between quotes (this is stated in the Style doc page, under the 'More Information' section)

edges = {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1};
GraphicsRow[{
  Graph[edges, EdgeLabels -> Exp[I x/10], 
         EdgeLabelStyle -> Directive["StandardForm", Red, 30], 
         ImagePadding -> 15],
   Graph[edges, EdgeLabels -> Exp[I x/10], 
         EdgeLabelStyle -> Directive["TraditionalForm", Red, 30], 
         ImagePadding -> 15]
}]

Mathematica graphics


EDIT

Based on your comments I tried to work with Notation and MakeBoxes.

Notation doesn't seem to cause problems:

Mathematica graphics

MakeBoxes seem to work if you use ToString:

gplus /: MakeBoxes[gplus[x_, y_, n_], StandardForm] := 
 RowBox[{MakeBoxes[x, StandardForm], 
   SubscriptBox["\[CirclePlus]", MakeBoxes[n, StandardForm]], 
   MakeBoxes[y, StandardForm]}]

edges = {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1};
GraphicsRow[{
  Graph[edges, 
    EdgeLabels -> ToString[gplus[a, b, c], StandardForm], 
    EdgeLabelStyle -> Directive["StandardForm", Red, 30], 
    ImagePadding -> 30],
  Graph[edges, 
    EdgeLabels -> ToString[gplus[a, b, c], StandardForm], 
    EdgeLabelStyle -> Directive["TraditionalForm", Red, 30], 
    ImagePadding -> 30]
  }]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ This is very good!....but now there is another problem. For some reason, StandardForm is indeed displayed, but my custom notation (created with Notation and MakeBoxes[....StandardForm]) are no longer working. I also tried "Output" and "Text", to no avail. Any suggestions? $\endgroup$
    – magma
    Aug 1, 2012 at 20:35
  • $\begingroup$ @magma Tried something (see edits). If this doesn't work for you, you probably should be more specific in how you use Notation and MakeBoxes. $\endgroup$ Aug 1, 2012 at 21:14
  • $\begingroup$ thank you @Sjoerd. Please read my edited question. $\endgroup$
    – magma
    Aug 2, 2012 at 2:07
3
$\begingroup$

You can use StandardForm to wrap the labels:

 edges = {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1};
 GraphicsRow[{Graph[edges, EdgeLabels -> Exp[I x/10], 
 EdgeLabelStyle -> Directive[Red, 30], ImagePadding -> 15], 
 Graph[edges, EdgeLabels -> TraditionalForm[Exp[I x/10]], 
 EdgeLabelStyle -> Directive[Red, 30], ImagePadding -> 15], 
 Graph[edges, EdgeLabels -> StandardForm[Exp[I x/10]], 
 EdgeLabelStyle -> Directive[Red, 30], ImagePadding -> 15]}]

enter image description here

$\endgroup$
1
  • $\begingroup$ I tried that before asking here, but I really do not wish to mess up with the labels $\endgroup$
    – magma
    Aug 2, 2012 at 2:06
3
$\begingroup$

I personally prefer using String for labels instead of evaluable expressions (in case they are messed up because of incautious assignments).

edges = {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1};

Graph[edges, EdgeLabels -> {
                  1 \[UndirectedEdge] 2 -> Style[
                               ToString[HoldForm[
                                    Integrate[Exp[(I*x)/10], {x, \[Alpha], \[Beta]}]
                                   ], StandardForm],
                               FontFamily -> "Times", Blue, 15, Bold],
                  2 \[UndirectedEdge] 3 -> "text",
                  3 \[UndirectedEdge] 1 -> ToString[HoldForm[
                               Integrate[Exp[(I*x)/10], {x, \[Alpha], \[Beta]}]
                                            ], TraditionalForm]
                           },
             EdgeLabelStyle -> Directive[Red, 30], ImagePadding -> 15]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ This is not good for me Silvia. In my case case the label are "the message", so I want them to evaluate and stay untouched $\endgroup$
    – magma
    Aug 1, 2012 at 19:58
  • $\begingroup$ @magma In that case maybe you could remove the HoldForm wrapper, so the expression will be evaluated when generating the label content. Converting it to String is aimed at preventing the generated graph being modified by latter evaluation. eg. The graph is wrapped in a Dynamic, then a latter assignment, say x = 0, may cause Exp[I x/10] becoming 1. But if this behavior is indeed what you need, then using String will be inappropriate. $\endgroup$
    – Silvia
    Aug 1, 2012 at 20:09
2
$\begingroup$

I think you can just use the FormatType option of Graph. First, your notation:

enter image description here

Then, the graph:

Graph[{1<->2, 2<->3, 3<->1}, EdgeLabels->Binomial[n,k], FormatType->StandardForm]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you Carl. This was a very old question of mine. I will check your answer tomorrow (if I can still find the problem :-)) $\endgroup$
    – magma
    Oct 3, 2018 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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