4

As it can be seen in the figure i added a line from the first intersection (y=2x and y=6-x) to x(2). But i couldn't add a line from second intersection (y=x and y=6-x) to x(3). i tried intersection-2 but it gave an error. i want to make this using intersection command because there may be new situations in which i can't find the intersection exactly.

What should i do?

enter image description here

\documentclass[11pt,a4paper]{article}%
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{tikz,pgf,pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,intersections}

    \begin{document}
    \begin{tikzpicture}
    \begin{axis}[axis lines=middle,
         height=7cm, width=7cm,
                xlabel=$x$,
                ylabel=$y$,
                enlargelimits,
                ytick=\empty,
                xtick={2,3},          
    every axis y label/.style={at=(current axis.above origin),anchor=south},
    every axis x label/.style={at=(current axis.right of origin),anchor=west}],
    \addplot[name path=f,blue,domain={0:2}] {2*x} node[pos=.8, above]{};
    \addplot[name path=h,blue,domain={2:3}] {6-x} node[pos=.8, above,,rotate=-15]{};
    \addplot[name path=g,blue,domain={0:3}] {x} node[pos=.8, below,rotate=30]{};
    \addplot[name path=bos,white,domain={2:4}] {x/2} node[pos=.8, below,rotate=30]{};

    \addplot[pattern=north west lines, pattern color=brown!50]fill between[of=f and g, soft clip={domain=0:2}];
    \addplot[pattern=north west lines, pattern color=brown!50]fill between[of=h and g, soft clip={domain=2:3}];
    \node[coordinate,pin=95:{\footnotesize{$y=2x$}}] at (axis cs:1,2){};
    \node[coordinate,pin=-45:\footnotesize{$y=x$}] at (axis cs:0.8,0.8){};
    \node[coordinate,pin=40:\footnotesize{$y=6-x$}] at (axis cs:2.5,3.5){};
    \draw [name intersections={of=f and h},ultra thin,gray] ({axis cs:0,0}-|intersection-1) -- (intersection-1);
    \end{axis}
    \end{tikzpicture}
    \end{document}
2
  • They only intersect at one point.
    – percusse
    May 18 '14 at 20:33
  • right but how to add. i tried intersection-1 as well but it didn't work.
    – mert
    May 18 '14 at 20:36
5

You need to compute the intersections independently to use them in the same path. Otherwise you can use the name intersections on each path with different path names.

One option is to use different name prefixes.

EDIT About problems;

  1. You are intersecting the lines at their endpoints and this causes numerical instabilities. Your redundant path named boş causes the figures drawn smaller and that shrinks the paths. Instead you can increase the domain of lines slightly to ensure a proper intersection.
  2. In order to extend the axis you can use xmax key instead as I did below. Or you can use clip=false if you need that node to be printed
  3. If you don't want to draw anything, instead of white use draw=none.

\documentclass[11pt,a4paper]{article}%
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{tikz,pgf,pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,intersections}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
     height=7cm, width=7cm,
            xlabel=$x$,
            ylabel=$y$,
            enlargelimits,
            ytick=\empty,
            xtick={2,3},xmax=4,% <---Added xmax instead 
every axis y label/.style={at=(current axis.above origin),anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west}],
\addplot[name path=f,blue,domain={0:2}] {2*x} node[pos=.8, above]{};
\addplot[name path=h,blue,domain={2:3.005}] {6-x} node[pos=.8, above,,rotate=-15]{};
\addplot[name path=g,blue,domain={0:3.005}] {x}   node[pos=.8, below,rotate=30]{};
%\addplot[name path=bos,draw=none,domain={2:4}] {x/2} node[pos=.8, below,rotate=30]{};

\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=f and g, soft clip={domain=0:2}];
\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=h and g, soft clip={domain=2:3}];
\node[coordinate,pin=95:{\footnotesize{$y=2x$}}] at (axis cs:1,2){};
\node[coordinate,pin=-45:\footnotesize{$y=x$}] at (axis cs:0.8,0.8){};
\node[coordinate,pin=40:\footnotesize{$y=6-x$}] at (axis cs:2.5,3.5){};
\draw [name intersections={of=f and h,name=fh},
       name intersections={of=h and g,name=hg},
       ultra thin,gray] 
       (hg-1) -- (axis cs:3,0)
       (fh-1) -- (axis cs:2,0)
    ;
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

8
  • sorry. i added \draw [name intersections={of=f and h,name ... to my original code but it gave an error that is No shape named hg-1 is known. . where is the problem?
    – mert
    May 18 '14 at 20:59
  • @mert whatever you put in the name=, it becomes the prefix. If you said fh you need to use fh-1
    – percusse
    May 18 '14 at 21:01
  • i copied what you write and pasted. it is just as you write but it is still giving error.
    – mert
    May 18 '14 at 21:05
  • i deleted \draw [name intersections={of=f and h},ultra thin,gray] ({axis cs:0,0}-|intersection-1) -- (intersection-1); and added \draw [name intersections={of=f and h,name=fh}, name intersections={of=h and g,name=hg}, ultra thin,gray] (hg-1) -- (axis cs:3,0) (fh-1) -- (axis cs:2,0) ;
    – mert
    May 18 '14 at 21:09
  • @mert That's strange. Do you completely copy paste the whole code in a separate document? It should work I guess. I don't have a special TeX distro.
    – percusse
    May 18 '14 at 21:15

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.