News for hackers. Stuff that matters.
Re:Tips and tricks to write LaTeX papers in with figures gen... by semi-extrinsic on Monday March 18, 2019 @07:39PM (#19426491)
One itch which (curiously) I can't seem to quite scratch in LaTeX is that it should be possible to say "plot equation \ref{eq:smth} for X in (-4,4)" and just get the bloody graph. Why should I need to define the equation again in a separate place, perhaps even in a separate file?
Re:Tips and tricks to write LaTeX papers in with figures gen... by Wookai on Monday March 18, 2019 @08:26PM (#19426827)
It's not exactly what you want, but you can do parametrized plots in TikZ to plot a given function for some range: http://www.texample.net/tikz/examples/parameterized-plots/.

I wouldn't be surprised if someone wrote a tool that allows you to use a reference to an equation as the function to plot :).

Re:Tips and tricks to write LaTeX papers in with figures gen... by dmlorenzetti on Monday March 18, 2019 @10:50PM (#19427657)
This is not what you asked for, since it still requires a separate file. However it might be close enough to what you want, and -- for complicated expressions -- possibly even better.

You can write (or derive) the expression using sympy, then have sympy generate a numpy expression that can be evaluated. Sympy can also generate the LaTeX code for any expression. So while that isn't an in-LaTeX solution, it may be close to what you want.

Johansson's "Numerical Python" shows several examples of this. I will scavenge one of his examples below (trusting it falls under "fair use", and hoping I transcribe it correctly -- note I have left out the imports). The example uses sympy to generate and plot Taylor series expansions of sin(x).

The key bit to look for in the example is `sympy.lambdify()`.

    sym_x = sympy.Symbol("x")
    x = np.linspace(-2 * np.pi, 2 * np.pi, 100)

    def sin_expansion(x, n):
        return sympy.lambdify(sym_x, sympy.sin(sym_x).series(n=n+1).removeO(), 'numpy')(x)

    fig, ax = plt.subplots()
    ax.plot(x, np.sin(x), linewidth=4, color="red", label='exact')
    colors = ["blue", "black"]
    linestyles = [':', '-.', '--']

    for idx, n in enumerate(range(1, 12, 2)):
        ax.plot(x, sin_expansion(x, n), color=colors[idx // 3],
            linestyle=linestyles[idx % 3], linewidth=3,
            label="order %d approx." % (n+1))

    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(-1.5*np.pi, 1.5*np.pi)

    ax.legend(bbox_to_anchor=(1.02, 1), loc=2, borderaxespad=0.0)
    fig.subplots_adjust(right=.75)
I highly recommend the book. It's full of nuggets like this.
Re:Tips and tricks to write LaTeX papers in with figures gen... by kccqzy on Monday March 18, 2019 @07:47PM (#19426542)
LaTeX doesn't have enough information about what your notations mean. You can very well write nonsensical formulas that look pretty in LaTeX but are absolutely meaningless.
Re:Tips and tricks to write LaTeX papers in with figures gen... by bingerman on Monday March 18, 2019 @10:13PM (#19427492)
I wish I had read the texbook or something similar sooner to gain knowledge like this. Used latex for years without knowing the basics and I regret that a lot.

Also, (v)phantom and smash are something I really should have learned before all those fancy packages, nowadays I'm mostly using context anyways.