> I'm writing a piece of code which takes a Mma expression
> as an argument, processes it through TeXForm, merges that
> with some LaTeX to make a runnable LaTeX file, which is
> viewed with a DVI viewer on the screen. You can see that
> this is potentially useful, in that it allows an inexperienced
> computer user to get a translation of their Mma expressions
> into standard mathematical form. You can also see that its
> rather crucial that the original expression remains completely
> unevaluated in the process, even to the point of simple
> reordering of sub-expressions into canonical form.
One way to do this is to apply InString to $Line. If you write
s = InString[$Line] then s is a string containing exactly the
current input. You can then operate on it using Mathematica's
string functions. An illustration of its use is in the
following code, which is part of my package 0204-769 on
MathSource. Try TruthTable[(x && y) || z], for example.
TT[1] := {{True},{False}}
TT[n_ /; n>1] := T[n] =
Join[Map[Prepend[#,True]&,TT[n-1]],
Map[Prepend[#,False]&,TT[n-1]]]
SymbolQ[x_] := If[Head[x]==Symbol,True,False,False]
VariableSet[e_] :=
Select[Union[Level[e, {-1}]],SymbolQ]
MakeArgs[l_List] := StringReplace[ToString[l],
{"," -> "_,","{" -> "[", "}" -> "_]"}]
MakeFunction[e_,f_] :=
ToExpression[StringJoin[f // ToString,
MakeArgs[VariableSet[e]],
" := ", e // InputForm // HoldForm //ToString]]
Abb[True] := "T"
Abb[False] := "F"
SetAttributes[Abb,Listable]
TruthTable[e_] :=
Module[{args = VariableSet[e],
expr=InString[$Line],list,lbracket,rbracket,n,ff},
(MakeFunction[e,ff];
n = Length[args];
lbracket=StringPosition[expr,"[",1][[1]][[1]];
rbracket=StringPosition[expr,"]",1][[1]][[1]];
list = MapThread[Join,
{TT[n],Map[List[Apply[ff,#]]&,TT[n]]}] // Abb;
list = Prepend[list,
Join[args,{StringTake[expr,{lbracket+1,rbracket-1}]}]];
TableForm[list,
TableAlignments-> Center,
TableSpacing -> {0,2}])]
--
Charles Wells
Department of Mathematics, Case Western Reserve University
10900 Euclid Avenue, Cleveland, OH 44106-7058, USA
216 368 2893