0

The top function manages several sub-functions which set visual properties over a list of Options.

Inside stylerules the function deconstruct Extract-s parts of a Rule and calls string or symbol for applying appropriate Style-s then reconstructs them.

A whole set of Rule-s is processed by Map-ping deconstruct onto set. Style properties are generated or selected based on user-input for stylerules[set_List, keystyle_, valstyle_, strstyle_] (option rules, keys style, values style, strings style ). For any style argument, user may input Automatic vs True vs any custom Style properties in a List vs None or False (last two default to generic Style-s)

I'm having difficulties with the output. Style-s are not evaluating, which also prevents the deconstructed Rules from reconstructing.

Don't worry, the code below looks a lot worse than it is. I promise that it is very easy to follow. I believe the problem areas to focus on are the inner functions string and symbol which may be scoped wrongly. I tried other scopes which did not help, including dropping initialized variables and constants into the main body. Please help. Thank you.

To reiterate:

  1. Why are Style-s not evaluating and how to remedy this.

  2. I just noticed that some strange out-of-place Null-s are also appearing in the resulting rules. I don't know why.


(* PrettyOptions 0.7 Prototype 1 Runs - 2 *)

ClearSystemCache[]; ClearAll[set, stylerules];

(* the Echo's are temporary *) stylerules[set_List, keystyle_, valstyle_, strstyle_] := With[{ rules = {x_ /; MatchQ[x, Automatic | True | False | None | Null]} -> Nothing, keys = DeleteDuplicates[ Flatten[Join[{Bold, FontFamily -> "Courier New"}, {keystyle}]] //. rules], vals = DeleteDuplicates[ Flatten[Join[{Plain, FontFamily -> "Arial"}, {valstyle}]] //. rules], strs = DeleteDuplicates[ Flatten[Join[{Bold, FontFamily -> "Arial"}, {strstyle}]] //. rules] }, Echo[keys, "keys [Rule]"]; Echo[vals, "vals [Rule]"]; Echo[strs, "strs [Rule]"]; deconstruct[rule_, keysty_, valsty_, strsty_] := With[{ nbcol = Options[EvaluationNotebook[], FontColor], comcol = RGBColor[.75, 1, .75], altcol = Lighter[LightBlue], a = Extract[rule, {1}], b = Extract[rule, {2}], fn = Head[rule] }, x = If[StringQ[a], string[a, strs, strsty], symbol[a, keys, keysty]]; y = If[StringQ[b], string[b, strs, strsty], symbol[b, vals, valsty]]; Return[fn[x, y]]; ]; symbol[sym_, ops_, style_] := If[(ListQ[style] || ColorQ[style]), Style[sym, ops], Which[ MatchQ[style, Automatic], Evaluate[Style[sym, comcol, ops]], TrueQ[style], Style[sym, altcol, ops], True, Style[sym, ops] ] ]; string[str_, ops_, style_] := If[(ListQ[style] || ColorQ[style]), Style[str, ops], Which[ MatchQ[style, Automatic], Evaluate[Style[InputForm[str], Gray, ops]], TrueQ[style], Style[str, comcol, ops], True, Style[str, ops] ]; ]; Echo[Map[deconstruct[#, keystyle, valstyle, strstyle] &, set]] ]; stylerules[set_] := set;


(*  arbritary data *)
set = Options[TestCases] = {TestCase1 :> 111111111, 
    TestCase2 :> "string222", "TestCase3" -> Symbol333, 
    TestCase4 :> Symbol444, "TestCase5" -> "string555"};

(* a few typical use cases, none worked *)
stylerules[set, Automatic, Automatic, Automatic];
stylerules[set, True, True, True];
stylerules[set, None, None, None];
stylerules[set, Automatic, True, Automatic];

(* this test case works *)
stylerules[
  set, {Blue, FontFamily -> "Times New Roman"}, {Magenta, 
   FontFamily -> "Times New Roman"}, {Orange, Plain, Italic}];
Jules Manson
  • 2,457
  • 11
  • 19
  • 1
    The main issue is that With[{a=1,b=2a},{a,b}] doesn't do what you think it does, see e.g. this question for some context – Lukas Lang Dec 15 '22 at 19:30
  • @LukasLang i fixed the typesetting in the code view. Initialization in the scoping does appear like it would be the cause but im still a little confused. would you mind having a 2nd look and be more detailed? thank you. – Jules Manson Dec 15 '22 at 19:48
  • 2
    What I am saying is that e.g. for your keys = ... line in your stylerules definition will not work as is, since rules is not replaced with the value from the line above. You'll have to use nested Withs or similar, as discussed in the linked question. (So With[{rules = ...}, With[{keys = ...}, ... ]] – Lukas Lang Dec 15 '22 at 21:01
  • @LukasLang that was more clear. thank you. – Jules Manson Dec 15 '22 at 21:27
  • 1
    Does this answer your question? How to avoid nested With[]? – MarcoB Dec 16 '22 at 16:17
  • @MarcoB only halfway but still having difficulty getting it to work. thanks anyway bro you helped me a lot before. – Jules Manson Dec 16 '22 at 17:42

0 Answers0