15

I have a simple drawing via Tikz. The picture in the standalone mode is

enter image description here

But when I add it inside the documentation as script not pdf or image, the output is

enter image description here

It is messed up. I'm not sure what may yield this kind of problems. Any suggestions?

This is the MWE.

\documentclass{article}
\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=-8, xmax=10,ymin=-8, ymax=30,axis lines=middle,very thick,ticks=none,xlabel=$x$, ylabel=$y$]  
\addplot[samples=100, domain=-8:4] {5*x}   node[left]{$y_2$}; 
\addplot[samples=50, domain=-8:4]  {2*x+5} node[right]{$y_1$}; 
\draw [thick] (axis cs: 2.2,11) to[bend right] node[scale=.8,above,xshift=-2mm]{$a$} (1,7) ;
\end{axis} 
\end{tikzpicture}

\end{document}
24

I'm guessing you had \pgfplotsset{compat=1.11} or a higher compat version in the standalone file. With that setting, axis cs: is the default, so you don't have to use it for all your coordinates.

In your code you use axis cs: for the first coordinate in the \draw, but not in the second.

Add a compat setting of 1.11 or higher in the preamble, or add axis cs: to the second coordinate.

Note also that using 100 samples for a linear function is sort of pointless, 2 is enough.

output of code

\documentclass{article}
\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-8, xmax=10,ymin=-8, ymax=30,axis lines=middle,very thick,ticks=none,xlabel=$x$, ylabel=$y$]  
\addplot[samples=100, domain=-8:4] {5*x}   node[left]{$y_2$}; 
\addplot[samples=50, domain=-8:4]  {2*x+5} node[right]{$y_1$}; 
\draw [thick] (axis cs: 2.2,11) to[bend right] node[scale=.8,above,xshift=-2mm]{$a$} (axis cs:1,7) ; % <--- added axis cs:
\end{axis} 
\end{tikzpicture}

\end{document}

For fun, a different method for indicating an angle is shown below, using a couple of named coordinates, the intersections library to find the intersection, and the angles and quotes library to draw the angle.

output of code

\documentclass{article}
\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=1.15}
\usetikzlibrary{angles,quotes,intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
   xmin=-8, xmax=10,
   ymin=-8, ymax=30,
   axis lines=middle,
   very thick,
   ticks=none,
   xlabel=$x$,
   ylabel=$y$
]  
\addplot[name path=a,samples=2, domain=-8:4] {5*x}   coordinate[label=left:$y_2$] (y2); 
\addplot[name path=b,samples=2, domain=-8:4]  {2*x+5} coordinate[pos=0] (y1) node[right]{$y_1$}; 
\draw [name intersections={of=a and b,by={o}}]
    pic[draw,angle radius=4mm,angle eccentricity=1.3,"$a$"] {angle=y2--o--y1};
\end{axis} 
\end{tikzpicture}

\end{document}
| improve this answer | |
  • I got this error Package pgfplots Error: Sorry, 'compat=1.15' is unknown in this context. Please use at most 'compat=1.14'. \pgfplotsset{compat=1.15} Using 1.14 didn't solve the problem. – CroCo Dec 3 '17 at 21:23
  • Oops, I've forgotten to add axis cs: in the second coordinate which solved the problem. The code worked without adding \pgfplotsset though. Thank you. – CroCo Dec 3 '17 at 21:26
  • @CroCo Your code works for me if I add \pgfplotsset{compat=1.11} (or 1.12, 1.13, 1.14 or 1.15). But without that setting, you must specify axis cs: in coordinates used by \draw etc. As I said, with that setting axis cs: is the default. – Torbjørn T. Dec 3 '17 at 21:35

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.