Sintaxe da função \tikzmath{....?

Sintaxe da função \tikzmath{....?

O que há de errado com a sintaxe das duas \tikzmathdefinições de função aqui?

\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, seja gentil: ainda sou um novato TikZ!)

Responder1

Aqui está um pequeno exemplo de uso. Observe que eu uso \tikzmathentre \begin{document}e \end{document}, não no preâmbulo.

Sobre a colocação de semi-colunas: o manual afirma quetoda instrução (tikzmath) deve terminar com ponto e vírgula.

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

insira a descrição da imagem aqui

Editar:mais sobre o que é uma declaração.

De acordo com o pgf/TikManual Z, existem diferentes tipos de instruções em um \tikzmathcomando:

  • Atribuição (por exemplo, \a = 3 + 4;ou let \x = 2*3;)
  • "Declaração de tipo" ( int \i;, real \z;ou coordinate \c;)
  • iterações (por exemplo for \n in {1,...,5} {⟨other statements⟩};)
  • condicionais (por exemplo if ⟨condition⟩ then ⟨statements⟩ else ⟨statements⟩;)
  • declaração de função (por exemplo function product(\x,\y) {return \x*\y;};, observe que aqui return \x*\y;também é uma instrução, e a segunda ;no primeiro código é para a instrução de declaração de função)

Se uma instrução começar com uma chave {, a chave de fechamento }deverá ser seguida por uma semicoluna.

Aqui tomo um exemplo do pgf/TikManual Z, com comentários adicionais (seção 58.7Executando código fora do analisador, página 708 do manual atual):

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

Observe que o \drawcomando Tikz dentro de \tikzmathestá entre colchetes, portanto, após o ;final de \draw, há também um ;após a chave de fechamento.

informação relacionada