# TikZ does not draw functions anymore

I am using the latest TeXstudio version with PdfLaTeX as my compiler. I have this commands enabled: pdflatex.exe -synctex=1 -shell-escape -interaction=nonstopmode %.tex. The document is getting created but TikZ does not draw the functions. The Code works in Overleaf (online editor) perfectly fine with the drawn functions so there must be a problem with my PdfLaTeX or my TeXstudio. I tried reinstalling MikTeX and TeXstudio. I have even installed on a virtual machine to look if it works there but without success. It also does not work in WinShell. I also tried to compile the document with LuaLaTeX but again, without success. Furthermore, Overleaf's default compiler was set to PdfLaTeX. I thinks that there is something wrong with my PdfLaTeX but I have no idea left for a possibility to solve that problem. My Code is:

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{through,calc,arrows,snakes}

\pgfplotsset{compat=1.16}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.8, x=2cm, y=2cm, axis/.style={ ->, >=stealth'}]
\draw[dotted,color=black, xstep=1cm, ystep=1cm] (-1.9,0) grid (1.9,3.9);
\draw[axis] (-2,0) -- (2,0) node[above] {\small $x$};
\draw[axis] (0,0) -- (0,4) node[right] {\small $y$};
\foreach \x/\xtext in {-2/-2, -1/-1, 0/0, 1/1} \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below, fill=white] {\small \xtext};
\foreach \y/\ytext in {1/1, 2/2, 3/3}  \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left, fill=white] {\small \ytext};
% functions
\draw[color=red, thick] plot[domain=-2:2, samples=250] function{x*x} node[right] {\small $f(x)$};
\end{tikzpicture}
\end{center}
\end{document}


These are the results:

As you can see on the left it looks perfectly fine (Overleaf) but on the right (TeXstudio) it doesnt. I want to continue writing in TeXstudio not in Overleaf so it would be great if this is working.

• Do you have gnuplot installed and is it in your PATH? Aug 18, 2020 at 10:55
• You are loading an advanced graphing package, \usepackage{pgfplots} and even set the compat. -but you are not using it at all. Why draw axis, grid, scale, ticks and the plot manually? Search for PGFPlot examples here or google. Aug 18, 2020 at 11:54
• Your code work for me on TeXStudio with MiKTeX and your compilation command. Aug 19, 2020 at 7:29

This works in local (recent) MikTeX installation as well in Overleaf:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows,
calc,
decorations.pathmorphing,
through}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.8, x=2cm, y=2cm, axis/.style={ ->, >=stealth'}]
\draw[dotted, xstep=1cm, ystep=1cm] (-1.9,0) grid (1.9,3.9);
\draw[axis] (-2,0) -- (2,0) node[above] {\small $x$};
\draw[axis] ( 0,0) -- (0,4) node[right] {\small $y$};
%
\foreach \x/\xtext in {-2/-2, -1/-1, 0/0, 1/1}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below, fill=white] {\small \xtext};
\foreach \y/\ytext in {1/1, 2/2, 3/3}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left, fill=white] {\small \ytext};
% functions
\draw[color=red, thick] plot[domain=-2:2, samples=250]
(\x,{\x*\x}) node[right] {\small $f(x)$};
\end{tikzpicture}
\end{center}
\end{document}


• But the code is different, I actually want to know what is wrong with my PdfLaTeX or TeXstudio and how I can fix this problem, not bypass it... Aug 18, 2020 at 11:57

Your code is calling gnuplot. It need both gnuplot installed and -shell-escape (or enable-write18) compilation option.

• I have installed gnuplot but do I have to do something with the gnuplot console? When I compile the code, TeXstudio says that the command "gnuplot" is not found. What do I have to do? Aug 18, 2020 at 12:43
• did you add the path to the gnuplot.exe to the systèm PATH ? (eg "c:\Program Files\gnuplot\bin") Aug 18, 2020 at 17:45