路径与构造名称的交集

路径与构造名称的交集

这个问题是这个问题的“后续”在 TikZ foreach 循环中命名路径。我可以构造路径的名称,但是使用它们来计算交点时会出现错误。

下面是一个示例

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}

\newcommand{\modulargrid}{%
 \begin{tikzpicture}[remember picture,   overlay,  ]

%% Trick to name paths:
%% https://tex.stackexchange.com/questions/5242/naming-paths-inside-a-tikz-foreach-loop

%% Draw horizontal lines
  \foreach \x in {1,...,6}   {
    \edef\pathhone{name path global=horizontal\x}
    \expandafter\draw\expandafter[\pathhone] ($
    (current page.north west) 
    + \x-1*(0,20pt-5cm)
    $)
    -- +(20cm, 0);

%% Draw vertical lines
   \edef\pathvone{name path global=vertical\x}
   \expandafter\draw\expandafter[\pathvone] ($
    (current page.south west) 
    + \x-1*(20pt+5cm,0)
    $)
    -- +(0,25cm);
}


\draw (0,0)--(0,0);
\fill[red, opacity=0.5, name intersections={of=vertical1 and horizonta1}]
(intersection-1) circle (2pt) node {1};
 \end{tikzpicture}
}

\begin{document}

\modulargrid
\newpage

\end{document}

代码的目的

该代码背后的想法是制作一个垂直和水平线的网格,计算交点,然后用计算出的点构建矩形(我希望能够将网格中的每个单元作为一个节点。)

编辑代码

我根据@jake的评论编辑了代码。 (由于将更复杂的例子复制到MWE中,所以出现了错误,对此深表歉意。)

\i我的问题是在\fill命令中使用

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}

\newcommand{\modulargrid}{%
 \begin{tikzpicture}[remember picture,   overlay,  ]

%% Trick to name paths:
%% https://tex.stackexchange.com/questions/5242/naming-paths-inside-a-tikz-foreach-loop

%% Draw horizontal lines
  \foreach \x[count=\i] in {1,...,6}   {
    \draw[name path global=horizontal\i] ($
    (current page.north west) 
    + \x-1*(0,20pt-5cm)
    $)
    -- +(20cm, 0);

%% Draw vertical lines
   \draw[name path global=vertical\i] ($
    (current page.south west) 
    + \x-1*(20pt+5cm,0)
    $)
    -- +(0,28cm);
 \draw (0,0)--(0,0);
 \fill[red, opacity=0.5, name intersections={of=vertical\i and horizontal\i}]
 (intersection-1) circle (2pt) node {1};
}


 \end{tikzpicture}
}

\begin{document}

答案1

除了拼写错误之外,主要问题在于name intersections。 latex 产生的错误是“参数失控?”,表明它没有正确解析参数。要修复此问题,请{vertical\i}在括号中写入 ,类似地{horizontal\i}。下面的代码也以各种方式整理,可能有助于解决您的一般问题,现在产生:

示例输出

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}

\newcommand{\modulargrid}{%
 \begin{tikzpicture}[remember picture, overlay]
%% Draw horizontal lines
  \foreach \x[count=\i] in {1,...,6}   {
    \draw[name path global=horizontal\i] ($
    (current page.north west) 
    + \x*(0,20pt-5cm)
    $)
    -- +(22cm, 0);
%% Draw vertical lines
   \draw[name path global=vertical\i] ($
    (current page.south west) 
    + \x*(20pt+3cm,0)
    $)
    -- +(0,28cm);
%% Dots at intersections
    \fill[red, opacity=0.5,
    name intersections={of={vertical\i} and {horizontal\i}}]
    (intersection-1) circle (5pt) node {};
    }
 \end{tikzpicture}
}

\usepackage{lipsum}

\begin{document}

\modulargrid
\lipsum[1-6]
\end{document}

主要变化如下:

  • 使\x变量乘以偏移量(并使用而\x不是\x - 1
  • 为清晰起见,增加了点的尺寸,并提供了一个空的node论点
  • lipsum通过可视化包更改间距和长度并包含背景文本

相关内容