5
$\begingroup$

When I execute the code below:

Tooltip[Quantity[1, "Month"], "how to make it works?"]

The tooltip that is automatically generated by Quantity prevails over the one I added myself with Tooltip.

the tooltip is "unit: month" instead of the desired string

I would like to preserve the Quantity objects (rather than convert them to strings), but I would like to be able to override the automated tooltip with my own.

How can I fix It?

$\endgroup$
5
$\begingroup$
Clear["Global`*"]

Create a wrapper for adding a Tooltip to a Quantity

qTooltip[q_Quantity, tt_String] :=
 Tooltip[QuantityForm[q, "Abbreviation"], tt]

Like other wrappers (e.g., MatrixForm), the wrapper should not be included in variable definitions.

qTooltip[quant = Quantity[1, "Month"], "how to make it work?"]

(* Tooltip[1*"mo", "how to make it work?", 
   TooltipStyle -> "TextStyling"] *)

The definition of quant does not include the wrapper.

QuantityMagnitude[quant]

(* 1 *)

UnitConvert[quant, "days"]

(* Quantity[365/12, "Days"] *)

EDIT: You do not necessarily need to restrict the tooltip to being a string, and you can default to a normal Tooltip for none quantities.

Clear[qTooltip];

plt[n_?NumericQ] := Plot[x^n, {x, 0, 1}];

qTooltip[q_Quantity, tt_] :=
  Tooltip[QuantityForm[q, "Abbreviation"], tt];

qTooltip[q_, tt_] := Tooltip[q, tt];

Examples (outputs deleted):

qTooltip[quant = Quantity[1, "Month"], plt[1]]

qTooltip[n = 3, plt[n]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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