13
$\begingroup$

I am investigating the use of the Notation package facilities to create strict the equivalence between pretty-printed symbols such as OverBar[SuperStar[af]] that formats as $\overline{af^*}$, and easy to type (but hard to read) symbols such as afstarbar.

My issue is that I fail at making two such symbols strictly equivalent.

I would like that, after typing Notation[ParsedBoxWrapper[\[Beta]]\[DoubleLongLeftRightArrow]ParsedBoxWrapper[beta]], I get the following behavior:

 In[] beta = 3 
Out[] 3

 In[] \[Beta] 
Out[] 3

 In[] \[Beta] = 0 
Out[] 0

 In[] beta     
Out[] 0

 In[] beta = 4 
Out[] 4

In[] Information[beta] 
Out[] \[Beta]
      \[Beta] = 4

It is to be noted that with Mathematica 8.0.0.0, the Notation behaves differently on Windows and Linux and on Mac, as I figured out by starting this reddit discussion, but on all platforms, I could not manage to get assignments to one symbol propagate to the other symetrically.

The behavior that I get instead of the above is:

 In[] beta = 3 
Out[] 3

 In[] \[Beta] 
Out[] 3

 In[] \[Beta] = 0 
Out[] 0

 In[] beta     
Out[] 3

 In[] beta = 4
Out[] 4

In[] Information[beta] 
Out[] \[Beta]
      \[Beta] = 4

Is there a way to get the scrict and robust equivalence between symbols instead of the assymetrical behavior shown above?

$\endgroup$
3
  • $\begingroup$ I see both beta and \[Beta] being used in the above session. Is this a copy-and-paste issue? $\endgroup$ Jan 18, 2012 at 16:40
  • 1
    $\begingroup$ I do not think so. The OP want to type "beta" to input the symbol "[Beta]". In general the OP wants to type simple strings in order to input more complex strings. $\endgroup$
    – magma
    Jan 18, 2012 at 17:15
  • $\begingroup$ @magma Exactly, thanks for clarifying that for me. $\endgroup$
    – agravier
    Jan 19, 2012 at 4:11

2 Answers 2

5
$\begingroup$

You appear to be looking for the functionality of $PreRead:

