我是 TikZ 的新手。在过去的两天里,我一直在评估 TikZ-KTikZ 组合,作为 Xfig 的替代品,我已经使用了 17 年。我设法使用以下代码创建了一个网格结构:
\begin{tikzpicture}
\newcommand{\GL}{5};
\draw [step=0.1, help lines,red!20] (-\GL,-\GL) grid (\GL,\GL);
\draw [step=1.0, help lines,red!40] (-\GL,-\GL) grid (\GL,\GL);
\draw [step=\GL.0, help lines,red!100] (-\GL,-\GL) grid (\GL,\GL);
\foreach \x in {-\GL,...,\GL} { \draw node at (\x,-\GL) [label=below:{\tiny $\x.0$}]{}; };
\foreach \x in {-\GL,...,\GL} { \draw node at (\x,\GL) [label=above:{\tiny $\x.0$}] {}; };
\foreach \y in {-\GL,...,\GL} { \draw node at (-\GL,\y) [label=left:{\tiny $\y.0$}] {}; };
\foreach \y in {-\GL,...,\GL} { \draw node at (\GL,\y) [label=right:{\tiny $\y.0$}] {}; };
\end{tikzpicture}
我喜欢使用网格结构,因为它使我能够快速将图片元素放置在 {tikzpicture} 环境中。但是,我必须在绘制图片后将其注释掉。我想将上述代码打包成一个灵活的命令,我可以将其插入 {tikzpicture} 并在完成后快速将其注释掉。
有丰富 TikZ 经验的人士能否建议我如何将上述代码打包成一个命令,该命令可以接受可选参数,例如 Xmin、Xmax、Ymin、Ymax 和网格颜色以及默认值。
任何帮助将不胜感激。
答案1
这是一个通过 解决的解决方案pgfkeys
。
首先,我创建了新的系列,我在这个系列中定义了一些键及其初始值,并定义了使用这个系列的mygrid
新命令:\mygridset
\pgfkeys{
mygrid/.is family,
mygrid,
min x/.initial=-5,
max x/.initial=5,
min y/.initial=-5,
max y/.initial=5,
small step/.initial=.1,
step/.initial=1,
big step/.initial=5,
color/.initial=red,
}
\newcommand\mygridset[1]{\pgfkeys{mygrid,#1}}
然后我使用一个空的可选参数定义新命令\mygrid
。此命令的前几行(对的调用)通过然后将每个键的值存储在宏中来\mygridset
更改键的值。最后几行使用这些值绘制网格。#1
\newcommand\mygrid[1][]{
\mygridset{#1,
min x/.get=\gridminx,
max x/.get=\gridmaxx,
min y/.get=\gridminy,
max y/.get=\gridmaxy,
small step/.get=\gridsmallstep,
step/.get=\gridstep,
big step/.get=\gridbigstep,
color/.get=\gridcolor
}
\draw [step=\gridsmallstep, help lines,\gridcolor!20]
(\gridminx,\gridminy) grid (\gridmaxx,\gridmaxy);
\draw [step=\gridstep, help lines,\gridcolor!40]
(\gridminx,\gridminy) grid (\gridmaxx,\gridmaxy);
\draw [step=\gridbigstep, help lines,\gridcolor!100]
(\gridminx,\gridminy) grid (\gridmaxx,\gridmaxy);
\foreach \x in {\gridminx,...,\gridmaxx} {
\node[below,font=\tiny] at (\x,\gridminy) {$\x$};
\node[above,font=\tiny] at (\x,\gridmaxy) {$\x$};
};
\foreach \y in {\gridminy,...,\gridmaxy} {
\node[left,font=\tiny] at (\gridminx,\y) {$\y$};
\node[right,font=\tiny] at (\gridmaxx,\y) {$\y$};
};
}
下面是一个完整的示例(3 页)来说明用法:
完整代码如下:
\documentclass[tikz]{standalone}
\pgfkeys{
mygrid/.is family,
mygrid,
min x/.initial=-5,
max x/.initial=5,
min y/.initial=-5,
max y/.initial=5,
small step/.initial=.1,
step/.initial=1,
big step/.initial=5,
color/.initial=red,
}
\newcommand\mygridset[1]{\pgfkeys{mygrid,#1}}
\newcommand\mygrid[1][]{
\mygridset{#1,
min x/.get=\gridminx,
max x/.get=\gridmaxx,
min y/.get=\gridminy,
max y/.get=\gridmaxy,
small step/.get=\gridsmallstep,
step/.get=\gridstep,
big step/.get=\gridbigstep,
color/.get=\gridcolor
}
\draw [step=\gridsmallstep, help lines,\gridcolor!20]
(\gridminx,\gridminy) grid (\gridmaxx,\gridmaxy);
\draw [step=\gridstep, help lines,\gridcolor!40]
(\gridminx,\gridminy) grid (\gridmaxx,\gridmaxy);
\draw [step=\gridbigstep, help lines,\gridcolor!100]
(\gridminx,\gridminy) grid (\gridmaxx,\gridmaxy);
\foreach \x in {\gridminx,...,\gridmaxx} {
\node[below,font=\tiny] at (\x,\gridminy) {$\x$};
\node[above,font=\tiny] at (\x,\gridmaxy) {$\x$};
};
\foreach \y in {\gridminy,...,\gridmaxy} {
\node[left,font=\tiny] at (\gridminx,\y) {$\y$};
\node[right,font=\tiny] at (\gridmaxx,\y) {$\y$};
};
}
% a style to memorize some change to the default values
\mygridset{
a grid/.style={
min x=-3,
max x=3,
min y=-3,
max y=3,
small step=.2,
step=1,
big step=2,
color=orange,
}
}
\begin{document}
\begin{tikzpicture}
% a grid with default values
\mygrid
\end{tikzpicture}
\begin{tikzpicture}
% a grid with specific values
\mygrid[min x=-3, max x=2,min y=-2,max y=1,color=blue]
\end{tikzpicture}
\begin{tikzpicture}
% a grid using the `a grid` style
\mygrid[a grid]
\end{tikzpicture}
\end{document}
答案2
这是我的看法。困难实际上在于 LaTeX 只接受 中的一个参数(第一个)的默认值\newcommand
。如果你想指定多个,你需要想出一些技巧。这实际上与 TikZ 无关。我的解决方案使用包xstring
(可能不是最优雅的解决方案):
\newcommand{\bgndgrid}[1]{%
\IfStrEq{#1}{default}{%
\draw [step=0.1, help lines,red!20] (-5,-5) grid (5,5);
\draw [step=1, help lines,red!40] (-5,-5) grid (5,5);
\draw [step=5, help lines,red!100] (-5,-5) grid (5,5);
\foreach \x in {-5,...,5} {\draw node at (\x,-5) [label=below:{\tiny $\x.0$}]{};};
\foreach \x in {-5,...,5} {\draw node at (\x,5) [label=above:{\tiny $\x.0$}]{};};
\foreach \y in {-5,...,5} {\draw node at (-5,\y) [label=left:{\tiny $\y.0$}]{};};
\foreach \y in {-5,...,5} {\draw node at (5,\y) [label=right:{\tiny $\y.0$}]{};};
}%
{%
\StrBefore{#1}{,}[\xmin]
\StrBetween[1,2]{#1}{,}{,}[\ymin]
\StrBetween[2,3]{#1}{,}{,}[\xmax]
\StrBetween[3,4]{#1}{,}{,}[\ymax]
\StrBehind[4]{#1}{,}[\gcolor]
\draw [step=0.1, help lines,\gcolor!20] (-\xmin,-\ymin) grid (\xmax,\ymax);
\draw [step=1, help lines,\gcolor!40] (-\xmin,-\ymin) grid (\xmax,\ymax);
\draw [step=5, help lines,\gcolor!100] (-\xmin,-\ymin) grid (\xmax,\ymax);
\foreach \x in {-\xmin,...,\xmax} {\draw node at (\x,-\ymin) [label=below:{\tiny $\x.0$}]{};};
\foreach \x in {-\xmin,...,\xmax} {\draw node at (\x,\ymax) [label=above:{\tiny $\x.0$}]{};};
\foreach \y in {-\ymin,...,\ymax} {\draw node at (-\xmin,\y) [label=left:{\tiny $\y.0$}]{};};
\foreach \y in {-\ymin,...,\ymax} {\draw node at (\xmax,\y) [label=right:{\tiny $\y.0$}]{};};
}
}
您需要输入:\bgndgrid{default}
以获取编码的默认行为(网格从 (-5,-5) 到 (5,5),以红色显示)。如果愿意,您可以重新定义默认字符串。否则,宏将尝试在找到逗号(分隔符)的地方切断参数,如果成功,将绘制网格。参数应输入为:\bgndgrid{xmin,ymin,xmax,ymax,color}
,例如:
\bgndgrid{1,2,3,4,blue}
得出的结果是:
欢迎随意定制!