¿Sintaxis de \tikzmath{función....?

¿Sintaxis de \tikzmath{función....?

¿Qué hay de malo en la sintaxis de las dos \tikzmathdefiniciones de funciones aquí?

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

(Por favor, sea amable: ¡todavía soy un novato TikZ!)

Respuesta1

Aquí hay un pequeño ejemplo de uso. Tenga en cuenta que uso \tikzmathentre \begin{document}y \end{document}, no en el preámbulo.

Sobre la colocación de semicolumnas: el manual indica quecada declaración (tikzmath) debe terminar con un punto y coma.

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

ingrese la descripción de la imagen aquí

Editar:más sobre qué es una declaración.

Según el pgf/TikManual Z, existen diferentes tipos de declaraciones en un \tikzmathcomando:

  • Tarea (por ejemplo, \a = 3 + 4;o let \x = 2*3;)
  • "Declaración de tipo" ( int \i;, real \z;o coordinate \c;)
  • iteraciones (por ejemplo for \n in {1,...,5} {⟨other statements⟩};)
  • condicionales (por ejemplo if ⟨condition⟩ then ⟨statements⟩ else ⟨statements⟩;)
  • declaración de función (por ejemplo function product(\x,\y) {return \x*\y;};, tenga en cuenta que aquí return \x*\y;también hay una declaración, y la segunda ;en el primer código es para la declaración de función)

Si una declaración comienza con una llave {, la llave de cierre }debe ir seguida de una semicolumna.

Aquí tomo un ejemplo del pgf/Ti.kManual Z, con comentarios adicionales (apartado 58.7Ejecutar código fuera del analizador, página 708 del manual actual):

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

Tenga en cuenta que el \drawcomando Tikz dentro de \tikzmathestá rodeado por llaves, por lo que después del ;final de \draw, también hay una llave ;después de la llave de cierre.

información relacionada