Syntax der \tikzmath{Funktion....?

Syntax der \tikzmath{Funktion....?

Was ist hier an der Syntax der beiden \tikzmathFunktionsdefinitionen falsch?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\tikzmath{function f(\x) { return sin(\x);
  }

\begin{document}

\begin{tikzpicture}    

  \tikzmath{function g(\x) { return cos(\x);
  };

\end{tikzpicture}

\end{document}

(Seien Sie bitte nachsichtig: Ich bin noch ein blutiger Neuling bei TikZ!)

Antwort1

Hier ist ein kleines Verwendungsbeispiel. Beachten Sie, dass ich \tikzmathzwischen \begin{document}und verwende \end{document}, nicht in der Präambel.

Zur Platzierung von Halbsäulen: Im Handbuch steht, dassjede (tikzmath) Anweisung sollte mit einem Semikolon enden.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\tikzmath{
    function f(\x) { 
        return sin(\x);
        };
    real \f;
    \f = f(90);
    print \f;
    print \newline; % new line; the next line begins with no indent
    print f(45); % prints "f(45)" verbatim
    % No blank line allowed
    print \par; % new paragraph; the next line begins with an indent
    \f = f(-45);
    print \f;
}

Foo

\begin{tikzpicture}    

\tikzmath{
    function g(\x) {
        return cos(\x);
        };
    real \g;
    \g = g(180);
    };
\node at (0,0) {\g}; % use \g outside of \tikzmath, in a tikz node 
\end{tikzpicture}

\end{document}

Bildbeschreibung hier eingeben

Bearbeiten:mehr darüber, was eine Aussage ist.

Laut pgf/TikIm Z-Handbuch gibt es in einem \tikzmathBefehl verschiedene Arten von Anweisungen:

  • Zuordnung (zB \a = 3 + 4;oder let \x = 2*3;)
  • „Typdeklaration“ ( int \i;, real \z;oder coordinate \c;)
  • Iterationen (zB for \n in {1,...,5} {⟨other statements⟩};)
  • Konditionalsätze (zB if ⟨condition⟩ then ⟨statements⟩ else ⟨statements⟩;)
  • Funktionsdeklaration ( function product(\x,\y) {return \x*\y;};beachten Sie beispielsweise, dass es sich hier return \x*\y;auch um eine Anweisung handelt und die zweite ;im ersten Code für die Funktionsdeklarationsanweisung gilt)

Wenn eine Anweisung mit einer Klammer beginnt {, muss auf die schließende Klammer }ein Semikolon folgen.

Hier nehme ich ein Beispiel aus dem pgf/TikZ-Handbuch mit zusätzlichen Kommentaren (Abschnitt 58.7Ausführen von Code außerhalb des Parsers, Seite 708 im aktuellen Handbuch):

\begin{tikzpicture}
\draw [help lines] grid (3,2); % a common `\draw` TikZ command,
                               % which ends with a ";"
\tikzmath{ % begin of the \tikzmath command. Not a statemnt
  coordinate \c;               % a statement
  for \x in {0,10,...,360} {   % begin of a statement, no ";" here
    \c = (1.5cm, 1cm) + (\x:1cm and 0.5cm); % a statement inside
                                            % the for statement
    { \fill (\c) circle [radius=1pt]; }; % a second statement
                                         % inside the for statement.
                                         % In it, a common TikZ command
                                         % (`\draw`) which ends as usual
                                         % by a semi-column.
  }; % the end of the for statement. Ended by a semi-column
} % end of \tikzmath command. It's not a statement
\end{tikzpicture}

Beachten Sie, dass der Tikz- \drawBefehl innerhalb \tikzmathvon Klammern umgeben ist, sodass nach dem ;für das Ende von auch nach der schließenden Klammer \drawein steht .;

verwandte Informationen