tikz 内部文件的参数化输入

tikz 内部文件的参数化输入

我查看了一些关于向 \input 命令传递参数的线程,但发现没有太多。任何帮助解决我当前困境的帮助(或替代方法的反馈)都将不胜感激!我当前的设置(有效,但不理想)是:

main.tex 中的相关部分

\begin{tikzpicture}[scale=.3]
    \input{grid}
    %some other shapes
\end{tikzpicture}

网格

\draw[black!20!white] (0,0) grid (20, 20);
\fill (0,0) circle (2pt) node[below left] {(0,200)};
\fill (20,0) circle (2pt) node[below right] {(200,200)};
\fill (0,20) circle (2pt) node[above left] {(0,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\tiny
\foreach \x in {20, 40, ..., 180} {
    \fill (\x/10,20) circle (2pt) node[above] {\x};
}
\foreach \y in {20, 40, ..., 180} {
    \fill (0,20-\y/10) circle (2pt) node[left] {\y};
}

虽然我想自定义网格。比如 -

\pgfmathsetmacro{\inc}{#1/10}
\draw[black!20!white] (0,0) grid (\inc,\inc);
\fill (0,0) circle (2pt) node[below left] {(0,#1)};
\fill (\inc,0) circle (2pt) node[below right] {(#1,#1)};
\fill (0,\inc) circle (2pt) node[above left] {(0,0)};
\fill (\inc,\inc) circle (2pt) node[above right] {(#1,0)};
\fill (\inc,\inc) circle (2pt) node[above right] {(#1,0)};
\tiny
\foreach \x in {#2, #2*2, ..., #1 - #2} {
    \fill (\x/10,\inc) circle (2pt) node[above] {\x};
}
\foreach \y in {#3, #3*2, ..., #1 - #3} {
    \fill (0,\inc-\y/10) circle (2pt) node[left] {\y};
}

并在 main.tex 中输入如下:

\begin{tikzpicture}[scale=.3]
    \input{grid}[160][10][25]
    %some other shapes
\end{tikzpicture}

或者

\begin{tikzpicture}[scale=.3]
    \input{grid}[300][15][15]
    %some other shapes
\end{tikzpicture}

编辑:

按照@Andrew的要求,MWE(可以工作,但不理想)-

主要.tex:

\documentclass{exam}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.3]
    \input{grid}
\end{tikzpicture}   
\end{center}
\end{document}

网格.tex:

\draw[black!20!white] (0,0) grid (20, 20);
\fill (0,0) circle (2pt) node[below left] {(0,200)};
\fill (20,0) circle (2pt) node[below right] {(200,200)};
\fill (0,20) circle (2pt) node[above left] {(0,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\tiny
\foreach \x in {20, 40, ..., 180} {
    \fill (\x/10,20) circle (2pt) node[above] {\x};
}
\foreach \y in {20, 40, ..., 180} {
    \fill (0,20-\y/10) circle (2pt) node[left] {\y};
}

编辑2:

根据@Andrew 的建议,我设法在主文件中定义变量并在 grid.tex 中使用相同的变量

主要.tex:

\documentclass{exam}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.3]
    \def\gridsize{300}
    \def\xMarker{45}
    \def\yMarker{50}
    \input{grid}
\end{tikzpicture}   
\end{center}
\end{document}

网格.tex:

\pgfmathsetmacro\size{\gridsize/10}
\draw[black!20!white] (0,0) grid (\size,\size);
\fill (0,0) circle (2pt) node[below left] {(0,\gridsize)};
\fill (\size,0) circle (2pt) node[below right] {(\gridsize,\gridsize)};
\fill (0,\size) circle (2pt) node[above left] {(0,0)};
\fill (\size,\size) circle (2pt) node[above right] {(\gridsize,0)};
\fill (\size,\size) circle (2pt) node[above right] {(\gridsize,0)};
\tiny
\foreach \x in {0,\xMarker, ..., \gridsize} {
    \fill (\x/10,\size) circle (2pt) node[above] {\x};
}
\foreach \y in {0,\yMarker, ..., \gridsize} {
    \fill (0,\size-\y/10) circle (2pt) node[left] {\y};
}

仍然希望我能以某种方式将值传递给文件:)

我认为我无法创建命令,因为我无法将其放入 tikz 中。

答案1

你想要这样的东西吗?

\documentclass{exam}
\usepackage{tikz,xparse}
\NewDocumentCommand \mygrid { O {300} O {45} O {50} }
{
  \pgfmathsetmacro\size{#1/10}
  \draw[black!20!white] (0,0) grid (\size,\size);
  \fill (0,0) circle (2pt) node[below left] {(0,#1)};
  \fill (\size,0) circle (2pt) node[below right] {(#1,#1)};
  \fill (0,\size) circle (2pt) node[above left] {(0,0)};
  \fill (\size,\size) circle (2pt) node[above right] {(#1,0)};
  \fill (\size,\size) circle (2pt) node[above right] {(#1,0)};
  \begin{scope}[font=\tiny]
    \foreach \x in {0,#2, ..., #1} {
      \fill (\x/10,\size) circle (2pt) node[above] {\x};
    }
    \foreach \y in {0,#3, ..., #1} {
      \fill (0,\size-\y/10) circle (2pt) node[left] {\y};
    }
  \end{scope}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.3]
  \mygrid[200][35][25]
\end{tikzpicture}
\begin{tikzpicture}[scale=.3]
  \mygrid
\end{tikzpicture}
\end{center}
\end{document}

网格上的变化

相关内容