$PreRead = # /. "beta" -> "\[Beta]" &;

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ Hotness! Thanks, I'll give PreRead a try ASAP. $\endgroup$
    – agravier
    Feb 2, 2012 at 18:10
  • $\begingroup$ Some feedback: $PreRead = ReplaceAll[#, "afstarbar" -> OverBar[SuperStar["af"]]] & is accepted but then afstarbar gives Syntax::sntxi: Incomplete expression; more input is needed ., so I need to use $PreRead = ReplaceAll[#, "afstarbar" -> OverscriptBox[SuperscriptBox["af", "*"], "_"]] & where the _ is too low, or the same with [HorizontalLine], that makes a too thick bar. $\endgroup$
    – agravier
    Feb 3, 2012 at 14:38
  • $\begingroup$ In a comment, @Rojo proposed me a neat solution: MakeBoxes[OverBar[x_], TraditionalForm] := OverscriptBox[#, "_", DiacriticalPositioning -> False] &[ MakeBoxes[x, TraditionalForm]] ; Now this plus $PreRead = ReplaceAll[#, "afstarbar" -> OverscriptBox[SuperscriptBox["af", "*"], "_"]] & works great, now my next goal is to let OverscriptBox[SuperscriptBox["af", "*"], "_"]] be a symbol to which I can assign values like to afstarbar, which I think I just managed to do using Symbolize from the ``Notation``` package: Symbolize[ParsedBoxWrapper["afstarbar"]]. I'm testing it more right now :P $\endgroup$
    – agravier
    Feb 3, 2012 at 14:39
  • 1
    $\begingroup$ @agravier I should have pointed out that $PreRead is operating on Box form, and needs to return an expression also in Box form. This is often most conveniently done with ToBoxes: $PreRead = ReplaceAll[#, "afstarbar" -> ToBoxes@OverBar@SuperStar@"af"] & Using that you should be able to assign to afstarbar. $\endgroup$
    – Mr.Wizard
    Feb 3, 2012 at 17:53
  • 1
    $\begingroup$ @agravier Related to what you are doing is the Format function, and the MakeBoxes[ ... ] := that you have already been shown. I suggest you look carefully at both of these. $\endgroup$
    – Mr.Wizard
    Feb 3, 2012 at 17:54
6
$\begingroup$

What you are really looking for is InputAutoReplacements.

If I understand your question correctly you are looking for a simple/quick way to input mildly complex strings of characters. In your example you want to find an easy way to input $\beta$. But there is no point in creating another symbol beta, since the 2 symbols are meant to always be "equivalent" as you say. So you need to create an InputAutoReplacements option for the notebook.

This creates a new notebook. As soon as you type beta and then something else like space or "[" or an operation symbol you will get $\beta$.

nb = CreateDocument[{}, 
       InputAutoReplacements -> {"beta" -> "\[Beta]"}];

In case you just want to add this feature to your current notebook, you can use the Option Inspector, change the InputAutoReplacements option and save the notebook, to fix the changes.

A somewhat alternative route would be to use the AddInputAlias template from the Notation package. At the end of the process you would have to type: Esc beta Esc to get $\beta$. Obviously in this simple case it does not make much sense.

To recap: if you do not want to use Esc, use the InputAutoReplacements system, if do you want to use Esc, use the AddInputAlias system.

Why doesn't the Notation template work in this case? Well, first the LHS and the RHS of the Notation template have different meanings (external form versus internal form), so even if they are linked by a DoubleLongLeftRightArrow they are NOT equivalent in a mathematical sense. In your case you want to parse beta and get $\beta$ which displays as $\beta$, so perhaps this should work better:

Notation[ParsedBoxWrapper["beta"] \[DoubleLongRightArrow] 
  ParsedBoxWrapper["\[Beta]"]]

And indeed it works as long as you leave beta without own- or down- values. But this route is not really what you want to do I guess.

$\endgroup$
9
  • 1
    $\begingroup$ This is good, but it does not work if, for example, there is an existing definition like f[x_]:=beta^x. Entering beta=3 (which properly replaces) and then f[2] does not give the desired 9, but still just beta^2. (By the way, though I'm pointing this out, I don't know how to get around this) $\endgroup$
    – William
    Jan 18, 2012 at 18:07
  • 1
    $\begingroup$ If you type (and evaluate) the definition of f in a notebook whose InputAutoReplacements option is appropriately set, then your approach works. If the definition already exists (e.g. in some other notebook where the InputAutoReplacements option was not appropriately set when the definition was entered), then this approach does not work. Please also note that re-evaluating the cell containing the definition doesn't help, even if InputAutoReplacements is then set appropriately; it would be necessary to actually re-type [parts of] the definition with InputAutoReplacements appropriately set. $\endgroup$
    – William
    Jan 19, 2012 at 1:58
  • 1
    $\begingroup$ Thanks for this solution magna. William makes a valid point, but there is another issue: can you get the Mathematica to reparse an input line for auto-replacements? One of the reasons why I want to have this kind of "symbol a is identical to symbol b" equivalence is for pasting equations from other programs where the symbols are just ugly variable names. And when I set up your solution with InputAutoReplacements, pasted symbols are not auto-replaced. Any keyboard shortcut to tell Mathematica to re-parse the input field? $\endgroup$
    – agravier
    Jan 19, 2012 at 4:26
  • 1
    $\begingroup$ @agravier Then this is a different problem/setting from what I understood. I suggest you ask a separate question describing exactly what is your setting (Matlab import) and add perhaps some concrete examples of what you want to achieve. In general (if I understand correctly), you might end up making a list of Rule statements which will replace the original variables. You also need a Matlab-MMA translator and perhaps some Hold/HoldForm command on the translated formulas. $\endgroup$
    – magma
    Jan 19, 2012 at 14:16
  • 1
    $\begingroup$ @agravier I suggest you simply ask another question, giving the complete context of your situation (matlab import, frequent updates, each update in same/different notebook, ect).Also add some simple examples (1,2 matlab formulas and show how you would like them to be transformed). In general, MMA can automatize "any" workflow. But you need to explain your (desired) workflow, if you ask for help $\endgroup$
    – magma
    Jan 19, 2012 at 14:39

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.