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]];