错误编译 tikz、交集和 foreach

错误编译 tikz、交集和 foreach

我想编译此代码(使用 PDFLaTeX)

\documentclass[12pt]{article}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{color}
\usetikzlibrary{arrows,shapes,scopes,calc,intersections}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\begin{document}
\newcommand{\coorpente}[4]{\coordinate(#1) at ($(#2)+({#4/(sqrt((#3*#3)+1))},{#3*#4/(sqrt((#3*#3)+1))})$);}
\newcommand{\inter}[3]{\path [name intersections={of=#2 and #3,by=#1}];}
\newcommand{\linenp}[4]{\path[name path=#1,#4](#2)--(#3);}

\begin{tikzpicture}[]
\draw[help lines,step=1,line width=1.5pt,black!50] (0,-1) grid (10,6);
\draw[help lines,step=.2] (0,-1) grid (10,6);
\coordinate(ptad1) at (0,0) ;
\coordinate(ptad2) at (10,0) ;
\coordinate(ptg1) at (0.5,-1) ;
\coordinate(ptg2) at (10,5) ;

%espace
\linenp{ad}{ptad1}{ptad2}{draw,yellow!80!black,line width=5pt}
\linenp{adb}{ptad1}{ptad2}{draw,line width=1.5pt}
\draw[cyan,line width=4pt,name path=gamma] (ptg1)to [out=90,in=195] (ptg2);
\linenp{adb}{ptad1}{ptad2}{draw,line width=1.5pt}
\draw[line width=1.5pt,name path=gammab] (ptg1)to [out=90,in=195] (ptg2);

\def\pente {3.5}
%iterations
%1d
\coordinate(iterdb1) at (10,3);
\coorpente{iterd1}{iterdb1}{\pente}{-4}
\linenp{itd1}{iterdb1}{iterd1}{}
\inter{intd1}{itd1}{ad}
\draw(iterdb1)--(intd1);
\foreach \nuuu/\nddd in{1/2,2/3,3/4,4/5,5/6}
{
%u
\coorpente{iteru\nuuu}{intd\nuuu}{-\pente}{-5}
\linenp{itu\nuuu}{iteru\nuuu}{intd\nuuu}{}
\inter{intu\nuuu}{itu\nuuu}{gamma}
\draw(intd\nuuu)--(intu\nuuu);
%d
\coorpente{iterd\nddd}{intu\nuuu}{\pente}{-5}
\linenp{itd\nddd}{intu\nuuu}{iterd\nddd}{}
\inter{intd\nddd}{itd\nddd}{ad}
\draw(intu\nuuu)--(intd\nddd);
}

\draw (ptad1)--(ptg1);

\end{tikzpicture}
\end{document}

我收到了奇怪的错误信息:

! Undefined control sequence.
\tikz@intersect@namedpaths ...@path@name@itd\nddd 
                                              \endcsname {\pgfsyssoftpat...
l.165 \draw (ptad1)--(ptg1);

当我删除或注释包含的行时\draw (ptad1)--(ptg1);,我没有收到任何错误。我认为错误是由于“foreach”命令造成的:当我抑制该\foreach命令并手动编写命令(对于 foreach 命令的每个步骤)时,我没有收到任何错误。

您是否知道此错误的根源?

答案1

将您的宏更改为

\newcommand{\coorpente}[4]{%
  \begingroup\edef\x{\endgroup
    \noexpand\coordinate(#1) at
      ($(#2)+({#4/(sqrt((#3*#3)+1))},{#3*#4/(sqrt((#3*#3)+1))})$);}\x}
\newcommand{\inter}[3]{%
  \begingroup\edef\x{\endgroup
    \noexpand\path [name intersections={of=#2 and #3,by=#1}];}\x}
\newcommand{\linenp}[4]{%
  \begingroup\edef\x{\endgroup
    \noexpand\path[name path=#1,#4](#2)--(#3);}\x}

这样\nuuu,和将在和命令开始工作\nddd之前被展开。\coordinate\path

在此处输入图片描述

相关内容