TwoString: Using ToString for Interpreter[“TexExpression”][string]
TwoString: Using ToString for Interpreter[“TexExpression”][string]
Thomas Colignatus
http://thomascool.eu
April 5 2018
http://thomascool.eu
April 5 2018
Abstract
ToString is a routine in Mathematica, that has default FormatType OutputForm and that has no attribute HoldFirst. For applications for the Interpreter for it makes more sense to have attribute HoldFirst. When this is used then other forms may get HoldForm in the output string. It is easy to remove the latter again for InputForm. Since this works fine, it makes sense to make the latter the new default. Since ToString is such a core routine, this new functionality can better be provided by a new routine TwoString that builds around ToString. Alternative names are TooString or MakeString. It is suggested to the makers of Mathematica that ToString is adapted with options HoldFirst True and HoldForm False for this particular use just like TwoString (with default InputForm and the removal of HoldForm from the output string).
T
E
XKeywords
ToString, TeXForm, Mathematica, Wolfram language, Programming, Package
MSC2010
97M70 Mathematics education. Behavioral and social sciences
Cloud
This paper is also available at:
https://www.wolframcloud.com/objects/thomas-cool/Utilities/2018-04-05-TwoString.nb
Contents
Contents
Introduction
Introduction
Quick intro
Quick intro
How this notebook and package came about
How this notebook and package came about
The package
The package
Comment on backslashes
Comment on backslashes
Conclusions
Conclusions
Appendix. More drawn out
Appendix. More drawn out
Literature
Literature
Start (subsection for the initialisation packages)
Start (subsection for the initialisation packages)
Introduction
Introduction
ToSting TwoString
ToSting TwoString
ToString is a routine in Mathematica, that has (1) default FormatType OutputForm which uses new lines (“\n”) for vertical formatting, and (2) no attribute HoldFirst. For applications for the Interpreter for and FormatType TeXForm it makes more sense to have attribute HoldFirst, so that ToString[expr, TeXForm] would be applied to HoldForm[expr] rather than an evaluated expr.
T
E
XThis new functionality is provided by a routine TwoString that builds around ToString. Alternative names are TooString (more pun) or MakeString (less pun). When TwoString is called with HoldFirst False then it fully reduces to ToString.
The body of the notebook has a quick introduction, with a listing of the routines in the package.
Some authors rightly protest against the introduction of new names for only special applications. In this case ToString has quite a legacy, and the present idea seems a relevant amendment. This new routine is better than unprotecting ToString, adapting it, say as ToString[][expr], and protecting again. This present discussion may at least alert some users to particular properties of ToString.
For WRI it is a suggestion to adapt ToString with options HoldFirst True and HoldForm False like TwoString has.
Handling HoldForm
Handling HoldForm
With attribute HoldFirst we may get HoldForm in the generated strings (except e.g. TeXForm, that has no use for it).
For InputForm, the removal of HoldForm from this string is easy. Since this works fine and actually is desirable, it makes sense to take this as the default of TwoString. Option HoldForm True however allows to keep HoldForm also in InputForm.
With HoldForm in the string, ToExpression keeps HoldForm too. The routine FromString has a default ReleaseHold. Thus we get the structure:
◼
ToString has no attribute HoldFirst. ToExpression has a feature to use Hold.
◼
TwoString has attribute HoldFirst. FromString has default ReleaseHold (and otherwise use Identity).
A bug in Interpreter for TEX
A bug in Interpreter for
T
E
XThe Appendix contains a longer discusison.
There appeared to exist a bug, see below, that has been reported to Support. Trying to understand a new routine, like I did for Interpreter for , appears to be awkward, when there appears to be some bug that makes you quite confused about what is happening, as you lack the experience to diagnose quickly that there is a bug. This event also causes that this notebook is longer and more involved than it otherwise would have been. I had to make sure that the problem didn’t originate in ToString, and my build around it, but in Interpreter. It may still be that ToString[expr, FormatType TeXForm] still isn’t fully developed for translating Mathematica input into .
T
E
XT
E
XQuick intro
Quick intro
TwoString with HoldFirst and InputForm
TwoString with HoldFirst and InputForm
The routine TwoString works as ToString, but with:
◼
attribute HoldFirst
◼
default FormatType InputForm
◼
with FormatType InputForm: also the option HoldForm False (default) for the removal of HoldForm from the output String again.
This uses the default settings of TwoString.
TwoString[Integrate[y,x]]
Integrate[y, x]
%//FullForm
"Integrate[y, x]"
ToExpression[%%]
xy
With the default Inputform, the option HoldForm True stops the removal of HoldForm for the output string.
TwoString[Integrate[y,x],HoldFormTrue]
HoldForm[Integrate[y, x]]
%//FullForm
"HoldForm[Integrate[y, x]]"
ToExpression[%%]
∫yx
With the option HoldFirst False, TwoString becomes ToString (with default OutputForm).
{ToString[Integrate[y,x]],TwoString[Integrate[y,x],HoldFirstFalse]}
{x y,x y}
TwoString and StandardForm: HoldForm in output that does not display
TwoString and StandardForm: HoldForm in output that does not display
The output of TwoString for other forms like FormatType StandardForm will have HoldForm in the FullForm, which does not display in ToExpression.
res=TwoString[Integrate[y,x],StandardForm]
∫yx
%//FullForm
"\!\(\*TagBox[RowBox[{\"\[Integral]\", RowBox[{\"y\", RowBox[{\"\[DifferentialD]\", \"x\"}]}]}], HoldForm]\)"
ToExpression[res]
FromString
FromString
The routine FromString is a particular call of ToExpression that helps reminding about the hidden HoldForm.
TeXForm has no use of HoldForm
TeXForm has no use of HoldForm
Observe the double backslashes.
This allows these three operations:
PM. ToString[expr, TeXForm] may generate a String, but the Interpreter cannot always handle that String.
How this notebook and package came about
How this notebook and package came about
I was looking at Interpreter[“TeXExpression”].
◼
Automation however is not simple when the input is first evaluated. The following expression evaluates, and ToString has no HoldFirst and will evalutate its input.
◼
A solution is to use HoldForm.
The package
The package
The following is a variant of TwoString[Sum[1/k^2, {k, 1, Infinity}], TeXForm].
Comment on backslashes
Comment on backslashes
Reviewing the issue, we can be amazed at the proliferation of backslashes, see also Xkcd (2016).
Entering "\int y \, dx" in Mathematica gives a syntax error, and ctrl-shift-E already shows four backslashes for one backslash in input.
My impression is that it is currently impossible to create such an object TeXString[expr], as Mathematica is set up to use the backslash as an exception switch. Even the notion of a “character” turns a single \ into a \\.
Conclusions
Conclusions
The routine TwoString provides some functionality around ToString.
The change is minor, which is witness to the core relevance of ToString.
Literature
Literature
Thomas Colignatus is the scientific name of Thomas Cool, econometrician and teacher of mathematics, Scheveningen, Holland.
b3m2a1 (2018), “In defense of simplicity (or why I disagree with Stephen Wolfram)”, http://community.wolfram.com/groups/-/m/t/1310588
Wolfram Research (2018), http://reference.wolfram.com/language/ref/ToString.html
Xkcd (2016), “1638: Backslashes”, https://www.xkcd.com/1638/