Discussion:
[LaTeX][PGF/TikZ] Undefined control sequence error for PGF intersections.
(too old to reply)
Annada Behera
2024-08-30 07:59:14 UTC
Permalink
Consider this minimal working example. I have two functions, f(x) = x
and g(x) = x+sin(x). I want to draw them with PGF and at the intersection
points, I want put black dots. Here is the TikZ code,

\documentclass{standalone}
\usepackage{tikz, amsmath}
\usetikzlibrary{intersections}
\begin{document}\begin{tikzpicture}

% Plots
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[red, name path=line] (0,0) plot (\x, \x);
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[blue, name path=sine] (0,0) plot (\x, {\x + sin(\x r)});

% Drawing the dots
\fill[name intersections={of=line and sine, name=i, total=\t}, black]
\foreach \s in {1,...,\t} {(i-\s) circle (2pt)};

\end{tikzpicture}\end{document}

Now this code works as expected. But I also wanted to draw dashed lines from
the intersections to each axes.

% Axes
\draw [<->](-6.28, 0) -- (6.28, 0);
\draw [<->](0, -6.28) -- (0, 6.28);

% Mark intersection points and draw dashed lines
\foreach \n in {1,...,\t} {
\path ({i-\n}) coordinate (i\n); % <--- Error Here
\fill[black] (i\n) circle (2pt);
\draw[dashed] (i\n) -- (i\n |- 0,0);
\draw[dashed] (i\n) -- (0,0 -| i\n);
}

In this part, pdflatex (my distro is TeX Live 2024) throws an error what
I don't understand,

! Undefined control sequence.
\UseTextAccent ...p \@firstofone \let \@***@enc
\***@encoding \@***@text@en...
l.28 }

What is undefined? I am pretty sure the name=i in \fill command populates
the namespace with i-1, 1-2 and 1-3, because \fill has drawn them. I
have even tried to help the parser with extra braces, {i-\n} and {i\n}
but that also doesn't help. Anybody know the reason why I get the error
and how to fix them?
Stefan Ram
2024-08-30 12:20:12 UTC
Permalink
Post by Annada Behera
\foreach \n in {1,...,\t} {
\path ({i-\n}) coordinate (i\n); % <--- Error Here
\fill[black] (i\n) circle (2pt);
\draw[dashed] (i\n) -- (i\n |- 0,0);
\draw[dashed] (i\n) -- (0,0 -| i\n);
}
I'm about as familiar with LaTeX as I am with surfing Pipeline
- which is to say, not at all. And this tikz thing? Totally
off my radar. But after messing around like a bear in a honey
factory, I stumbled onto something. Turns out, if you unravel
that last loop and use the following code, it's all gravy here.

\path ({i-1}) coordinate (i1);
\fill[black] (i1) circle (2pt);
\draw[dashed] (i1) -- (i1 |- 0,0);
\draw[dashed] (i1) -- (0,0 -| i1);
\path ({i-2}) coordinate (i2);
\fill[black] (i2) circle (2pt);
\draw[dashed] (i2) -- (i2 |- 0,0);
\draw[dashed] (i2) -- (0,0 -| i2);
\path ({i-3}) coordinate (i3);
\fill[black] (i3) circle (2pt);
\draw[dashed] (i3) -- (i3 |- 0,0);
\draw[dashed] (i3) -- (0,0 -| i3);
\path ({i-4}) coordinate (i4);
\fill[black] (i4) circle (2pt);
\draw[dashed] (i4) -- (i4 |- 0,0);
\draw[dashed] (i4) -- (0,0 -| i4);

