4
$\begingroup$

Consider following command

Cases[ToBoxes[Block[{Identity}, Graph[{1 <-> 2, 2 <-> 3, 1 -> 3, 2 -> 1, 2 -> 3, 3 -> 1}, 
          EdgeWeight -> Identity /@ {{0, 0}, {0, 0}, {1, 0}, {0, 1}, {1, 0}, {0, 1}}]]],
      _GraphicsComplexBox, Infinity][[1, 2]]

with its output

{{Arrowheads[Medium], 
  Directive[Opacity[0.7], Hue[0.6, 0.7, 0.5]], {Arrowheads[0.], 
   ArrowBox[BezierCurveBox[{1, {0.606341, 0.514848}, 2}], 0.0127299]},
   ArrowBox[BezierCurveBox[{1, {0.10576, 0.352183}, 3}], 0.0127299], 
  ArrowBox[BezierCurveBox[{2, {0.499413, 0.70158}, 3}], 
   0.0127299], {Arrowheads[0.], 
   ArrowBox[BezierCurveBox[{2, {0.500581, 1.03046}, 3}], 0.0127299]}, 
  ArrowBox[BezierCurveBox[{2, {0.890575, 0.349397}, 1}], 0.0127299], 
  ArrowBox[BezierCurveBox[{3, {0.391162, 0.515612}, 1}], 
   0.0127299]}, {Directive[Hue[0.6, 0.2, 0.8], 
   EdgeForm[Directive[GrayLevel[0], Opacity[0.7]]]], 
  DiskBox[1, 0.0127299], DiskBox[2, 0.0127299], 
  DiskBox[3, 0.0127299]}}

I would like to reformat the output to something like

{
    {
        Arrowheads[Medium],
        Directive[Opacity[0.7],
        Hue[0.6, 0.7, 0.5]],
        {
            Arrowheads[0.],
            ArrowBox[BezierCurveBox[{1, {0.606341, 0.514848}, 2}], 0.0127299]
        },
        ArrowBox[BezierCurveBox[{1, {0.10576, 0.352183}, 3}], 0.0127299], 
        ArrowBox[BezierCurveBox[{2, {0.499413, 0.70158}, 3}], 0.0127299],
        {
            Arrowheads[0.], 
            ArrowBox[BezierCurveBox[{2, {0.500581, 1.03046}, 3}], 0.0127299]
        }, 
        ArrowBox[BezierCurveBox[{2, {0.890575, 0.349397}, 1}], 0.0127299], 
        ArrowBox[BezierCurveBox[{3, {0.391162, 0.515612}, 1}], 0.0127299]
    },
    {
        Directive[Hue[0.6, 0.2, 0.8],
        EdgeForm[Directive[GrayLevel[0], Opacity[0.7]]]], 
        DiskBox[1, 0.0127299],
        DiskBox[2, 0.0127299], 
        DiskBox[3, 0.0127299]
    }
}

(ideally short lists are not broken)

Is there a way to do it in Mathematica or on the command line similar to GNU Indent or other available tools for languages like json, lisp, python list, etc. such as https://prettier.io ?

$\endgroup$
5
  • 3
    $\begingroup$ <<GeneralUtilities`; PrettyForm @ expr or mathematica.stackexchange.com/q/2809/5478 $\endgroup$
    – Kuba
    Commented Apr 18, 2018 at 8:51
  • $\begingroup$ Thanks for your comment. Why did you choose to add this only as a comment? I don't see PrettyForm in the answers to the referenced question. $\endgroup$
    – Hotschke
    Commented Apr 18, 2018 at 8:54
  • $\begingroup$ Because I think it is a duplicate but PrettyForms needs to be added the second answer maybe. $\endgroup$
    – Kuba
    Commented Apr 18, 2018 at 9:05
  • $\begingroup$ I can agree with being more or less a duplicate. Even though I am only interested in the list structure not the tree of other functions. I will add your comment with the actual output as an answer to illustrate how PrettyForm formats my sample (e.g. PrettyPrint inserts also shorthands such as @) $\endgroup$
    – Hotschke
    Commented Apr 18, 2018 at 9:12
  • $\begingroup$ As far as I have tested, the source code formatting feature of the Wolfram Workbench (Eclipse IDE Plugin) cannot format the given output. $\endgroup$
    – Hotschke
    Commented Apr 18, 2018 at 9:35

1 Answer 1

3
$\begingroup$

PrettyForm from <<GeneralUtilities

(applying comment of Kuba to given sample)

Cases[ToBoxes[Block[{Identity}, Graph[{1 <-> 2, 2 <-> 3, 1 -> 3, 2 -> 1, 2 -> 3, 3 -> 1}, 
          EdgeWeight -> Identity /@ {{0, 0}, {0, 0}, {1, 0}, {0, 1}, {1, 0}, {0, 1}}]]],
      _GraphicsComplexBox, Infinity][[1, 2]]
<< GeneralUtilities`; PrettyForm@%

This gives

{
    {
        Arrowheads @ Medium,
        Directive[Opacity @ 0.7, Hue[0.6, 0.7, 0.5]],
        {
            Arrowheads @ 0.,
            ArrowBox[
                    BezierCurveBox @ {1, {0.6063408473035717, 0.5148479095999602}, 2},
                    0.012729919145102353
                ]
        },
        ArrowBox[
            BezierCurveBox @ {1, {0.1057602447225488, 0.3521832484933547}, 3},
            0.012729919145102353
        ],
        ArrowBox[
            BezierCurveBox @ {2, {0.49941304589520347, 0.7015802720433176}, 3},
            0.012729919145102353
        ],
        {
            Arrowheads @ 0.,
            ArrowBox[
                    BezierCurveBox @ {2, {0.5005806025810231, 1.0304596775108001}, 3},
                    0.012729919145102353
                ]
        },
        ArrowBox[
            BezierCurveBox @ {2, {0.8905750124163706, 0.3493970235499975}, 1},
            0.012729919145102353
        ],
        ArrowBox[
            BezierCurveBox @ {3, {0.3911619665211691, 0.5156117679108979}, 1},
            0.012729919145102353
        ]
    },
    {
        Directive[Hue[0.6, 0.2, 0.8],
            EdgeForm @ Directive[GrayLevel @ 0, Opacity @ 0.7]
        ],
        DiskBox[1, 0.012729919145102353],
        DiskBox[2, 0.012729919145102353],
        DiskBox[3, 0.012729919145102353]
    }
 }

As you can see this

  • breaks functions in general not only list constructs {} and
  • represents floats in double precision.

Therefore the output is longer and not as compact as I hoped for.

BTW it also inserts prefix notation @ and possibly in other situations more shorthands.

$\endgroup$

Your Answer

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

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