# Optimal way to use TikZ for a graphics

I would like to have your input on the following example. I simply want to show a few points, draw connections between them and label/color them accordingly.

Using TSX I came up with the following but am not sure if this is -- in the long run -- the best way. I like the tupel based input, but would like to use names to simply drawing the line between them. Can the example be upgraded to include something like this?

\documentclass[12pt,english]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{frame}[plain]
\hspace*{-1.5em}\begin{tikzpicture}
\begin{axis}[
domain=0:10,
axis lines = center,
height=10cm, width=12cm, grid=major,grid style={dashed, gray!30},
xmin=0, xmax=12, ymin=0, ymax=9,xtick={1,2,...,11},ytick={1,2,...,10}]
\addplot+[red,only marks,mark=*,mark options={scale=2, fill=red},text mark as node=true] coordinates {
(1,2)
(3,1)
(4,3)
(5,2)
(2,3)
};
\addplot+[red,only marks,mark=*,mark options={scale=2, fill=red},text mark as node=true] coordinates {
(7,7)
(8,8)
(9,5)
(11,6)
(10,8)
};

\draw (axis cs:0,0) -- (axis cs:2,2);
\filldraw (axis cs:5,5) circle (3pt);
\node[anchor=north] at (axis cs:7,6.9) {1};
\node[anchor=south] at (axis cs:5,1) {$f(\theta_A, x)$};
\end{axis}

\end{tikzpicture}
\end{frame}
\end{document}

• What do you mean when you say "...but would like to use names to simply drawing the line between them"? – Alenanno Aug 28 '16 at 14:17
• Like node A is on (1,0), B is on (2,2), draw line from A to B. – Uwe Ziegenhagen Aug 28 '16 at 14:23
• Where are these nodes in your code? I don't see anything with those coordinates. – Alenanno Aug 28 '16 at 14:27
• The coordinates were just examples. Maybe my question can be expressed as "Is there a way to assign labels when using the addplot (x,y) tupel syntax?" – Uwe Ziegenhagen Aug 28 '16 at 15:44
• So do you want to link the red dots? If so, all of them or arbitrarily? – Alenanno Aug 28 '16 at 15:47

Well, if you want to simply name the circles so you can act on them, you would need to use nodes, because simple shapes do not have aliases. An easy solution to achieve this, in my opinion, would be to switch to regular Tikz. With a \foreach, you can create many nodes and assign names to each using a counter, which in our case will be like n\n, resulting in nodes named n1, n2, n3 and so on.

## Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\newcommand\labelit[3][below]{%
\node[label={#1:#3}] at (#2) {};
}

\def\maxc{11}

\begin{document}
\begin{tikzpicture}
\draw[-Stealth, shorten >=-1cm] (0,0) -- (\maxc,0);
\draw[-Stealth, shorten >=-1cm] (0,0) -- (0,\maxc-2);
\foreach \x [count=\y] in {1,...,\maxc}{
\draw (\x,.1) -- (\x,-.1) node[below] {\x};
\ifnum\x<10\relax
\draw (.1,\y) -- (-.1,\y) node[left] {\y};
\draw[dashed, gray!30] (0,\y) -- (\maxc+1,\y);
\fi
\draw[dashed, gray!30] (\x,0) -- (\x,\maxc-1);
}

\foreach \point [count=\n] in {%
(1,2),  (3,1),
(4,3),  (5,2),
(2,3),  (7,7),
(8,8),  (9,5),
(11,6), (10,8)
}{%
\node[circle, fill=red, inner sep=3pt] (n\n) at \point {};
}

% label points, arguments are: [position]{node}{label}
\labelit{n4}{$f(\theta_A, x)$}