| /H H
| :$ .;XM
| =$ +%$@=
| =$ .+$//H:
| =$ .+$+ +H=
| =$ ,+%+ XH
| =$ ,+$/ /H$
| =$ .+%; /XX=
| =$ .--,+HX$/+$X$:
| =$ ;$X$%X@#M+++/:
| =$ %HX/.-+%;-
| =$ :HX, ,+%/ ;: .
| =$ $H: -%%; ;=
| =$ HX -%%; ;=
| =$ .H$-%%; ;=
| :$ HX+$; ;=
| %@H+: ;=
|;/::::::::::::::::;:::::::::::::;:-X#@+:=;:::::::::::%+:::::::::::::::;
|X/---------------=%------------,,/X#M ---------------------------------
|$, / :%$M;/
|$- / ;%%%H:-$
|$- / ;%%-/H= :$
|$- / ;%%- $H, =$
|$- + ;%%- =HX =$
|$- .+ ;%+, ,XH/ =$
|$- ;;%+=-+XX% =$
|$- .;%%%$@#@X%$$%: =$
|$- :XX$;:%$%: =$
|$- -XH/ ;%+, =$
|$- +H+ /$+, =$
|$- XH +%+, =$
|$- H$ /$+, =$
|$-.H$/$+. =$
|+,@H%+. =$
|@#H;. :$
|##;/+++++++++++++++++++++++++++++++X@++++++++++++++++++++++++++++++++++
Stefan Ram
2024-08-30 22:26:28 UTC
Permalink
Post by Annada Behera
% Mark intersection points and draw dashed lines
\foreach \n in {1,...,\t} {
\path ({i-\n}) coordinate (i\n); % <--- Error Here
\fill[black] (i\n) circle (2pt);
\draw[dashed] (i\n) -- (i\n |- 0,0);
\draw[dashed] (i\n) -- (0,0 -| i\n);
}
Plain TeX to the rescue! What also works here is:

% Mark intersection points and draw dashed lines
\newcount\n
\n=1
\loop
\path ({i-\the\n}) coordinate (i\the\n);
\fill[black] (i\the\n) circle (2pt);
\draw[dashed] (i\the\n) -- (i\the\n |- 0,0);
\draw[dashed] (i\the\n) -- (0,0 -| i\the\n);
\advance\n by 1
\ifnum\n<5
\repeat
Stefan Ram
2024-08-31 18:14:25 UTC
Permalink
Post by Annada Behera
% Mark intersection points and draw dashed lines
\foreach \n in {1,...,\t} {
. . .
Post by Annada Behera
What is undefined?
Looks like "\t" is on the fritz or not pulling its weight,
since the loop seems to be cruising when we swap "\t" for "4":

\foreach \n in {1,...,4} {

. That error message was probably about as clear as LA smog.
Annada Behera
2024-09-01 07:52:58 UTC
Permalink
Post by Annada Behera
% Mark intersection points and draw dashed lines
\foreach \n in {1,...,\t} {
. . .
Post by Annada Behera
What is undefined?
  Looks like "\t" is on the fritz or not pulling its weight,
\foreach \n in {1,...,4} {
  . That error message was probably about as clear as LA smog.
In this line, \t is definitely "pulling its weight,"

\fill[name intersections={of=line and sine, name=i, total=\t}, black]
\foreach \s in {1,...,\t} {(i-\s) circle (2pt)};

It's just looks like \t's scope is limited to the commands where it is defined.
Don't know whether this is expected behavior or a bug.
Stefan Ram
2024-09-01 15:13:46 UTC
Permalink
Post by Annada Behera
Don't know whether this is expected behavior or a bug.
The TikZ & PGF manual (for Version 3.1.5b) has got this entry
"/tikz/intersection/total=<macro>" in the "13.3.2 Intersections
of Arbitrary Paths" section. It's got this example where
the "\foreach \s in {1,...,\t}" is all indented and stuff.

|\usetikzlibrary {intersections}
|\begin{tikzpicture}
| \clip (-2,-2) rectangle (2,2);
| \draw [name path=curve 1] (-2,-1) .. controls (8,-1) and (-8,1) .. (2,1);
| \draw [name path=curve 2] (-1,-2) .. controls (-1,8) and (1,-8) .. (1,2);
|
| \fill [name intersections={of=curve 1 and curve 2, name=i, total=\t}]
| [red, opacity=0.5, every node/.style={above left, black, opacity=1}]
| \foreach \s in {1,...,\t}{(i-\s) circle (2pt) node {\footnotesize\s}};
|\end{tikzpicture}
|
from "TikZ & PGF" "Manual for Version 3.1.5b".

If you drop a semicolon in front of the "\foreach" (right after
"opacity=1}]"), it's going to wig out on you with that error message.

Well, "\fill" is shorthand for "\path[fill]", and the 411 on
"\path" syntax is dropped in "14 Syntax for Path Specifications",
which lays it down like this:

|\path<specification>;

. So, there's that semicolon hanging ten, but I'm not stoked on where
its implications for the scope of "name intersections" is posted up.
Ulrich D i e z
2024-09-02 00:52:46 UTC
Permalink
Post by Annada Behera
\documentclass{standalone}
\usepackage{tikz, amsmath}
\usetikzlibrary{intersections}
\begin{document}\begin{tikzpicture}
% Plots
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[red, name path=line] (0,0) plot (\x, \x);
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[blue, name path=sine] (0,0) plot (\x, {\x + sin(\x r)});
% Drawing the dots
\fill[name intersections={of=line and sine, name=i, total=\t}, black]
\foreach \s in {1,...,\t} {(i-\s) circle (2pt)};
\end{tikzpicture}\end{document}
Now this code works as expected. But I also wanted to draw dashed lines from
the intersections to each axes.
% Axes
\draw [<->](-6.28, 0) -- (6.28, 0);
\draw [<->](0, -6.28) -- (0, 6.28);
% Mark intersection points and draw dashed lines
\foreach \n in {1,...,\t} {
\path ({i-\n}) coordinate (i\n); % <--- Error Here
\fill[black] (i\n) circle (2pt);
\draw[dashed] (i\n) -- (i\n |- 0,0);
\draw[dashed] (i\n) -- (0,0 -| i\n);
}
In this part, pdflatex (my distro is TeX Live 2024) throws an error what
I don't understand,
! Undefined control sequence.
l.28 }
What is undefined?
When you say

\errorcontextlines=10000
\documentclass{...

, the error-message is:

! Undefined control sequence.
\UseTextAccent ...p \@firstofone \let \@***@enc
\***@encoding
\@***@text@en...

\?-cmd ...sname \csname ?\string #1\endcsname \fi
\csname \***@encoding
\stri...

\***@dots@charcheck ***@dots@***@temp {#1
}\expandafter
\expandafter...

\***@dots@***@process ...value \pgffor@@stop

\***@alphabeticsequen...

\***@dotsscanend ***@process {\***@dotsend }

\***@dots@***@process...

\***@values ->1,...,\t ,
\***@stop ,
l.27 }


and you see that \t in "\foreach \n in {1,...,\t} {...}" is undefined,


The problem is that the macro \t comes into being while a TikZ-path is
evaluated - \fill is a macro which expands to "\path..." - while TikZ
does not do control-sequence-evaluation/macro-expansion as usual while
parsing/evaluating/carrying out a TikZ-path-directive.

So you face the nice problem that \t being defined is restricted to the
scope of that TikZ-path-directive while inside TikZ-path-directives you
cannot easily use macros/control-sequences as directives for saving \t
away as a global macro which is available outside the scope of the
\fill-path-directive also.

As \t denotes a natural number, you can work around this problem by as a
component of the TikkZ-path-directive specifying a TikZ-coordinate where
one component (either the X-component or the Y-component) comes from \t,
using the measurement-unit sp (scaled point) and later retrieving that
component of the TikZ-coordinate and using it with \number, hereby
taking into account that using a TeX-\dimen or a TeX-\skip or a
LaTeX-length with \number directly yields the numerical value which
belongs to the quantity in question when it is expressed as a multiple
of the measurement-unit sp (scaled point):



%\errorcontextlines=10000
\documentclass{standalone}
\usepackage{tikz, amsmath}
\usetikzlibrary{intersections}

\newlength\scratchlength

\begin{document}

\begin{tikzpicture}

% Plots
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[red, name path=line] (0,0) plot (\x, \x);
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[blue, name path=sine] (0,0) plot (\x, {\x + sin(\x r)});

% Draw the dots and use \t for saving a coordinate so that
% \t can later be retrieved outside the scope of the \fill-path
% as well:
\fill [name intersections={of=line and sine, name=i, total=\t},
black]
\foreach \s in {1,...,\t} {(i-\s) circle (2pt)}
% Before ending with a semicolon (;) the path-specifica-
% tion started via \fill, let's save total/\t as the
% y-value of a TikZ-coordinate whose name is "total";
% specify the unit sp (scaled point) as all lengths in
% TeX internally are calculated/rounded to be integer
% multiples of 1sp; thus when specifying sp you don't get
% rounding-errors when later retrieving the value:
coordinate (total) at (0pt, \t sp);

% Extraxt to \scratchlength the y-coordinate of the pgfpoint
% which forms the center-anchor of the coordinate-node whose
% name is "total" :
\pgfextracty{\scratchlength}{\pgfpointanchor{total}{center}}%
% When you use a TeX-\dimen or TeX-\skip/LaTeX-length with
% \number directly, you get the numerical value which belongs
% to the (unstretched and unshrinked) quantity in question
% when it is expressed as a multiple of the measurement-unit
% sp(scaled point).
% So we are lucky as in the begin dimensions for coordinates
% were provided with measurement unit sp:
\edef\t{\number\scratchlength}%
% Now we have \t defined outside the scope of the \fill-path
% and can use it in next \foreach-loop.
%\show\t

% Axes
\draw [<->](-6.28, 0) -- (6.28, 0);
\draw [<->](0, -6.28) -- (0, 6.28);

% Mark intersection points and draw dashed lines
\foreach \n in {1,...,\t} {
\path ({i-\n}) coordinate (i\n); % <--- Error Here
\fill[black] (i\n) circle (2pt);
\draw[dashed] (i\n) -- (i\n |- 0,0);
\draw[dashed] (i\n) -- (0,0 -| i\n);
}

\end{tikzpicture}

\end{document}



Sincerely

Ulrich
Ulrich D i e z
2024-09-02 04:25:49 UTC
Permalink
Post by Annada Behera
Consider this minimal working example. I have two functions, f(x) = x
and g(x) = x+sin(x). I want to draw them with PGF and at the intersection
points, I want put black dots. Here is the TikZ code,
I just realized that inside TikZ-paths you can use \pgfextra{...}
for having LaTeX carry out arbitrary LaTeX code.
You can use this for making \t a global macro:


%\errorcontextlines=10000
\documentclass{standalone}
\usepackage{tikz, amsmath}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}

% Plots
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[red, name path=line] (0,0) plot (\x, \x);
\draw[very thick, smooth, samples=20, domain=-6.28:6.28]
[blue, name path=sine] (0,0) plot (\x, {\x + sin(\x r)});

% Draw the dots and use \pgfextra for globally saving \t:
\fill [name intersections={of=line and sine, name=i, total=\t},
fill=green, draw=black, thin]
foreach \n in {1,...,\t} {(i-\n) circle (2pt)}
\pgfextra{\global\let\t\t};

% Draw dashed lines
\foreach \n in {1,...,\t} {
\draw[dashed] ({i-\n}) -- (0,0 -| {i-\n});
}


% Axes
\draw [<->](-6.28, 0) -- (6.28, 0);
\draw [<->](0, -6.28) -- (0, 6.28);

\end{tikzpicture}

\end{document}



Sincerely

Ulrich

Loading...