9
$\begingroup$

OverVector can be used to make a symbol look like a vector in text sections of documents. I happen to like bold symbols for vectors better, as in latex \mathbf{x} output.

I've found that I can do, say, Style[x, Bold], but this is actually a very subtle seeming change, and I find it hard to actually notice the difference. Perhaps this is because the default font picked for variables is something quite different from what one gets with \mathbf. If this is just a font issue, what's a good font for this sort of output (i.e. like \mathbf{x}), and how would it be selected?

Also, the Style function applies to expressions, whereas I want it to apply to just particular variables (i.e. leaving scalars in regular font), so that when they are used in input, the output comes out in the desired way.

Is there a mechanism to declare a variable, and have specific style settings associated with it, so that it is displayed in the desired way?

$\endgroup$

4 Answers 4

8
$\begingroup$

I am not sure I understand your needs, but consider this:

Format[OverVector[v_]] := Style[HoldForm[v], FontFamily -> "Arial Black"]

{q, r, OverVector[s], t, u, v}

Mathematica graphics

$\endgroup$
8
$\begingroup$

This is mainly an add-on to David solution. I'm new in SE, so I can't comment yet. So I put it in an answer.

One could maintain a list of the symbols, which should be displayed as vectors, eg:

vList = {}
defAsVector[x__Symbol] := (vList = Union[Join[vList, {x}]];)
defAsVector[] := vList
undefAsVector[x__Symbol] := (vList = Complement[vList, {x}];)
undefAsVector[] := (vList = {}; $PrePrint =.;)
$PrePrint = # /. {x_ /; MemberQ[vList, x] :> Style[x, Bold]} &;
$\endgroup$
5
$\begingroup$

I only have my iPhone now, but something like this should work well.

MakeBoxes[x,StandardFom]:=StyleBox["x",FontWeight->Bold];  
MakeExpression[StyleBox["x",FontWeight->Bold]]:=MakeExpression["x"].  

Better yet you could 'declare' that certain symbols represent vectors.

declaredVectorQ[x_Symbol]:^=declaredVector[x]^=True;
MakeBoxes[x_Symbol?declaredVectorQ,StandardForm]:=
                                    StyleBox[ToString[x],FontWeight->Bold];  
MakeExpression[StyleBox[str_String,FontWeight->Bold]:=ToExpression[str];
$\endgroup$
4
$\begingroup$
  • You can specify a different font by using the FontFamily parameter for Style: enter image description here

  • If you want specific symbols to be printed differently on output, you may want to have a look at $Post and $PrePrint. For example, the following prints the symbol x bold and in Helvetica:

$PrePrint =.
x
$PrePrint = # /. {x -> Style[x, Bold, FontFamily -> "Helvetica"]} &;
x

enter image description here

Be careful though, a styled object can have unwanted side effects. Therefore, in this paragraph, I sincerely disencourage you to change styling of symbols you want to do calculations with, and would recommend doing the prettyprinting in the very end, using an explicit handcoded function.

$\endgroup$
2
  • $\begingroup$ What kind of bad side effects are you thinking of? Don't have time to fiddle with this, but I thought of using Interpretation to make the output re-usable, what do you think? $\endgroup$
    – Szabolcs
    Feb 24, 2012 at 8:53
  • $\begingroup$ Imagine you've got something with a style and apply some definitions to it, and then use the normal symbol again - not fun to fix. In other places both of them might be equivalent, I don't think there's a consistenz way of doing it. Interpretation is a very good idea though. $\endgroup$
    – David
    Feb 24, 2012 at 9:13

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.