Summarizing all of the above, following code implements the list of formatting rules. The features are:
- Displays in output based on the set of formatting tags. e.g.
a[1] displays as $a_1$
- Matches string patterns such as "a*" to
ab[1]
- Copy/paste in the notebook of the output
a_1 actually copies a[1] as it should.
- ToLatex takes an expression and generates the latex with the formatting. i.e.
a[1] becomes a_1 in latex.
- Special cases for hats, bars, etc. e.g.
a[star] displays as $a^{*}$
- Rules include variations for both variables and functions. i.e. $\hat{f}_l(z)$ and $a_l$
- Can handle derivatives $f[l]'[z]$ to $f_l'(z)$
- Can handle functions with/without subscripts. e.g. $f[z]$ and $f_l[z]$ simultaneously if $z$ is flagged as a parameter.
Usage:
$scriptedconstants = {a, ab};
$scriptedfunctions = {f};
$scriptedfunctionsvars = {x};
val = {a[1], ab[1], a[1, 2], a[bar], a[hat], a[tilde], a[vec],
a[underbar], a[plus], a[minus], a[star], a[l][bar], a[l][hat],
a[l][tilde], a[l][vec], a[l][underbar], a[l][plus], a[l][minus],
a[l][star]}
val2 = {f[x], D[f[x], x], f[l][x, z], f[l], f[l]'[z],
D[f[l][z], z, z], D[f[l][x, z], z], D[f[hat][x, z], z],
D[f[l][hat][x, z], z], f[1][z], f[1][z_], f[bar][z], f[bar][x, z],
f[hat][z], f[tilde][z], f[vec][z], f[underbar][z], f[plus][z],
f[minus][z], f[star][z], f[l][bar][z], f[l][hat][z], f[l][tilde][z],
f[l][vec][z], f[l][underbar][z], f[l][plus][z], f[l][minus][z],
f[l][star][z]}
val // ToLatex
val2 // ToLatex
This displays as:
$$
\left\{a_1,\text{ab}_1,a_{1,2},\bar{a},\hat{a},\tilde{a},\overset{\rightharpoonup }{a},\underline{a},a^+,a^-,a^*,\bar{a}_l,\hat{a}_l,\tilde{a}_l,\overset{\rightharpoonup }{a}_l,\underline{a}_l,\text{Subsuperscript}[a,l,+],\text{Subsuperscript}[a,l,-],\text{Subsuperscript}[a,l,*]\right\}
$$
(the super/subscripts here actually show correctly in the FrontEnd, but are the only thing which don't translate to Latex well. An independent problem from this code)
$$
\left\{f(x),f'(x),f_l(x,z),f_l,f_l'(z),f_l''(z),f_l{}^{(0,1)}(x,z),\hat{f}^{(0,1)}(x,z),\hat{f}_l{}^{(0,1)}(x,z),f_1(z),f_1(\text{z$\_$}),\bar{f}(z),\bar{f}(x,z),\hat{f}(z),\tilde{f}(z),\overset{\rightharpoonup }{f}(z),\underline{f}(z),f^+(z),f^-(z),f^*(z),\bar{f}_l(z),\hat{f}_l(z),\tilde{f}_l(z),\overset{\rightharpoonup }{f}_l(z),\underline{f}_l(z),\text{Subsuperscript}[f,l,+](z),\text{Subsuperscript}[f,l,-](z),\text{Subsuperscript}[f,l,*](z)\right\}
$$
And the ToLatex @val2 generates as:
\left{f(x),f'(x),f_l(x,z),f_l,f_l'(z),f_l''(z),f_l{}^{(0,1)}(x,z),\hat{f}^{(0,1)}(x,z),\hat{f}_l{}^{(0,1)}(x,z),f_1(z),f_1(\text{z$\_$}),\bar{f}(z),\bar{f}(x,z),\hat{f}(z),\tilde{f}(z),\overset{\rightharpoonup }{f}(z),\underline{f}(z),f^+(z),f^-(z),f^*(z),\bar{f}_l(z),\hat{f}_l(z),\tilde{f}_l(z),\overset{\rightharpoonup }{f}_l(z),\underline{f}_l(z),\text{Subsuperscript}f,l,+,\text{Subsuperscript}f,l,-,\text{Subsuperscript}f,l,*\right}
The full listing of code for this, slightly modified version from that of mr-wizard is:
NotScriptedVarQ[z_] := ! MemberQ[$scriptedfunctionsvars, z, Infinity];
makeDef[pat_, body_] := (
MakeBoxes[a : pat, fmt_] := ToBoxes[Interpretation[body, a], fmt] /;
MemberQ[Union[$scriptedconstants, $scriptedfunctions], Unevaluated @ h];
MakeBoxes[a : pat[sub_], fmt_] := ToBoxes[Interpretation[body[sub], a], fmt] /;
MemberQ[$scriptedfunctions, Unevaluated @ h]
)
set1 = {
bar -> OverBar,
hat -> OverHat,
tilde -> OverTilde,
vec -> OverVector,
underbar -> UnderBar,(* After here subsuper is ncessary to have both annotations*)
plus -> SuperPlus,
minus -> SuperMinus,
star -> SuperStar
};
set2 = {
plus -> "+",
minus -> "-",
star -> "*"
};
makeDef[h_Symbol[#], #2[h]] & @@@ set1;
makeDef[h_Symbol[argssub__][#], Subscript[#2[h], argssub]] & @@@ Take[set1, 5];
makeDef[h_Symbol[argsub_][#], Subsuperscript[h, argsub, #2]] & @@@ set2;
makeDef[h_Symbol[argssub__?NotScriptedVarQ], Subscript[h, argssub]];
$displayscripted = {a_}whereab[1]becomes $ab_1$ – jlperla Aug 22 '13 at 16:13Patternobject:$displayscripted = {"a*"}-- if this is acceptable I will adapt my answer to include it. – Mr.Wizard Aug 22 '13 at 16:16a[1], displaying as $a_1$, it copies asa[1]. If I do things like:ToString[a[1],StandardFormand then latex copy/paste the output it is closer, but still wacky. Finally, if I put in my other forms such asa[1][2]to $a_1^2$ ora[star]to $a^{*}$ I get an error likeRecursion depth of 4096 exceededor it simply doesn't copy properly. – jlperla Aug 22 '13 at 17:59MakeBoxes[a : h_[star], fmt_] /; MemberQ[$variablelist, Unevaluated@h] := ToBoxes[Interpretation[SuperStar[h], a], fmt];for example. – jlperla Aug 22 '13 at 18:00Interpretation? YourSuperStarcode doesn't seem quite right; do you really mean to match the symbolstarexpressly? If you could include an example of a problematic expression in your question, along with theMakeBoxesdefinitions that you are using, I may be able to fix it. – Mr.Wizard Aug 22 '13 at 18:04star, reserving and abusing the symbol to aid in easy equation typing (and diff'ing in SVN with a .m file). The implementaiton ofa[1][2]to $a_1^2$ isMakeBoxes[a : h_[argssub__][argssuper__], fmt_] /; MemberQ[$variablelist, Unevaluated@h] := ToBoxes[Interpretation[Subsuperscript[h, argssub, argssuper], a], fmt];– jlperla Aug 22 '13 at 18:10a[1], etc. in my .m file or notebook. 2) Have the output displayed as $a_1$ in the output window when executing the package or notebook. 3) Be able to copy/paste the output into a latex file. 3b) It wouldn't hurt to be able to copy/paste parts of the output in the notebook to edit equations in the input, but I would actually prefer it to copy asa[1]so that all formulas in the input look proper. If you are saying that 3) and 3b) are mutually exclusive, I choose 3) – jlperla Aug 22 '13 at 18:13\text{$\$$CellContext$\grave{ }$a}(1)+\text{$\$$CellContext$\grave{ }$a}(2)+(\text{$\$$CellContext$\grave{ }$bx}(1))^2-However, following your suggestion, if I remove theInterpretationfrom the Extension code, it seems to copy/paste as I had hoped. – jlperla Aug 22 '13 at 21:29Interpretation, noted specifically for later versions. – Mr.Wizard Aug 22 '13 at 21:56InterpretationBox-- give me a minute to code that. – Mr.Wizard Aug 22 '13 at 22:17MakeBoxes[a : h_[args__], fmt_] := Block[{$inSSfmt = True}, InterpretationBox @@ ToBoxes /@ {Subscript[h, args], a} ] /; ! TrueQ[$inSSfmt] && MemberQ[Names@$subs, ToString@Unevaluated@h]– Mr.Wizard Aug 22 '13 at 22:22\text{RowBox}[\{\text{b},[,2,]\}]\to \frac{1}{2} \left(\text{RowBox}[\{\text{a},[,1,]\}]+\text{RowBox}[\{\text{bx},[,1,]\}]^2+\text{RowBox}[\{\text{a},[,2,]\}]-c(1)\right)– jlperla Aug 22 '13 at 23:01Interpretation[(* stuff with subscripts *), (* stuff without subscripts *)]and try the copy? – Mr.Wizard Aug 22 '13 at 23:06