diff --git a/WolframLanguageForJupyter/Resources/OutputHandlingUtilities.wl b/WolframLanguageForJupyter/Resources/OutputHandlingUtilities.wl index 0f5c670..e0c5f50 100644 --- a/WolframLanguageForJupyter/Resources/OutputHandlingUtilities.wl +++ b/WolframLanguageForJupyter/Resources/OutputHandlingUtilities.wl @@ -198,6 +198,29 @@ If[ (* generate the textual form of a result using the current PageWidth setting for $Output *) toText[result_] := toText[result, $truePageWidth]; + toTeX[result_] := + Module[ + { + (* if the result should be marked as TeX *) + isTeX + }, + (* check if the result should be marked as TeX *) + isTeX = ((Head[result] === TeXForm) || ($outputSetToTeXForm)); + Return[ + If[ + !isTeX, + (* if not TeX, return $Failed *) + $Failed, + StringJoin[ + (* wrap the result with latex symbol *) + "$$", + toText[result, Infinity], + "$$" + ] + ] + ]; + ]; + (* generate HTML for the textual form of a result *) toOutTextHTML[result_] := Module[ diff --git a/WolframLanguageForJupyter/Resources/RequestHandlers.wl b/WolframLanguageForJupyter/Resources/RequestHandlers.wl index 4782277..8411775 100644 --- a/WolframLanguageForJupyter/Resources/RequestHandlers.wl +++ b/WolframLanguageForJupyter/Resources/RequestHandlers.wl @@ -293,6 +293,11 @@ If[ (* the data representing the results and messages *) "data" -> { + (* "text/plain" -> + StringJoin[ + toText[totalResult] + ] + , *) (* generate HTML for the results and messages *) "text/html" -> If[ @@ -360,10 +365,42 @@ If[ }, {outIndex, 1, Length[totalResult["EvaluationResult"]]} ] + ], + Module[ + (* list of the results in the TeX form *) + { + results + }, + results = If[ + loopState["isCompleteRequestSent"], + (* if an is_complete_request has been sent, assume jupyter-console is running the kernel, + and do not generate HTML *) + "", + If[ + Length[totalResult["EvaluationResult"]] > 1, + (* If there are multiple results, display it line by line *) + Table[ + toTeX[totalResult["EvaluationResult"][[outIndex]]], + {outIndex, 1, Length[totalResult["EvaluationResult"]]} + ], + (* Otherwise for single result, display the first one *) + If[ + Length[totalResult["EvaluationResult"]] == 0, + {$Failed}, + {toTeX[First[totalResult["EvaluationResult"]]]} + ] + ] + ]; + (* check if any of the result is not in the TeXForm, if so, do not display TeX output *) + If[ + MemberQ[results, $Failed], + Nothing, + "text/latex" -> results ] + ] }, (* no metadata *) - "metadata" -> {"text/html" -> {}, "text/plain" -> {}} + "metadata" -> {"text/html" -> {}, "text/plain" -> {}, "text/latex" -> {}} ], "JSON", "Compact" -> True