Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

x\[Prime] looks like $x_{'}$, ugly right? Is there a way to make a symbol with prime to look like $x'$?


That's what I'm trying right now:

Notation[ x_', Primed[x_]]
Primed[x_] := Symbol[SymbolName[x] <> "\[Prime]"]

But how then I use it in patterns? If I do this:

f[q_, q':_] := q + q'

Weird syntax errors appear:

Syntax::sntxf: "T[r_," cannot be followed by "q':_]".
Syntax::tsntxi: "q':_" is incomplete; more input is needed.
Syntax::sntxi: Incomplete expression; more input is needed .

If we replace q':_ with Pattern[q',_] another error:

Pattern::patvar: "First element in pattern Pattern[q',_] is not a valid pattern name. "

But Head[q'] returns Symbol and Pattern requires a Symbol as first parameter.

What is wrong?

share|improve this question
    
Why not type x'? – R. M. Oct 3 '12 at 17:33
    
@rm-rf Because it will be a derivative then and not a symbol. – swish Oct 3 '12 at 17:44
    
Ah, you want to be able to do things like $x^\prime=3$, then? – J. M. Oct 3 '12 at 17:48
    
@J.M. Yes, but without Needs["Notation`"];Symbolize[x']. – swish Oct 3 '12 at 17:52
1  
Very closely related: How to Clear variables with apostrophe? There, I suggest using a different unicode glyph that looks better. – Jens Oct 20 '14 at 15:40
up vote 7 down vote accepted

As mentioned in my comment to the question, I think the best solution is to use a Unicode character (see also the answer by The Vee). Here is a modified version of my earlier answer:

SetOptions[EvaluationNotebook[], 
 InputAliases -> 
  DeleteDuplicates@
   Join[{"'" -> FromCharacterCode[700]}, 
    InputAliases /. 
      Quiet[Options[EvaluationNotebook[], InputAliases]] /. 
     InputAliases -> {}]]

eʼ = 1

(* ==> 1 *)

eʼʼ = 2

(* ==> 2 *)

eʼ + eʼʼ

(* ==> 3 *)

The only modification is that I defined the shortcut so that you can now really enter the prime by typing the actual prime symbol on the keyboard, just surrounded by escape, i.e., Esc'Esc.

As you can see in the example, the input alias immediately converts this to primed variable names, which can be used like any other symbol.

share|improve this answer

This is a method that is similar to Oleksandr R.'s answer:

The following tells Mathematica to render Primed symbols in superscripted box form:

MakeBoxes[Primed[x_], StandardForm] := SuperscriptBox[ToBoxes[x], "\[Prime]"]

And this tells Mathematica to parse a superscripted box structure internally as Primed, and not as Derivative:

MakeExpression[SuperscriptBox[x_, "\[Prime]"], form_] := 
  MakeExpression[RowBox[{"Primed", "[", x, "]"}], form]

Now $a^\prime$ inputed using the keyboard sequence [A]-([CTRL]+[6])-[ESC]-[']-[ESC] is understood as Primed[a]:

enter image description here


To use this as a pattern, you need to do it like this:

f[q_, Primed[q_]] := q + Primed[q]

Let's try it out:

enter image description here

share|improve this answer

This is following Jens's suggestion to use a different Unicode glyph, but different from the answer linked in the corresponding comment. We can use Unicode directly, so let's just find a letter-like modifier glyph that looks good. A quick search for "prime" gives a nice solution in MODIFIER LETTER PRIME. You can type it using the notation \:02b9 which renders as ʹ, or just copy and paste the character itself:

enter image description here

This does not require any packages or prior code.

Closely related \:02c8 and \:02ca are also options and work just as well, the choice is a matter of preference.

share|improve this answer
    
Thanks for mentioning my answer (+1) - I think I'll re-post it here with a modification that would make typing even easier... – Jens Apr 1 at 1:27
    
@Jens Oh, mine is more similar than I originally thought. Only now I noticed that your constant 700 is simply 0x2bc and thus FromCharacterCode[700] = \:02bc, just a different one from the same list. I did not understand that the code is there just to simplify the keyboard input, I thought it was using some sort of invisible surrogate. – The Vee Apr 1 at 1:33

In Mathematica 10 using

<< Notation`
Notation[x' => xPrime]

seems to work to "disconnect" x' from its meaning as a derivative. (Note- the Notation text used here represents entering using the Notation Palette. Mathematica interprets this to

Notation[ParsedBoxWrapper[
   RowBox[{"x", "'"}]] \[DoubleLongRightArrow] ParsedBoxWrapper["xPrime"]
]

but no reason to do that by hand )

share|improve this answer
    
Trying the first two lines, I get a Notation::noexbtag error. (V10.0.1) – Michael E2 Oct 20 '14 at 13:35
1  
NB: This only worked for me when I reproduced the second line with the Notation Palette. – evanb Oct 20 '14 at 17:50

This is an unashamed variation on @Oleksandr R's post but prompted by the OP comment "Basically I want \[Prime] to be higher."

Using adjustment boxes you can position the prime exactly where you want. Easiest way to do set the position is probably to type x\[Prime] and go to the menu Insert > Typesetting and nudge the prime around.

enter image description here

SetAttributes[higherPrime, HoldAllComplete];

higherPrime[
   sym_Symbol] := (sym /: MakeBoxes[sym, form_] = 
    With[{name = SymbolName[sym]}, 
     RowBox[{name, 
       AdjustmentBox["\[Prime]", BoxBaselineShift -> -0.9, 
        BoxMargins -> {{-0.15, 0}, {0, -0}}]}]];
   sym);

enter image description here

share|improve this answer
    
The main problem is that FullForm[higherPrime[x]] is stil x, so it's impossible to use both primed and unprimed versions of one symbol. Nudge Up is a great suggestion, what we need now is the way to automate it. – swish Oct 3 '12 at 21:54

This is not really ideal, but it gets you most of the way there:

SetAttributes[makeSuperscript, HoldAllComplete];
makeSuperscript[sym_Symbol] := (
   sym /: MakeBoxes[sym, form_] = With[{name = SymbolName[sym]},
     InterpretationBox[SuperscriptBox[name, "\[Prime]"], sym]
    ];
   sym
  );

makeSuperscript[q]
(* -> InterpretationBox[SuperscriptBox["q", "\[Prime]"], q] *)

The symbol is still called q, but it now looks like $q'$, as the InterpretationBox is not visible. You can either copy and paste the superscripted form (to make assignments, or use in other expressions) or enter it as q, as you prefer.

share|improve this answer
    
But I want to have this notation inside input, mostly in patterns, so that functions like f[x'_] := x' would work. Basically I want \[Prime] to be higher. – swish Oct 3 '12 at 19:29
    
@swish after evaluating makeSuperscript[x], f[x_] := x displays in the way that you want. If you copy and paste the output of Information[f], it works as input, even in a new session. I know this is not quite how you envisioned this working, but unfortunately I'm not aware of any reasonably safe way of accomplishing that. Even if you use a different font that has a higher-up \[Prime], you'll still need a StyleBox for it to display as such unless you edit the font mapping file UnicodeFontMapping.tr. – Oleksandr R. Oct 3 '12 at 19:48
    
That causes q to display as q^\[Prime]. But you could instead use InterpretationBox[SuperscriptBox[name, "\[Prime]"], Symbol[name <> "\[Prime]"]]. – Mechanical snail Dec 6 '12 at 23:19
    
@Mechanicalsnail yes, that's right, but the point was to have a symbol that appeared to be primed without having to have ' or \[Prime] in its actual name due to likely confusion (of both the user and Mathematica itself) with Derivative[1]. If you really want \[Prime] in the name then yes, your suggestion is sensible. One could also use any other Unicode character that displays in approximately the right way. – Oleksandr R. Dec 7 '12 at 7:57
    
@OleksandrR.: The Derivative confusion only occurs with an apostrophe, not \[Prime]. The asker wanted to be able to use unprimed and primed in the same expression. – Mechanical snail Dec 8 '12 at 0:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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