\newcommand Tikz 参数问题

\newcommand Tikz 参数问题

我有这两个命令来在图上从 y 读取 x 或从 x 读取 y。

问题在于标签是 180:#3,其上方位置位于 #3 东侧位置。

愿望(不太重要):如何将两个命令合并为一个?

\newcommand{\ReadY}[3][180]{%
    \draw[very thin, dashed] (axis cs:#2,0)|-(axis cs:0,#3) coordinate (Y) ;
    \node[small dot,pin=#1:#3] at (Y) {};
}

\newcommand{\ReadX}[3][270]{%
    \draw[very thin, dashed] (axis cs:0,#2)-|(axis cs:#3,0) coordinate (Y) ;
    \node[small dot,pin=#1:#3] at (Y) {};
}

须更正为:

\newcommand{\ReadY}[3][180]{%
    \draw[very thin, dashed] (axis cs:#2,0)|-(axis cs:0,#3) coordinate (Y) ;
    \node[small dot,pin={[pin position={#1}]{#3}}] at (Y) {};
}

\newcommand{\ReadX}[3][270]{%
    \draw[very thin, dashed] (axis cs:0,#2)-|(axis cs:#3,0) coordinate (Y) ;
    \node[small dot,pin={[pin position={#1}]{#3}}] at (Y) {};
}

可以将这两个命令合并为一个吗?

我是说 :

  • 第一个参数是最佳角度,

  • 第二个是我知道的价值

  • 函数给出的值中的第三个,并在图上显示为红色(图像或先行项)

标签必须位于这个节点上。

最小示例:

 \documentclass{standalone}
 \usepackage{pgfplots}

 % boite de couleur avec un filet autour
 \tikzset{%
     boxcolor/.style args={#1}{%
   fill=#1!25!white,rectangle,rounded corners=4pt,font=\footnotesize,draw=#1},
    small dot/.style={fill=black,circle,scale=0.3}}


\newcommand{\ReadY}[3][175]{%
    \draw[very thin, dashed] (axis cs:#2,0)|-(axis cs:0,#3) coordinate (Y) ;
    \node[small dot,pin={[boxcolor=green,pin position={#1}]{%
            \pgfmathprintnumber[use comma]{#3}}}] at (Y) {};
}

\newcommand{\ReadX}[3][265]{%
    \draw[very thin, dashed] (axis cs:0,#2)-|(axis cs:#3,0) coordinate (Y) ;
    \node[small dot,pin={[boxcolor=green,pin position={#1}]{%
\pgfmathprintnumber[use comma]{#3}}}] at (Y) {};
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmax=2,xmin=-2,ymin=-2,ymax=2,
axis x line=center, axis y line=center]
\node[below left] at (axis cs:0,0) {\footnotesize $0$};
\ReadX{.5}{.5} ;
\ReadY{1}{1.5} ;
\end{axis}
\end{tikzpicture}

\end{document}

感谢 Peter Grill,他提供了更多选项,甚至还有 en node 来在要点本身上放置一些内容:

\NewDocumentCommand{\ReadXY}{%
    D<>{green}% [#1] first color
    o%% [#2] = optional parameters x pin
        % (no x pin if no angle provided, which is the default)
    m%  % {#3} = x value (mandatory paramater)
    D<>{green}% [#4] second color
    o%% [#5] = optional parameters for y pin
        % (no y in if no angle provided, which is the default)
    m%  % {#6} = y value (mandatory paramater)
}{%
    \draw[very thin, dashed] (axis cs:#3,0) coordinate (ReadX)%
        |- (axis cs:0,#6) coordinate (ReadY) ;
    % new node on the point
    \coordinate (ReadXY) at (axis cs:#3,#6) ;
    %
    \IfValueTF{#2}{%
        \node[small dot,pin={[boxcolor=#1,pin position={#2}]
            {\smash[b]{\pgfmathprintnumber[use comma]{#3}}}}] at (ReadX) {};
    }{}%
    %
    \IfValueTF{#5}{%
        \node[small dot,pin={[boxcolor=#4,pin position={#5}]
            {\smash[b]{\pgfmathprintnumber[use comma]{#6}}}}] at (ReadY) {};
    }{}%
}%

答案1

我不能 100% 确定我理解您的意思,但是这应该可以让您了解如何将命令组合成一个命令。

下面的宏\ReadXY有两个可选参数,分别为xy引脚角度。如果未指定角度,则不标记该坐标。因此,这个宏允许您有四种可能的组合:

  • 标签盒xy
  • 标签仅有的x
  • 标签仅有的y
  • 标签两者都不坐标,只需画出线条:

如图所示:

在此处输入图片描述

代码:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{xparse}
\usepackage{xstring}

\tikzset{%
    boxcolor/.style args={#1}{%
    fill=#1!25!white,rectangle,rounded corners=4pt,font=\footnotesize,draw=#1},
    small dot/.style={fill=black,circle,scale=0.3}}


\NewDocumentCommand{\ReadXY}{%
    O{}% [#1] = optional parameters x pin (no x pin if no angle provided, which is the default)
    O{}% [#2] = optional parameters for y pin (no y pin if no angle provided, which is the default)
    m% {#3} = x value (mandatory paramater)
    m% {#4} = y value (mandatory paramater)
}{%
    \IfStrEq{#1}{}{}{%
        \draw[very thin, dashed] (axis cs:0,#4)-|(axis cs:#3,0) coordinate (Y) ;
        \node[small dot,pin={[boxcolor=green,pin position={#1}]
            {\pgfmathprintnumber[use comma]{#3}}}] at (Y) {};
    }%
    %
    \IfStrEq{#2}{}{}{%
        \draw[very thin, dashed] (axis cs:#3,0)|-(axis cs:0,#4) coordinate (Y) ;
        \node[small dot,pin={[boxcolor=green,pin position={#2}]
            {\pgfmathprintnumber[use comma]{#4}}}] at (Y) {};
    }%
}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmax=2,xmin=-2,ymin=-2,ymax=2,axis x line=center, axis y line=center]
    \node[below left] at (axis cs:0,0) {\footnotesize $0$};
    \ReadXY[-120][-160]{0.75}{1.0};% <--- Label BOTH x and y
    \ReadXY[45][]{1.25}{1.4};% <--- Label ONLY x
    \ReadXY[][20]{-1.5}{-1.7};% <--- Label ONLY y
\end{axis}
\end{tikzpicture}

\end{document}

相关内容