Skip to main content
fixed issue (2)
Source Link
b3m2a1
  • 47.1k
  • 3
  • 95
  • 244

Then I pulled out the note extraction task and used boxExpr everywhere there was RowBox[{body}]. Note that I'm using the styleNote function I defined in my suggestion at the bottom. To deal with issue (2) mentioned in the comments we also apply boxExpr in the no-note case.

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($$note1 = Null;
      note), 
    e_ :> ($note1 = Null; $note2 = Null; eboxExpr@e)}];
applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $$Line},
   HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[TraditionalForm@Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($$note1 = Null;
      note), 
    e_ :> ($note1 = Null; $note2 = Null; eboxExpr@e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $$Line},
   HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $$placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  TraditionalForm@Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;

Then I pulled out the note extraction task and used boxExpr everywhere there was RowBox[{body}]. Note that I'm using the styleNote function I defined in my suggestion at the bottom.

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];
applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;

Then I pulled out the note extraction task and used boxExpr everywhere there was RowBox[{body}]. Note that I'm using the styleNote function I defined in my suggestion at the bottom. To deal with issue (2) mentioned in the comments we also apply boxExpr in the no-note case.

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), 
    e_ :> ($note1 = Null; $note2 = Null; boxExpr@e)}];
applyFormatting[out_] :=
  With[{line = $Line},
   HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  TraditionalForm@Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), 
    e_ :> ($note1 = Null; $note2 = Null; boxExpr@e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  TraditionalForm@Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;
deleted 36 characters in body
Source Link
b3m2a1
  • 47.1k
  • 3
  • 95
  • 244
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted]formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted]formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted]formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted]formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted], r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted], r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, formatted, r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, formatted}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{formatted, r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;
added 66 characters in body
Source Link
b3m2a1
  • 47.1k
  • 3
  • 95
  • 244
applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{$note1, Pane[formatted], $note2r1, Pane[formatted], r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $$placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted], r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;
applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{$note1, Pane[formatted], $note2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted], r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;
applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted], r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];
$note1 = Null;
$note2 = Null;
$note3 = Null;

$outputStyles =
  <|
   "Default" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "Before" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     },
   "After" -> {
     Blue,
     15,
     Italic,
     FontFamily -> "Times"
     }
   |>;

boxExpr[body_] :=
  
  RowBox@{"Replace", "[", "\"thisIsJustATag\"", ";", body, ",", 
    "Null", "->", "\"\"", "]"};
styleNote[note_, style_] :=
  
  Style[ToExpression@note, 
   Sequence @@ Lookup[$outputStyles, style, $outputStyles["Default"]]];

extractNotes[boxes_] := 
  Replace[boxes, {RowBox[{note1_String?(StringMatchQ[#, "\"*\""] &), 
       ";", body__, ";", 
       note2_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note1 = 
       styleNote[note1, "Before"]; $note2 = 
       styleNote[note2, "After"];
      boxExpr@body), 
    RowBox[{body__, ";", 
       note_String?(StringMatchQ[#, "\"*\""] &)}] :> ($note2 = 
       styleNote[note, "After"];
      $note1 = Null;
      boxExpr@body), 
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), ";", 
       body__}] :> ($note1 = styleNote[note, "After"];
      $note2 = Null;
      boxExpr@body),
    RowBox[{note_String?(StringMatchQ[#, "\"*\""] &), 
       ";"}] :> ($note3 = styleNote[note, "Neither"];
      $note2 = Null; $note1 = Null;
      note), e_ :> ($note1 = Null; $note2 = Null; e)}];

applyFormatting[out_] :=
  With[{line = $Line},
   TraditionalForm@HoldForm[In[line] = $placeHolder] /.
      
      DownValues[In] /. {
      $placeHolder -> out,
      HoldPattern[
        Replace[CompoundExpression["thisIsJustATag", expr_], 
         Null -> ""]] :> expr
      }
    /. {
     HoldPattern[a_ = ""] :> a,
     HoldPattern[a_ = a_] :> a,
     HoldPattern[a_ = HoldForm[a_]] :> a,
     HoldPattern[(c : (a_ = b_)) = b_] :> c,
     HoldPattern[(a_ = b_) = c_] :> HoldForm[a = b = c]
     }
   ];
addNotes[formatted_] :=
  Switch[{$note1, $note2, $note3},
   {Null, Null, Except@Null},
   With[{r = $note3}, $note3 = Null; r],
   {Except@Null, Except@Null, _},
   With[{r1 = $note1, r2 = $note2}, $note1 = $note2 = Null; 
    Row[{r1, Pane[formatted], r2}, Spacer[5]]
    ],
   {Except@Null, _, _},
   With[{r = $note1}, $note1 = Null;
    Row[{r, Pane[formatted]}, Spacer[5]]
    ],
   {_, Except@Null, _},
   With[{r = $note2}, $note2 = Null;
    Row[{Pane[formatted], r}, Spacer[5]]
    ],
   _,
   formatted
   ];

$PreRead = extractNotes;
$PrePrint = addNotes@*applyFormatting;
added 7 characters in body
Source Link
b3m2a1
  • 47.1k
  • 3
  • 95
  • 244
Loading
Completed answer
Source Link
b3m2a1
  • 47.1k
  • 3
  • 95
  • 244
Loading
added 1970 characters in body
Source Link
b3m2a1
  • 47.1k
  • 3
  • 95
  • 244
Loading
Source Link
b3m2a1
  • 47.1k
  • 3
  • 95
  • 244
Loading