TwoString: Using ToString for Interpreter[“TexExpression”][string]

Thomas Colignatus
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
T
E
X
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).
Keywords
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

Introduction

Quick intro

How this notebook and package came about

The package

Comment on backslashes

Conclusions

Appendix. More drawn out

Literature

Start (subsection for the initialisation packages)


Introduction

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
T
E
X
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.
This 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

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
    T
    E
    X

    The 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
    T
    E
    X
    , 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
    X
    .

    Quick intro

    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],HoldFormTrue]
    HoldForm[Integrate[y, x]]
    %//FullForm
    "HoldForm[Integrate[y, x]]"
    ToExpression[%%]
    ∫yx
    With the option HoldFirst  False, TwoString becomes ToString (with default OutputForm).
    {ToString[Integrate[y,x]],TwoString[Integrate[y,x],HoldFirstFalse]}
    {x y,x y}

    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]
    ∫yx
    %//FullForm
    "\!\(\*TagBox[RowBox[{\"\[Integral]\", RowBox[{\"y\", RowBox[{\"\[DifferentialD]\", \"x\"}]}]}], HoldForm]\)"
    ToExpression[res]
    ∫yx
    %//FullForm
    HoldForm[Integrate[y,x]]
    %%//ReleaseHold
    xy

    FromString

    The routine FromString is a particular call of ToExpression that helps reminding about the hidden HoldForm.
    FromString[res]
    xy
    FromString[res,InputForm,Identity]
    ∫yx

    TeXForm has no use of HoldForm

    TeXForm removes the HoldForm by itself too, since the latter notion is unknown in
    T
    E
    X
    .
    str=TwoString[Integrate[y,x],TeXForm]
    \int y \, dx
    Observe the double backslashes.
    %//FullForm
    "\\int y \\, dx"
    This allows these three operations:
    Interpreter["TeXExpression"][str]
    xy
    Interpreter["InactiveTeXExpression"][str]
    ∫yx
    Interpreter["HeldTeXExpression"][str]
    HoldComplete∫yx
    PM. ToString[expr, TeXForm] may generate a String, but the Interpreter cannot always handle that String.

    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.
  • Sum[1/k^2,{k,1,Infinity}]
    ◼
  • A solution is to use HoldForm.
  • The package

    The following is a variant of TwoString[Sum[1/k^2, {k, 1, Infinity}], TeXForm].

    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

    The routine TwoString provides some functionality around ToString.
    The change is minor, which is witness to the core relevance of ToString.

    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
    Seggev, I. (2017), https://mathematica.stackexchange.com/questions/152456/what-is-the-difference-between-the-second-argument-of-tostring-and-its-option-fo
    Wolfram Research (2018), http://reference.wolfram.com/language/ref/ToString.html
    Xkcd (2016), “1638: Backslashes”, https://www.xkcd.com/1638/