3

I've just been searching like an hour on how to draw a simple function in LaTeX, for example f(x)=x^2. The examples I've tried did not work for some unknown reason, most probably because pdflatex would not start gnuplot (the -enable-xxx options do not work), but I found a LaTeX package that works: pgfplots. The only problem is that the documentation does not contain a simple example like what I need. Yes, if I wanted to draw complex diagrams with whatnot then I might find something, but a really simple example of a function -- no luck! But maybe I just need new glasses. So please, I'd be glad to get a simple example of a function plot in LaTeX, with an x and y axis and a graph!

EDIT: I've found this the best solution so far, but I'm not yet satisfied, for example the x label is in the middle. I really just want a standard plot without any fancy colors or 3d or similar.

\begin{tikzpicture}
\begin{axis}[
    axis x line=bottom,
    axis y line=middle,
    xlabel=$x$,
    ylabel={$x^2$},
    xmin=-4, xmax=4,
    ymin=0, ymax=4,
    width=0.8\textwidth,
    height=5cm
]
\addplot[mark=noner,smooth,domain=-1:1]{x^2};
\end{axis}
\end{tikzpicture}
hochl
  • 12,524
  • 10
  • 53
  • 87

1 Answers1

2

For simple functions you don't need Gnuplot, pgfplots is able to do the calculations in LaTeX. See the examples in section 4.2.4 of the pgfplots manual; the first example draws the function xx² − 4.

Philipp
  • 48,066
  • 12
  • 84
  • 109
  • Added a first solution to the question, but it could be improved :) +1 Thanks anyways – hochl Feb 09 '11 at 23:57
  • What is the problem with the solution you posted? Why is it too "fancy"? Please describe how it should actually look like. – Philipp Feb 10 '11 at 23:46
  • I think I'm just overwhelmed by the endless possibilities to configure PGF, plus it seems my PGF version is either out-of-date or horribly broken, because most options just don't work (it says ... /tikz/legend reversed not found or something similar for 75% of everything that's described in the PGF plots manual -- this drives me crazy! Is that a Debian problem? – hochl Feb 11 '11 at 09:42
  • I have no idea where the problem lies, but versions of PGF and PGFPlots are only released if they are pretty stable, and there has never been a "horribly broken" version of PGF. Maybe you're looking at a documentation for a different version. Make sure that you use your system's `texdoc` to access the documentation, not some (probably newer) online version. – Philipp Feb 11 '11 at 14:26