I have been toying around with a couple of ideas which might be worth
investigating for the LaTeX printer. First off, different bracket
styles depending on the nesting level. So it might go:
{ [ ( ( ) ) ] }; exactly how to do this I am unsure. It depends on how
easy it is to maintain the nesting level. Ideally {} and [] should
only be used for adjacent brackets, for example:
(x*(y+3)+4)^3 doesn't really require nested brackets, but:
[a*exp(z*I)] does.
This is the kind of thing that I might be tempted towards the evils of
regular expressions to achieve. Hopefully, however there is a much
more elegant solution.
Secondly, constant expanding. If one has a fraction <x>/k where k is a
constant it is often nice to pull the constant out front when
printing, giving 1/k * <x> where <x> is an arbitrary expression. This,
again sacrifices horizontal space for better readability (a larger
font size can be used for <x>). I am yet to look into how hard this
would be to achieve, however.
Finally, an option to use exp() as opposed to {e}^{<x>}. This is most
useful when outputting the solutions to differential equations, which
often raise e to quite a large fraction/expression. Using exp() leads
to better readability (due to the larger font size and greater
spacing). I propose the following: we have a configuration option to
choose between {e}^{x}, exp(x) and auto, where auto uses heuristics to
decide (as soon as you get to needing brackets or fractions exp() is
often nicer).
Be interested to know what you all think -- and if you have any
implementation hints.
Regards, Freddie.
On 24 Dec 2008, at 23:36, Alan Bromborsky wrote:
> When you say configuration option I assume you mean a global switch so
> you can turn the option off and on for different parts of the program.
I was thinking more along the lines as an optional parameter passed to
latex(expr, expFormat='auto') or something along those lines. That way
there is no need for any kind of global state variable and so no need
to set/restore it.
Although having to pass multiple configuration options each time is
not the nicest thing in the world, in 99% of cases the defaults will
probably be what most people want.
Regards, Freddie.
On Thu, Dec 25, 2008 at 12:30 AM, Freddie Witherden
<fre...@witherden.org> wrote:
> Hi all,
>
> I have been toying around with a couple of ideas which might be worth
> investigating for the LaTeX printer. First off, different bracket
> styles depending on the nesting level. So it might go:
> { [ ( ( ) ) ] }; exactly how to do this I am unsure. It depends on how
> easy it is to maintain the nesting level. Ideally {} and [] should
> only be used for adjacent brackets, for example:
>
> (x*(y+3)+4)^3 doesn't really require nested brackets, but:
>
> [a*exp(z*I)] does.
Yes, I like what you propose.
>
> This is the kind of thing that I might be tempted towards the evils of
> regular expressions to achieve. Hopefully, however there is a much
> more elegant solution.
If the regular expression is maintainable, why not.
>
> Secondly, constant expanding. If one has a fraction <x>/k where k is a
> constant it is often nice to pull the constant out front when
> printing, giving 1/k * <x> where <x> is an arbitrary expression. This,
> again sacrifices horizontal space for better readability (a larger
> font size can be used for <x>). I am yet to look into how hard this
> would be to achieve, however.
Yes, I also agree with you.
>
> Finally, an option to use exp() as opposed to {e}^{<x>}. This is most
> useful when outputting the solutions to differential equations, which
> often raise e to quite a large fraction/expression. Using exp() leads
> to better readability (due to the larger font size and greater
> spacing). I propose the following: we have a configuration option to
> choose between {e}^{x}, exp(x) and auto, where auto uses heuristics to
> decide (as soon as you get to needing brackets or fractions exp() is
> often nicer).
Again +1.
>
> Be interested to know what you all think -- and if you have any
> implementation hints.
I am all for it, thanks for thinking about it. Thanks also for your
other patch in #1248.
Yes, I am also for latex(expr, exp_format='auto') or something, rather
than a global option.
Ondrej
> I have been toying around with a couple of ideas which might be worth
> investigating for the LaTeX printer. First off, different bracket
> styles depending on the nesting level. So it might go:
> { [ ( ( ) ) ] }; exactly how to do this I am unsure. It depends on how
> easy it is to maintain the nesting level. Ideally {} and [] should
> only be used for adjacent brackets, for example:
>
> (x*(y+3)+4)^3 doesn't really require nested brackets, but:
>
> [a*exp(z*I)] does.
>
> This is the kind of thing that I might be tempted towards the evils of
> regular expressions to achieve. Hopefully, however there is a much
> more elegant solution.
Currently the best idea on the table, so far as I can discern, is the
following:
- Use a regular expression to find pairs and triplets of brackets
adjacent to each other, so (( and (((;
- for the outermost bracket(s) find the complementary bracket (so
its opposite) -- this is easy to do using a stack;
- replace it and its complement with the appropriate substitution,
'[' or '{'.
It is not possible to use closing brackets and work backwards as
function calls should *always* use ( ), e.g., in (x + ln(x*(x+1)))^n
working backwards would give us {x + ln[x*(x+1)]} -- not what we want.
> Secondly, constant expanding. If one has a fraction <x>/k where k is a
> constant it is often nice to pull the constant out front when
> printing, giving 1/k * <x> where <x> is an arbitrary expression. This,
> again sacrifices horizontal space for better readability (a larger
> font size can be used for <x>). I am yet to look into how hard this
> would be to achieve, however.
I am currently putting together some examples where I feel this has a
positive impact on readability. A good example is sin(1/2 * x), on in
LaTeX \operatorname{sin}\frac{1}{2} x as opposed to \operatorname{sin}
\frac{x}{2}. The output of Wolfram's Interator, http://integrals.wolfram.com/index.jsp
is usually quite good at deciding whether to pull the constant out
or not. (Although do not believe the claim about it being the "world's
only full-power integration solve", Axiom eats it for breakfast :)
> Finally, an option to use exp() as opposed to {e}^{<x>}. This is most
> useful when outputting the solutions to differential equations, which
> often raise e to quite a large fraction/expression. Using exp() leads
> to better readability (due to the larger font size and greater
> spacing). I propose the following: we have a configuration option to
> choose between {e}^{x}, exp(x) and auto, where auto uses heuristics to
> decide (as soon as you get to needing brackets or fractions exp() is
> often nicer).
Again, some examples are needed. e^{\operatorname{tan}x} looks okay,
as does e^{ax+b} but two fractions (so a/b + p/q) usually don't. A way
to see if a mul will end up as a fraction would be nice.
Finally, while playing around some more with the LaTeX output (and
showing it to whoever was willing to listen) I found that some have an
affinity for tan-1 (\operatorname{tan}^{-1}x as opposed to atan or
arctan. G&R use arc<x> where <x> is an trigonometric function.
My proposal here is to support all three inverse trigonometric forms,
arc<x> being the default (it is what LaTeX defines, \arctan{x}) but
also supporting the other two forms by way of an optional parameter to
latex(expr,...).
The simplest way of implementing this is probably to use a look-up
table to identify a function as an inverse trigonometric function.
Then it is a simple case of stripping the first character and
appending either 'arc', 'a' or suffixing '^{-1}'.
In the case of atan(x)**2 being output in the form of tan-1 the output
would be: \operatorname{tan}^{-1}\left(x\right)^{2}. (Even if bracket
folding is enabled and applicable, when outputting as tan-1 where tan
is raised to a power brackets would be mandatory.)
Regards, Freddie.