Abstract: I use a pattern so often that I needed to make a function for constructing it. A function getName[...]
for extracting name of variable and value only works when defined globally but does not work if called from another function or even if defined from within a function.
Question: Can this be fixed? If possible the makeRow1[...]
is my preferred type of solution.
About the Code: the code below shows 3 different contexts/uses for defining getNamne[...]
. Only the globally defined works.
Clear[var]
var = 123; separator = " \[Rule] ";
(* define function in global scope *)
SetAttributes[getName, HoldFirst];
getName[symbol_] := SymbolName[Unevaluated@symbol];
Row[{getName[var], separator, TraditionalForm[var]}]
(* use from within another function *)
makeRow1[var_, separator_ : " \[Rule]"] :=
Row[{getName[var], separator, TraditionalForm[var]}]
makeRow1[var]
(* define and use within a function *)
makeRow2[var_, separator_ : " \[Rule]"] := Block[{getName},
SetAttributes[getName, HoldFirst];
getName[symbol_] := SymbolName[Unevaluated@symbol];
Row[{getName[var], separator, TraditionalForm[var]}]
];
makeRow2[var]