# TikZ inside pgfplot axis environment

I am trying to use the TkZ command MarkRightAngle in an axis environment, however I cannot get it to work. I get a "Missing character: There is no ; in font nullfont!" error and "Line 21: Package pgf Error: No shape named B is known.". An possible solution would be matching the axis and canvas scales (but I have no idea how to do that) and adding the commands outside of axis environment.

\documentclass{article}
\usepackage{mathptmx}
\usepackage{tikz, ifthen}
\usetikzlibrary{arrows,shapes,backgrounds,patterns,decorations.pathreplacing, decorations.pathmorphing,decorations.markings,shadows,shapes.misc,calc,%
positioning,% intersections}
\usepackage{color}
\usepackage{tikzpagenodes}
\usepackage{tkz-euclide}
\usepackage[active,pdftex,tightpage]{preview}
\usepackage{pgfplots}
\usetkzobj{all}
\PreviewEnvironment[]{tikzpicture}
\PreviewEnvironment[]{pgfpicture}
\DeclareSymbolFont{symbolsb}{OMS}{cmsy}{m}{n}
\SetSymbolFont{symbolsb}{bold}{OMS}{cmsy}{b}{n}
\DeclareSymbolFontAlphabet{\mathcal}{symbolsb}
\begin{document}
\begin{tikzpicture}
\begin{axis}[  axis lines =center,
disabledatascaling,
anchor=origin,
xlabel = $x$,    ylabel =$y$,
ymax=4, ymin=-8, xmax =8, xmin=-4,
ytick={-6, -4, ...,4},xtick={-4, -2, ...,6},]

\addplot [   color=black, dashed,]
coordinates {(-2,-5) (7,-5)} ;

\addplot [ color=black, dashed,]
coordinates {(7,-5) (7,-2)} node[label=above:{$T(7,-2)$},circle, fill, inner sep=2pt] {};

\addplot [color=black,]
coordinates {(7,-2) (-2,-5)} node[label=below:{$S(-2,-5)$},circle, fill, inner sep=2pt] {};

\tkzDefPoint(7,-2){A};
\tkzDefPoint(-2,5){B};
\tkzDefPoint(7,-5){C};
\tkzMarkRightAngle(A,B,C);
\end{axis}
\end{tikzpicture}
\begin{document}

• Welcome to TeX.SX! Please highlight code by inserting 4 spaces in front of each line (or you mark everything and press the {}-button. In order to get propper help here, you should complete your example to make it compilable. It should start with \documentclass{ and end with \end{document}, should be as short as possible and show your error. – LaRiFaRi Jul 8 '14 at 8:58

## 2 Answers

tikz inside plot axis is not a general problem. However, tkz-euclide has a completely different set-up for coordinates etc. For labelling the right-angle it is easier just to use the calc library. I have named the two nodes you are interested in by adding name=A and name=B to their options. Then I have drawn the right angle symbol via

\coordinate (X) at (A |- B);
\draw ($(X)!5pt!(A)$) -| ($(X)!5pt!(B)$);


You can change 5pt to some other dimension to get a different size mark.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\begin{axis}[axis lines =center,
disabledatascaling,
anchor=origin,
xlabel = $x$, ylabel =$y$,
ymax=4, ymin=-8, xmax=8, xmin=-4,
ytick={-6, -4, ...,4}, xtick={-4, -2, ...,6}]
\addplot [color=black, dashed] coordinates {(-2,-5) (7,-5)} ;
\addplot [ color=black, dashed] coordinates {(7,-5) (7,-2)}
node[label=above:{$T(7,-2)$},circle, fill, inner sep=2pt,name=A] {};
\addplot [color=black] coordinates {(7,-2) (-2,-5)}
node[label=below:{$S(-2,-5)$},circle, fill, inner sep=2pt,name=B]
{};
\coordinate (X) at (A |- B);
\draw ($(X)!5pt!(A)$) -| ($(X)!5pt!(B)$);
\end{axis}
\end{tikzpicture}

\end{document}

• Thank you very much, you just killed 2 birds with one stone. I wanted to use node names for another diagram today with a separate issue. – Nkosi Jul 9 '14 at 13:14

I found an alternate solution. The above solution was giving me issues if the right angle was not on a horizontal or vertical line.

\documentclass{article}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymax=4, ymin=-8, xmax=8, xmin=-4,
ytick={-6, -4, ...,4}, xtick={-4, -2, ...,6}]

\addplot [dashed] coordinates {(-2,-5) (7,-5)} ;

\addplot [dashed] coordinates {(7,-5) (7,-2)} node[pos=0,name=R]{}  node[label={[shift={(0,-0.2)}]{$T(7,-2)$}},,circle,fill,inner sep=1pt,name=T] {};

\addplot  coordinates {(7,-2) (-2,-5)}  node[pos=1,label={[shift={(-0.5,-1.3)}]{$S(-2,-5)$}}, ,circle,fill,inner sep=1pt,name=S] {};

\coordinate (U) at ($(S)!(T)!(R)$); %using node R distorts the right angle symbol

\end{axis}
\tkzMarkRightAngle(T,U,S)
\end{tikzpicture}
\end{document}