四边都有坐标的彩色网格?

四边都有坐标的彩色网格?

这是一个稍微增强版本的请求问题的答案:“所有边都有坐标的网格?”,这本身是对@Loopspace 先前答案的增强。

我想要第一个版本代码的彩色网格版本,位于问题的开头。链接的答案包括三个版本,以便于衡量。我认为对于熟悉 PGF/TikZ 相关部分的人来说,这很容易。为了具体起见,我们假设为浅绿色/叶绿色。我想要整个网格都是绿色,除了数字。

动机:我使用此网格代码来注释 PDF 表单。我想尝试使用不同于黑色的颜色,尤其是绿色,因为我认为绿色会与通常为黑白的实际表单形成对比

谢谢。

答案1

您可以直接在样式定义中添加颜色,也可以在生成网格时将其附加到样式中。在此代码中,我展示了后一种解决方案。我已将该tikz选项作为全局选项删除,以便我可以xcolor使用dvipsnames那些命名颜色的选项进行加载(在 之前加载tikz)。

\documentclass[border=3.14mm]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter% code from https://tex.stackexchange.com/a/469213/194703
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\def\GridCore{\edef\grd@@target{(\tikzinputsegmentlast)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikzinputsegmentfirst)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[minor help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast);
        \draw[major help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast);
        \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}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
        \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
        {\ifticksB
        \node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
        \fi
        \ifticksT
        \node[anchor=south] at (\x,\grd@yb) {\pgfmathprintnumber{\x}};
        \fi
        }
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        {\ifticksL
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
        \fi
        \ifticksR
        \node[anchor=west] at (\grd@xb,\y) {\pgfmathprintnumber{\y}};
        \fi}
}  
\newif\ifticksL  
\newif\ifticksR  
\newif\ifticksT  
\newif\ifticksB  
\tikzset{ticks left/.is if=ticksL,
ticks right/.is if=ticksR,
ticks on top/.is if=ticksT,
ticks at bottom/.is if=ticksB,
ticks left=true,
  ticks at bottom=true,
  ticks right=false,
  ticks on top=false,
  grid with coordinates/.style={
  decorate,decoration={show path construction,
  lineto code={\GridCore
    }}
  },
  minor help lines/.style={
    help lines,
    step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step}
  },
  major help lines/.style={
    help lines, line cap=rect,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    step=\pgfkeysvalueof{/tikz/grid with coordinates/major step}
  },
  grid with coordinates/.cd,
  minor step/.initial=.2,
  major step/.initial=1,
  major line width/.initial=2pt,
}
\makeatother
\begin{document}

\begin{tikzpicture}
\draw[grid with coordinates,ticks on top=true,ticks right=true,major help lines/.append style=LimeGreen,minor help lines/.append style=LimeGreen ] (-2,-2) -- (7,4);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是一个基于不带版本\pgfextra,理由是 pgfmanual v.3.1.4\pgfextra在第 167 页提到

请注意,此操作仅应由真正的专家使用,并且仅应在巧妙的宏内部深处使用,而不能在正常路径上使用。

这也是一种“清理”键的尝试,以便所有内容都在目录下/tikz/grid with coordinates/。它现在有键all help lines,它为次要和主要帮助行添加了一些样式。此外,语法也已尝试清理,灵感来自这次讨论:现在你只需要说一遍ticks/.list={top,left}而不用再说好true几遍。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter% https://tex.stackexchange.com/a/469213/194703
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\def\GridCore{\edef\grd@@target{(\tikzinputsegmentlast)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikzinputsegmentfirst)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[grid with coordinates/minor help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast);
        \draw[grid with coordinates/major help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast)
        (\tikzinputsegmentfirst) rectangle (\tikzinputsegmentlast);
        \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}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
        \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
        {\ifticksB
        \node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
        \fi
        \ifticksT
        \node[anchor=south] at (\x,\grd@yb) {\pgfmathprintnumber{\x}};
        \fi
        }
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        {\ifticksL
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
        \fi
        \ifticksR
        \node[anchor=west] at (\grd@xb,\y) {\pgfmathprintnumber{\y}};
        \fi}
}  
\newif\ifticksL  
\newif\ifticksR  
\newif\ifticksT  
\newif\ifticksB  
\tikzset{grid with coordinates/.style={
  /utils/exec=\tikzset{grid with coordinates/.cd,ticks=none,#1},
  decorate,decoration={show path construction,
  lineto code={\GridCore
    }}
  },
  grid with coordinates/.cd,
  ticks/.is choice,
  ticks/left/.code=\ticksLtrue,
  ticks/right/.code=\ticksRtrue,
  ticks/top/.code=\ticksTtrue,
  ticks/bottom/.code=\ticksBtrue,
  ticks/none/.code=\ticksLfalse\ticksRfalse\ticksTfalse\ticksBfalse,
  ticks/all/.code=\ticksLtrue\ticksRtrue\ticksTtrue\ticksBtrue,
  minor step/.initial=.2,
  major step/.initial=1,
  major line width/.initial=2pt,
  minor help lines/.style={
    help lines,
    step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step}
  },
  major help lines/.style={
    help lines,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    step=\pgfkeysvalueof{/tikz/grid with coordinates/major step}
  },
 all help lines/.code={\tikzset{grid with coordinates/.cd,
    major help lines/.append style={#1},
    minor help lines/.append style={#1}
    }} 
}
\makeatother
\begin{document}

\begin{tikzpicture}
\draw[grid with coordinates={ticks/.list={top,left},
 all help lines={draw=green!60!black},
 }] (-2,-2) -- (7,4);
\end{tikzpicture}

\begin{tikzpicture}
\draw[grid with coordinates={ticks=all,
 all help lines={draw=blue},minor help lines/.append style={dashed},
 }] (-2,-2) -- (7,4);
\end{tikzpicture}

\begin{tikzpicture}
\draw[grid with coordinates={ticks=none,
 all help lines={draw=red},minor help lines/.append style={densely dotted},
 }] (-2,-2) -- (7,4);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

color=green,可以通过添加minor help lines/.style=和来调整网格线的颜色major help lines/.style=

完整示例:

\documentclass[tikz,border=3.14mm,svgnames]{standalone}


\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}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
        \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
        {\node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
        \node[anchor=south] at (\x,\grd@yb) {\pgfmathprintnumber{\x}};
        }
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        {\node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
        \node[anchor=west] at (\grd@xb,\y) {\pgfmathprintnumber{\y}};}
      }
    }
  },
  minor help lines/.style={
    help lines,
    color=green,
    step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step}
  },
  major help lines/.style={
    help lines,
    color=green,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    step=\pgfkeysvalueof{/tikz/grid with coordinates/major step}
  },
  grid with coordinates/.cd,
  minor step/.initial=.2,
  major step/.initial=1,
  major line width/.initial=2pt,
}
\makeatother
\begin{document}

\begin{tikzpicture}
\draw (-2,-2) to[grid with coordinates] (7,4);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容