Function Repository Resource:


Source Notebook

Print all expressions used in the evaluation of an expression, along with the results of evaluating those expressions

Contributed by: Carl Woll

ResourceFunction["TracePrintEvaluations"][expr, form]

prints an in/out toggler for expressions that match form.


includes all evaluations that use transformation rules associated with the symbol s.

Details and Options

The output of ResourceFunction["TracePrintEvaluations"] looks very similar to the output of TracePrint. However, clicking one of the outputs toggles to the evaluated expression. The output is the result of evaluating expr:
ResourceFunction["TracePrintEvaluations"] indents its output in correspondence with the nesting levels for lists generated by Trace.
ResourceFunction["TracePrintEvaluations"] prints the forms of expressions before any of their elements are evaluated.
The output toggles between the unevaluated expressions and the actual result of evaluating those expressions during the evaluation of expr.
Right-clicking the trace output copies an unevaluated version of the displayed expression.
When the output has no visible form (e.g. an empty string or a Sequence[] object), the output is automatically modified to show the InputForm.
ResourceFunction["TracePrintEvaluations"][expr] returns the result of evaluating expr.
Loading ResourceFunction["TracePrintEvaluations"] automatically adds cell styles to the evaluation notebook. If the cell styles are absent from the notebook, the trace output includes the necessary styling information in each output cell, increasing the size of the notebook.
With the default option SaveDefinitionsFalse, trace cells do not include the evaluation information once the kernel has been quit.
Using the option SaveDefinitionsTrue stores the evaluated expressions of each trace cell in the result. The evaluated expressions persist even if the kernel has been quit, or the notebook has been closed.
The following options can be used to display the specified information using additional clicks:
"Timing"set to True to include timing of the traced evaluation
"TraceIn"function to apply to the traced input
"TraceOut"function to apply to the traced input/output pair
The option "TraceIndent" can be used to specify how to indent the input trace cells.


Basic Examples (2) 

Recursive Fibonacci definition:

f[0] = 0;
f[1] = 1;
f[n_] := f[n - 1] + f[n - 2]

Use TracePrintEvaluations to track calls to f. Click the printed values to toggle between inputs and outputs:

ResourceFunction["TracePrintEvaluations"][f[4], _f]

Scope (2) 

See what DateList evaluated to during the following function:

f[] := OddQ @ DateValue["Second"]

The TracePrintEvaluations trace cell includes the evaluation of the subexpression:

ResourceFunction["TracePrintEvaluations"][f[], _DateValue]

Options (8) 

SaveDefinitions (3) 

Using SaveDefinitionsTrue stores all of the evaluation expression in the result cell. Consider the recursive Fibonacci definition again:

f[0] = 0;
f[1] = 1;
f[n_] := f[n - 1] + f[n - 2]

Using TracePrintEvaluations:

ResourceFunction["TracePrintEvaluations"][f[4], _f, SaveDefinitions -> True]

Notice the CellDingbat of the previous result includes a • character. This indicates that the trace cell evaluation information is stored in the result cell:

Cases[NotebookRead @ PreviousCell[CellStyle -> "PersistentOutput"], RuleDelayed[DynamicModuleValues, rhs_] :> First @ rhs, Infinity] // InputForm

Timing (1) 

Using the "Timing"True option includes evaluation timing information in each trace cell:

ResourceFunction["TracePrintEvaluations"][Pause[.1]; Pause[.2]; 3, _Pause, "Timing" -> True]

Click on any trace cell twice to see the evaluation timing.

TraceIn (1) 

Using the "TraceIn"func option evaluates func[subexpr], where subexpr is the subexpression matching the TracePrintEvaluations pattern, and include the result in the trace cell:

 1 + (2 + 3) + 4, _Plus, "TraceIn" -> Replace[HoldForm[s_] :> Length @ Unevaluated @ s]

Click any trace cell twice to see the length of the Plus expression.

TraceIndent (1) 

The option "TraceIndent"string specifies the indentation to use to reflect the trace level:

 "TracePrintEvaluations"][(1 + (2 + (3 + (4 + 5)))), _Plus, "TraceIndent" -> "\[Bullet]"]

TraceOut (2) 

Using the "TraceOut"func option evaluates func[subexpr,eval], where subexpr is the subexpression matching the TracePrintEvaluations pattern and eval is what the subexpression evaluated to, and include the result in the trace cell:

 "TracePrintEvaluations"][(1 + (2 + (3 + (4 + 5)))), _Plus, "TraceOut" -> Rule]

Click any trace cell twice to see a rule indicating what each subexpression evaluated to.


Carl Woll

Version History

  • 1.0.0 – 24 October 2019

Related Resources

License Information