pfgplots 轴环境中的新命令 pfgkeys

pfgplots 轴环境中的新命令 pfgkeys

我正在尝试设置一个命令来创建技术图纸尺寸(灵感来自TikZ 中的技术图纸尺寸标注)。

我想使用pgfkeys它,以便本着同样的精神传递多个可选参数TikZ

axis除了在环境 (来自)中之外,我工作得很好 (至少我可以得到我需要的东西) pgfplots。我似乎所有pgfkeys来自所有dimline调用都被最后一个调用覆盖了。

我正在使用适用于 MacOSX 的最新 MacTex。

我正在使用以下方法编译源代码:

/usr/texbin/xelatex -interaction nonstopmode -shell-escap -output-directory . test3.tex

知道为什么axis环境会消除所有dimline选项吗?

这是我的代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\pgfkeys{
  dimline/.is family,
  dimline,
  dimcolor/.initial = black,
  start line/.initial=1cm,
  start line width/.initial=0.01,
  end line/.initial=1cm,
  end line width/.initial=0.01,
  label pos/.initial={},
}

% #1: optional keys parameters
% #2: start point
% #3: end point
% #4: text

\newcommand{\dimline}[4][]{%

  \pgfkeys{
    dimline,
    #1,
    start line width/.get=\dimlinestartlinewidth,
    start line/.get=\dimlinestartline,
    end line/.get=\dimlineendline,
    end line width/.get=\dimlineendlinewidth,
    label pos/.get=\labelpos,
  }

  \begin{scope}[
    dimrecall/.style={\pgfkeysvalueof{/dimline/dimcolor}},
    dimline/.style={\pgfkeysvalueof{/dimline/dimcolor}},
    ]

    \node (a) at #2 {};
    \node (b) at #3 {};

    \draw [/tikz/dimrecall, line width=\dimlinestartlinewidth]
    (a.center) -- ($(a)!\dimlinestartline!-90:(b)$);
    \draw [dimrecall, line width=\dimlineendlinewidth]
    (b.center) -- ($(b)!\dimlineendline!90:(a)$);

    \draw[|<->|, dimline,] (a.center) -- (b.center)
    node[fill=white, sloped, midway] {\tiny{#4}};
  \end{scope}
}


\begin{document}

Correct without \texttt{axis} environment:

\begin{tikzpicture}
  \draw  (0,0) -- (0,4) -- (.5,4) -- (.5,5) -- (1.,5) -- (1.,4) -- (1.5,4) -- (1.5,0) -- cycle;
  \dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{(3,0)}{(3,4)}{4.0};
  \dimline[dimcolor=red,start line=0.85, end line=0.85]{(0,6)}{(1.5,6)}{1.5};
\end{tikzpicture}


Incorrect within \texttt{axis} environment \texttt{pgfkeys} are overriden:

\begin{tikzpicture}
  \begin{axis} [
    xmin = -1,
    xmax = 8,
    ymin = -1,
    ymax = 8]

    \addplot[mark=none,  color=black] coordinates {
      (0,0)  (0,4) (.5,4) (.5,5) (1.,5) (1.,4) (1.5,4) (1.5,0) (0,0)
    };

    \dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{(axis cs: 3,0)}{(axis cs:3,4)}{4.0};
    \dimline[dimcolor=red,start line=0.85, end line=0.85]{(axis cs: 0,6)}{(axis cs: 1.5,6)}{1.5};

  \end{axis}

\end{tikzpicture}

\end{document}

结果

图像

答案1

虽然杰克的回答是实际的解决方案针对这个问题,这里对 UI 做了一些改进。让宏\dimline处理

  • 如果你需要使用(axis cs: x, y)(x, y)
  • 是否\pgfplotsextra需要应用。

axis和解决方案都tikzpicture使用相同的格式:

\dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{3,0}{3,4}{4.0};
\dimline[dimcolor=red,start line=0.85,   end line=0.85 ]{0,6}{1.5,6}{1.5};

得到:

在此处输入图片描述


参考:

笔记:

  • 指定坐标的方式已发生改变,即()删除了坐标周围的 。也许还有一种方法可以实现这一点。

代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\pgfkeys{
  dimline/.is family,
  dimline,
  dimcolor/.initial = black,
  start line/.initial=1cm,
  start line width/.initial=0.01,
  end line/.initial=1cm,
  end line width/.initial=0.01,
  label pos/.initial={},
}

% #1: optional keys parameters
% #2: start point
% #3: end point
% #4: text


\newcommand{\DoNothing}[1]{#1}%
\newcommand{\dimline}[4][]{%
\pgfplotsifinaxis{%
    \coordinate (a) at (axis cs: #2) {};
    \coordinate (b) at (axis cs: #3) {};
    \let\MyPgfplotsextra\pgfplotsextra%
}{%
    \coordinate (a) at (#2) {};
    \coordinate (b) at (#3) {};
    \def\MyPgfplotsextra#1{#1}%
    \let\MyPgfplotsextra\DoNothing%
}%

\MyPgfplotsextra{
  \pgfkeys{
    dimline,
    #1,
    start line width/.get=\dimlinestartlinewidth,
    start line/.get=\dimlinestartline,
    end line/.get=\dimlineendline,
    end line width/.get=\dimlineendlinewidth,
    label pos/.get=\labelpos,
  }


  \begin{scope}[
    dimrecall/.style={\pgfkeysvalueof{/dimline/dimcolor}},
    dimline/.style={\pgfkeysvalueof{/dimline/dimcolor}},
    ]


    \coordinate (a to b) at ($(a)!\dimlinestartline!-90:(b)$);
    \coordinate (b to a) at ($(b)!\dimlineendline!90:(a)$);

    \draw [dimrecall, line width=\dimlinestartlinewidth]
    (a.center) -- (a to b);
    \draw [dimrecall, line width=\dimlineendlinewidth]
    (b.center) -- (b to a);

    \draw[|<->|, dimline,] (a.center) -- (b.center)
    node[fill=white, sloped, midway] {\tiny{#4}};
  \end{scope}
 }
}


\begin{document}

%Correct without \texttt{axis} environment:

\begin{tikzpicture}
  \draw  (0,0) -- (0,4) -- (.5,4) -- (.5,5) -- (1.,5) -- (1.,4) -- (1.5,4) -- (1.5,0) -- cycle;
  \dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{3,0}{3,4}{4.0};
  \dimline[dimcolor=red,start line=0.85, end line=0.85]{0,6}{1.5,6}{1.5};
\end{tikzpicture}
%
%
%Incorrect within \texttt{axis} environment \texttt{pgfkeys} are overriden:
%
\begin{tikzpicture}
  \begin{axis} [
    xmin = -1,
    xmax = 8,
    ymin = -1,
    ymax = 8]

    \addplot[mark=none,  color=black] coordinates {
      (0,0)  (0,4) (.5,4) (.5,5) (1.,5) (1.,4) (1.5,4) (1.5,0) (0,0)
    };

    \dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{3,0}{3,4}{4.0};
    \dimline[dimcolor=red,start line=0.85, end line=0.85]{0,6}{1.5,6}{1.5};

  \end{axis}

\end{tikzpicture}

\end{document}

答案2

axis环境中,相当复杂的 TikZ 命令(例如,调用其他宏)应该包装在里面\pgfplotsextra{...}以确保它们正确执行:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\pgfkeys{
  dimline/.is family,
  dimline,
  dimcolor/.initial = black,
  start line/.initial=1cm,
  start line width/.initial=0.01,
  end line/.initial=1cm,
  end line width/.initial=0.01,
  label pos/.initial={},
}

% #1: optional keys parameters
% #2: start point
% #3: end point
% #4: text

\newcommand{\dimline}[4][]{%

  \pgfkeys{
    dimline,
    #1,
    start line width/.get=\dimlinestartlinewidth,
    start line/.get=\dimlinestartline,
    end line/.get=\dimlineendline,
    end line width/.get=\dimlineendlinewidth,
    label pos/.get=\labelpos,
  }

  \begin{scope}[
    dimrecall/.style={\pgfkeysvalueof{/dimline/dimcolor}},
    dimline/.style={\pgfkeysvalueof{/dimline/dimcolor}},
    ]

    \node (a) at #2 {};
    \node (b) at #3 {};

    \draw [/tikz/dimrecall, line width=\dimlinestartlinewidth]
    (a.center) -- ($(a)!\dimlinestartline!-90:(b)$);
    \draw [dimrecall, line width=\dimlineendlinewidth]
    (b.center) -- ($(b)!\dimlineendline!90:(a)$);

    \draw[|<->|, dimline,] (a.center) -- (b.center)
    node[fill=white, sloped, midway] {\tiny{#4}};
  \end{scope}
}


\begin{document}

\begin{tikzpicture}
  \begin{axis} [
    xmin = -1,
    xmax = 8,
    ymin = -1,
    ymax = 8]

    \addplot[mark=none,  color=black] coordinates {
      (0,0)  (0,4) (.5,4) (.5,5) (1.,5) (1.,4) (1.5,4) (1.5,0) (0,0)
    };


    \pgfplotsextra{
            \dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{(axis cs: 3,0)}{(axis cs:3,4)}{4.0};
            \dimline[dimcolor=red,start line=0.85, end line=0.85]{(axis cs: 0,6)}{(axis cs: 1.5,6)}{1.5};
        }

  \end{axis}

\end{tikzpicture}

\end{document}

相关内容