TikZ 中带有 to[path] 的不同 xstep 和 ystep (精确地说是 to[带坐标的网格])

TikZ 中带有 to[path] 的不同 xstep 和 ystep (精确地说是 to[带坐标的网格])

因为这是我的第一个问题,所以我想hello先说!

我在用[grid with coordinates]Andrew Stacey 答案中的代码(关联)为了如何在 TikZ 中自动按网格显示坐标?

我对其进行了一些修改并尝试获得不同的major/minor lines xstep结果ystep

\documentclass{article}

%\usepackage{pgfplots}
\usepackage{tikz,pgfplots}

\makeatletter
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\tikzset{
  grid with coordinates/.style={
    to path={%
      \pgfextra{%
        \edef\grd@@target{(\tikztotarget)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikztostart)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
        \draw[major help lines] (\tikztostart) grid (\tikztotarget);
        \grd@start
        \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
        \grd@target
        \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
        \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step x}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}}
        \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
        \node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
      }
    }
  },
  minor help lines/.style={
    help lines,
    gray,
    line cap =round,
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y}
  },
  major help lines/.style={
    help lines,
    line cap =round,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y}
  },
  grid with coordinates/.cd,
  minor step x/.initial=.5,
  minor step y/.initial=.2,
  major step x/.initial=1,
  major step y/.initial=1,
  major line width/.initial=1pt,
}
\makeatother

我现在需要的是实际地指定xstep和,例如在下面的代码中我想传递和(分别针对次要和主要行),以便图 B 得到。ysteptikzpicturexstepystepmajor lines xstep =0.2

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=1,yscale=2]
    \draw (0,0) to[grid with coordinates] (5,1);    
    \end{tikzpicture}
    \caption{A}\label{fig:sphere}
\end{figure}    

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=5,yscale=2]
    \draw (0,0) to[grid with coordinates] (1,1); %major step x =0.2
    \end{tikzpicture}
    \caption{B}\label{fig:sphere}
\end{figure}

\end{document}

我猜不出如何传递参数。

答案1

您可以使用该选项

grid with coordinates/major step x=<value>

完整示例:

\documentclass{article}

%\usepackage{pgfplots}
\usepackage{tikz,pgfplots}

\makeatletter
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\tikzset{
  grid with coordinates/.style={
    to path={%
      \pgfextra{%
        \edef\grd@@target{(\tikztotarget)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikztostart)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
        \draw[major help lines] (\tikztostart) grid (\tikztotarget);
        \grd@start
        \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
        \grd@target
        \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
        \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step x}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}}
        \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
        \node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
      }
    }
  },
  minor help lines/.style={
    help lines,
    gray,
    line cap =round,
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y}
  },
  major help lines/.style={
    help lines,
    line cap =round,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y}
  },
  grid with coordinates/.cd,
  minor step x/.initial=.5,
  minor step y/.initial=.2,
  major step x/.initial=1,
  major step y/.initial=1,
  major line width/.initial=1pt,
}
\makeatother

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=1,yscale=2]
    \draw (0,0) to[grid with coordinates] (5,1);    
    \end{tikzpicture}
    \caption{A}\label{fig:sphere}
\end{figure}    

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=5,yscale=2,grid with coordinates/major step x=0.2]
    \draw (0,0) to[grid with coordinates] (1,1); %major step x =0.2
    \end{tikzpicture}
    \caption{B}\label{fig:sphere}
\end{figure}

\end{document}

在此处输入图片描述

类似地,您可以更改major step yminor step xminor step ymajor line width

相关内容