我有一个节目有几个情节像问题如何在 tikz 图的点上显示标签 在同一个轴上,我正在尝试创建一种方法来分解重复部分,这样我可以使用一个简短的命令,而不是复制粘贴重复部分。
我尝试创建一个新环境,并将选项部分分解为新命令,但出现编译错误。有什么办法吗?
什么不起作用:在序言中
\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
};
}
在文档正文中:
\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}
有效的方法:
\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}
答案1
只需使用样式即可。我不会尝试创建新环境,尽管这需要付出一些努力。您可以通过覆盖或使用来自定义样式/.append style
。当然,您也可以在后面添加更多键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}