Skip to main content
added generalization
Source Link
Bob Hanlon
  • 162.7k
  • 7
  • 81
  • 205
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]]
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"] *)
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]]
Source Link
Bob Hanlon
  • 162.7k
  • 7
  • 81
  • 205

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"] *)