6

I'd like to generate PDF's for some ad hoc screenshots, but I'm getting an undefined control error. I tried \usepackage{amsmath} after the line \usepackage{pgfplots} but that didn't work. I thought maybe that package was required for use of \frac{x}{2}

I'm using TeXworks with the following to try and generate a generic plot of x/2:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[>=stealth]
    \begin{axis}[
        xmin=-10,xmax=10,
        ymin=-10,ymax=10,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$x$},
        ylabel={$y$},
        ]
        \addplot[no marks,blue,<->] expression[domain=-10:10,samples=100]{\frac{x}{2}} 
                    node[pos=0.65,anchor=south west]{$y=\frac{x}{2}$}; 
    \end{axis}
\end{tikzpicture}
\end{document}

The error is on the node line 16 I believe, but I can't tell if it's syntax or otherwise.

! Undefined control sequence.
\pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@ 

l.16 ...=0.65,anchor=south west]{$y=\frac{x}{2}$};

Please consider me very new to LaTeX. I guess I'm assuming there is a missing package, since I can successfully plot simple linear graphs that don't include \frac.

1
  • I realized as I was reading John's answer that the node was the annotation and should use \frac while the plot should be the generic expression.
    – David Fox
    Jun 4, 2013 at 16:13

1 Answer 1

8

You should write x/2 instead of \frac{x}{2}.

  • In the equation of the graph, you want a mathematical expression that PGFplots can understand. The syntax for division in PGFplots is simply /.
  • In the label for the graph, you just want a piece of text, so y=\frac{x}{2} is fine here.

Fixed code

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[>=stealth]
    \begin{axis}[
        xmin=-10,xmax=10,
        ymin=-10,ymax=10,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$x$},
        ylabel={$y$},
        ]
        \addplot[no marks,blue,<->] expression[domain=-10:10,samples=100]{x/2} 
                    node[pos=0.65,anchor=south west]{$y=\frac{x}{2}$}; 
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • I see--the node is the annotation which is essentially text while the addplot is the expression.
    – David Fox
    Jun 4, 2013 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.