2

Consider the following MWE:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+ [only marks,
nodes near coords,
point meta=explicit symbolic,
every node near coord/.style={
anchor=north east,
yshift=-1pt,
xshift=2pt}] table [x index=0,y index=1,meta index=1] {
0 0 -3 
1 1 -2
2 2 -1
};

\addplot+ [only marks,
nodes near coords,
point meta=explicit symbolic,
every node near coord/.style={
anchor=south east,
xshift=2pt,
yshift=1pt}] table [x index=0,y index=1,meta index=2] {
0 0 -3 
1 1 -2
2 2 -1
};
\end{axis}
\end{tikzpicture}
\end{document}

Which produces:

Result

I have quite a few plots of this type with values written above left and below left of the coordinates.

I would like to put the options to \addplot+ in a style of some sort and simplify the code to something like:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{southwestpoint/.style={only marks,
nodes near coords,
point meta=explicit symbolic,
every node near coord/.style={
anchor=north east,
yshift=-1pt,
xshift=2pt}}}

\pgfplotsset{northwestpoint/.style={only marks,
nodes near coords,
point meta=explicit symbolic,
every node near coord/.style={
anchor=south east,
xshift=2pt,
yshift=1pt}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+ [southwestpoint] table [x index=0,y index=1,meta index=1] {
0 0 -3 
1 1 -2
2 2 -1
};

\addplot+ [northwestpoint] table [x index=0,y index=1,meta index=2] {
0 0 -3 
1 1 -2
2 2 -1
};
\end{axis}
\end{tikzpicture}
\end{document}

But this gives an error:

Runaway argument? {northwestpoint.style={only marks, nodes near coords, point meta=exp\ETC. ! File ended while scanning use of \pgfkeys@@qset. \par

I'm guessing there is an error with the expansion but I'm not really an expert in TeX programming so I haven't been able to solve it. I also tried using with \def\northwestpoint{...} but got other errors.

How can I create an addplot-style?

2

You're just missing one closing brace in the definition of northwestpoint. Note that for southwestpoint you have three closing braces at the end of the \pgfplotsset, but in northwestpoint there are two.

Here's a slightly different version, which makes use of the fact that you can nest styles, and also prints the numbers in math mode, so you get proper minus signs.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{
point/.style={
  only marks,
  nodes near coords={$\pgfmathprintnumber\pgfplotspointmeta$},
  point meta=explicit symbolic,
},
southwestpoint/.style={
  point,
  every node near coord/.style={
    anchor=north east,
    yshift=-1pt,
    xshift=2pt}
},
northwestpoint/.style={
  point,
  every node near coord/.style={
    anchor=south east,
    xshift=2pt,
    yshift=1pt}
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+ [southwestpoint] table [x index=0,y index=1,meta index=1] {
0 0 -3 
1 1 -2
2 2 -1
};

\addplot+ [northwestpoint] table [x index=0,y index=1,meta index=2] {
0 0 -3 
1 1 -2
2 2 -1
};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • You're right.. I've been trying for hours, probably with different syntax errors, this shows how valuable a proper MWE can be.. Thanks for your help and for the additional suggestions for improving the code!! – user49901 May 9 '16 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy