2

I want to compile this code (using 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}

I obtain the weird error message:

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

When I remove or comment the line containing \draw (ptad1)--(ptg1);, I don't obtain any error. I think that the error is due to the "foreach" command: when I suppress the \foreach command and I write the command manually (for each step of the foreach command), I don't have any error.

Do you have an idea about the origin of this error?

Guuk
  • 1,495
  • 2
    Please reduce your code to something more readable, i.e. a MWE. Typically this lets you discover your own mistakes. But the error stems from \foreach not expanding the arguments of the macros, thus it searches for a point named iteru\nuuu and not iteru0, iteru1 etc. There are several similar questions on this site. And welcome! – nickpapior Jul 12 '13 at 12:29
  • I get several errors before the Undefined control sequence one; please, fix the code. – egreg Jul 12 '13 at 12:36
  • @zeroth both points (ptad1) and (ptg1) don't depend of the content of the \foreach command and without the \draw (ptad1) -- (ptg1). So I don't understand why tikz crashes in this case. I will try to simplify my code but currently the easiest solution is to suppress the \foreachcommand. – Guuk Jul 12 '13 at 12:40
  • @egreg I have fixed one mistake. The code is also available at the following url: https://www.writelatex.com/268046xmrpwj – Guuk Jul 12 '13 at 12:48

1 Answers1

3

Change your macros into

\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}

In this way \nuuu and \nddd will be expanded before the \coordinate and \path commands start their work.

enter image description here

egreg
  • 1,121,712