1

I have the code for a triangle on the Cartesian plane. There seems to be two commands that TikZ is not able to compile. The first line is \addplot[samples=501,dashed,domain=1:3] {3,x}; which should instruct pgfplots to draw a vertical line segment for the altitude of the triangle. The second line is \tkzMarkRightAngle(A,P,B); which should instruct TikZ to draw the right angle mark at the base of the altitude. (I put a % in front of these lines so that the code would compile.)

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{mathtools,systeme,array}

\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=6in,axis equal image,clip=false,
    axis lines=middle,
    xmin=-3,xmax=5,samples=501,
    xlabel=$x$,ylabel=$y$,
    ymin=-3,ymax=3,
    restrict y to domain=-3:3,
    enlargelimits={abs=0.25cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xtick={\empty},ytick={\empty},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=501,domain=-2:3,blue] {0.4*x + 1.8};
\addplot[samples=501,domain=3:4,blue] {-2*x + 9};
\addplot[samples=501,domain=-2:4,blue] {1};
%\addplot[samples=501,dashed,domain=1:3] {3,x};
\end{axis}
(-2,1)node[label=left:$A$](A){};
(3,3)node[label=above right:$B$](B){};
(4,1)node[label=right:$C$](C){};
(3,1)node(P){};
%\tkzMarkRightAngle(A,P,B);
\end{tikzpicture}

\end{document}
6
  • For your first question, replace the \addplot line with \draw[dashed] (axis cs:3,1) -- (axis cs:3,3). Commented Jan 4, 2015 at 22:02
  • @Paul Gessler Why is the addplopt command not being compiled? I have used this command in many other diagrams to draw vertical lines.
    – Adelyn
    Commented Jan 4, 2015 at 22:10
  • I'd like to see an example of that working, because it's really not designed to work that way at all... For one thing, the contents inside the curly braces are supposed to be one expression defining y in terms of x, not a parametric definition of x- and y-coordinates as you're attempting here. Commented Jan 4, 2015 at 22:17
  • @Paul Gessler Look for Using the \draw command to plot a vertical line at this web site. I just posted the code.
    – Adelyn
    Commented Jan 4, 2015 at 22:28
  • OK, but a hyperlink would have been much easier. Your posting there is not the same thing you're doing here. Take note of the difference between (6,x) and {3,x}. The choice of parentheses vs. braces is significant because it puts pgfplots in different "modes", if you will. Commented Jan 4, 2015 at 22:33

1 Answer 1

2

The first question was solved in the comments: replacing {3,x} with (3,x) allows the vertical line to be plotted in a more elegant way than the \draw[dashed] (axis cs:3,1) -- (axis cs:3,3); which I originally suggested. However, why draw 501 samples of the line when 2 will do? I've edited this in the code below.

The second question is solved by recognizing that the tkz-euclide macros expect to operate on TikZ coordinates rather than nodes. I also moved these inside the axis environment to use axis cs to place the coordinates appropriately.

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{mathtools,systeme,array}

\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=6in,axis equal image,clip=false,
    axis lines=middle,
    xmin=-3,xmax=5,samples=501,
    xlabel=$x$,ylabel=$y$,
    ymin=-3,ymax=3,
    restrict y to domain=-3:3,
    enlargelimits={abs=0.25cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xtick={\empty},ytick={\empty},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=501,domain=-2:3,blue] {0.4*x + 1.8};
\addplot[samples=501,domain=3:4,blue] {-2*x + 9};
\addplot[samples=501,domain=-2:4,blue] {1};
\addplot[samples=2,dashed,domain=1:3] (3,x); % solved in the comments to the question
\coordinate (P) at (axis cs:3,1);
\coordinate[label=left:$A$] (A) at (axis cs:-2,1);
\coordinate[label=above right:$B$] (B) at (axis cs:3,3);
\coordinate[label=right:$C$] (C) at (axis cs:4,1);
\end{axis}
\tkzMarkRightAngle(A,P,B);
\end{tikzpicture}

\end{document}

enter image description here

10
  • This is the diagram that I wanted. Thanks. For the code, I would like to have (3,1)coordinate(P); and (-2,1)coordinate(A)node[left]{$A$}; outside the axis environment. The coordinates are filed in memory correctly, but the placement of the labels for the vertices are not displayed properly. What is the code to have "A" displayed to the left of the vertex at (-2,1) and to have "B" displayed to the "above right" of (3,3)?
    – Adelyn
    Commented Jan 5, 2015 at 18:06
  • @Adelyn I'm not sure what your question is asking. The labels are already where you want them, are they not? Commented Jan 5, 2015 at 18:24
  • Yes, they are where I wanted them. I am interested in putting the commands for the vertices and the labels for the vertices outside the axis environment. That is the reason that I use different commands: (-2,1) coordinate (A) node[left]{$A$}; and (3,3) coordinate (B) node[left]{$B$}; and (3,1) coordinate (P);. These may not be quite right, as I had suggested. I get an error message with the code \tkzMarkRightAngle(A,P,B);. It says "no shape P found."
    – Adelyn
    Commented Jan 5, 2015 at 18:30
  • @Adelyn, I don't understand. The commands you have don't work. The commands I have provided do. Why do you want to change back to the ones that don't work? The commands must be inside of the axis environment in order to have access to that coordinate system (which may change with scaling, axis size, etc.). Commented Jan 5, 2015 at 19:09
  • I would like to get familiar with TikZ commands, too. For example, the following code also gives me the picture that I want (without the axes).
    – Adelyn
    Commented Jan 5, 2015 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .