Annada Behera wrote:
\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 \@curr@enc
\cf@encoding \@use@text@en...
l.28 }
What is undefined?
When you say
\errorcontextlines=10000
\documentclass{...
, the error-message is:
! Undefined control sequence.
\UseTextAccent ...p \@firstofone \let \@
curr@enc \
cf@encoding\@
use@text@en...
\?-cmd ...sname \csname ?\string #1\endcsname \fi
\csname \
cf@encoding\stri...
\
pgffor@dots@charcheck
...@dots@
charcheck@temp {#1
}\expandafter
\expandafter...
\
pgffor@dots@
value@process ...value \pgffor@@stop
\
ifpgffor@alphabeticsequen...\
pgffor@dotsscanend ...@process {\
pgffor@dotsend }
\
pgffor@dots@
value@process...\
pgffor@values ->1,...,\t ,
\
pgffor@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