• [LaTeX][PGF/TikZ] Undefined control sequence error for PGF intersection

    From Annada Behera@21:1/5 to All on Fri Aug 30 13:29:14 2024
    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 \@curr@enc
    \cf@encoding \@use@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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Annada Behera on Fri Aug 30 12:20:12 2024
    Annada Behera <segfault@tilde.green> wrote or quoted:
    \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@++++++++++++++++++++++++++++++++++

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Annada Behera on Fri Aug 30 22:26:28 2024
    Annada Behera <segfault@tilde.green> wrote or quoted:
    % 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Annada Behera on Sat Aug 31 18:14:25 2024
    Annada Behera <segfault@tilde.green> wrote or quoted:
    % Mark intersection points and draw dashed lines
    \foreach \n in {1,...,\t} {
    . . .
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Annada Behera@21:1/5 to Stefan Ram on Sun Sep 1 13:22:58 2024
    On Sat, 2024-08-31 at 18:14 +0000, Stefan Ram wrote:
    Annada Behera <segfault@tilde.green> wrote or quoted:
    % Mark intersection points and draw dashed lines
    \foreach \n in {1,...,\t} {
    . . .
    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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Annada Behera on Sun Sep 1 15:13:46 2024
    Annada Behera <segfault@tilde.green> wrote or quoted:
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ulrich D i e z@21:1/5 to Annada Behera on Mon Sep 2 02:52:46 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ulrich D i e z@21:1/5 to Annada Behera on Mon Sep 2 06:25:49 2024
    Annada Behera wrote:

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)