1

I have got a show a few plots like in question How to display labels on points of a tikz plot on the same axis, and I am trying to create a way to factor out the repeating part, so that I could use one short command instead of copy-pasting the repeating part.

I have tried to create a new environment, and to factor out the options part in a new command, but I am getting compilation errors. Is there any way to do it?

What does not work: in preamble

\newcommand{\plotoptions}{mark=*,
    visualization depends on=\thisrow{alignment} \as \alignment,
    nodes near coords, % Place nodes near each coordinate
    point meta=explicit symbolic, % The meta data used in the nodes is not explicitly provided and not numeric
    every node near coord/.style={anchor=\alignment} % Align each coordinate
    }
\usepackage{environ}% http://ctan.org/pkg/environ
\NewEnviron{hplot}{%
  \addplot[mark=*,
    visualization depends on=\thisrow{alignment} \as \alignment,
    nodes near coords, % Place nodes near each coordinate
    point meta=explicit symbolic, % The meta data used in the nodes is not explicitly provided and not numeric
    every node near coord/.style={anchor=\alignment} % Align each coordinate 
    ] table [% Provide data as a table
     meta index=2 % the meta data is found in the third column
     ] {x       y       label       alignment
        \BODY% regular \BODY
       };
}

in the body of the document:

\begin{tikzpicture}
    \begin{axis}[xmin=-10, xmax=15, ymin=0, ymax=15, grid style=dashed,
                 xmajorgrids=true, ymajorgrids=true, width=0.8\textwidth,scale only axis]
        \begin{hplot}
            -5    3    $A_1$      0
            -3    1    $B_1$     90
            -1    1    $M_1$    270
             1    1    $B'_1$    90
             3    1    $A'_1$   180
         \end{hplot}
   \addplot[\plotoptions] table [meta index=2] {
        x       y       label       alignment
       -5       3    $A_1$     0
       -3       1    $B_1$    90
       -1       1    $M_1$   270
        1       1   $B'_1$    90
        3       1   $A'_1$   180};

\end{axis}

\end{tikzpicture}

What works:

\begin{tikzpicture}
    \begin{axis}[xmin=-10, xmax=15, ymin=0, ymax=15, grid style=dashed,
                 xmajorgrids=true, ymajorgrids=true, width=0.8\textwidth,scale only axis]
         \addplot[mark=*,
                  visualization depends on=\thisrow{alignment} \as \alignment,
                  nodes near coords, % Place nodes near each coordinate
                  point meta=explicit symbolic, 
                  every node near coord/.style={anchor=\alignment} 
                 ] table [% Provide data as a table
                          meta index=2 % the meta data is found in the third column
                         ] {x       y       label       alignment
                           -6    1    $A_0$      0
                           -3    1    $B_0$      90
                            0    1    $M_0$     270
                            3    1    $B'_0$     90
                            6    1    $A'_0$    180};
    \end{axis}
Yulia V
  • 115
  • 4
  • You should store the common options in a style, and not in a macro. Then you can just use the style to install the options. –  Dec 18 '20 at 18:42

1 Answers1

3

Just use a style for that. I would not try to create a new environment, even though it can be done with some efforts. You can customize the style by either overwriting it or using /.append style. Of course, you can also just add more keys after my plot options.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17} 
\pgfplotsset{my plot options/.style={mark=*,
    visualization depends on=\thisrow{alignment} \as \alignment,
    nodes near coords, % Place nodes near each coordinate
    point meta=explicit symbolic, % The meta data used in the nodes is not explicitly provided and not numeric
    every node near coord/.style={anchor=\alignment} % Align each coordinate
    }}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmin=-10, xmax=15, ymin=0, ymax=15, grid style=dashed,
                 xmajorgrids=true, ymajorgrids=true, width=0.8\textwidth,scale only axis]
   \addplot[my plot options] table [meta index=2] {
        x       y       label       alignment
       -5       3    $A_1$     0
       -3       1    $B_1$    90
       -1       1    $M_1$   270
        1       1   $B'_1$    90
        3       1   $A'_1$   180
     };
\end{axis}

\end{tikzpicture} \end{document}

enter image description here