Here's a general workflow for blocking values of global variables while output is prepared. Before the output is returned, the global variables need to be protected from evaluation. HoldForm
is the way to do this and have the variables be typeset in their usual format.
x = 3; y = 7; (* to see if x appears as x *)
(** hold variables to be used in formula/output **)
heldvars = Hold[x, y];
(* use stored variables in heldvars to Block their values *)
heldvars /. Hold[$v___] :> Block[{$v, res},
(** possibly lengthy code to compute result `res` **)
res = Column[{x''[t]/Sqrt[x + y] == 5 y}, Spacings -> 0.5];
(* protect variables with HoldForm before returning *)
res = res /. {var : Alternatives[$v] :> HoldForm[var]};
(** format result **)
TextCell[
TraditionalForm@res,
"Text", FontFamily -> "Palatino", FontSize -> 18]
]
The lines of code after the comments between the double asterisks (**...**)
are user-supplied and would probably change from case to case. The lines of code after the comments between the single asterisks (*...*)
should be the same in all use-cases (that I've thought of). One exception might be whether to block the variable res
. It might be left global or it might be localized in a Module
instead of in Block
. Similarly, but more importantly, you shouldn't use $v
as a variable in the computation of res
, since it was used as a pattern name.