
Wenn ich eine Funktion habe
function(a, b)
print(a+b)
Wie kann ich dies in die Tikz-Sprache übersetzen, sodass ein „a“ und ein „b“ erforderlich sind?
hello = 1
bye = 2
function(hello, bye)
Die Ausgabe davon wäre 3
Antwort1
Die Funktion heißt add
. Sie können Makros darum herum packen.
\documentclass{article}
\usepackage{pgf}
\newcommand{\myaddandprint}[2]{\pgfmathparse{add(#1,#2)}\pgfmathprintnumber\pgfmathresult}
\newcommand{\myparseandprint}[1]{\pgfmathparse{#1}\pgfmathprintnumber\pgfmathresult}
\begin{document}
\pgfkeys{/pgf/declare function={hello=1;bye=2;}}
\pgfmathparse{add(hello,bye)}\pgfmathprintnumber\pgfmathresult
or \pgfmathparse{hello+bye}\pgfmathprintnumber\pgfmathresult
\myaddandprint{hello}{bye}
\myparseandprint{hello+bye}
\end{document}
Wenn Sie etwas haben möchten, das mehr an die Sprache Python erinnert, tikzmath
ist dies vielleicht das, wonach Sie suchen.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\tikzmath{integer \x, \y, \z;
\x = 1;
\y = 2;
let \z=\x+\y;
print {$x=\x$, $y=\y$, $z=x+y=\x+\y=\z$};}
\end{document}
Wenn Sie erweiterbare Funktionen haben möchten, können Sie verwenden xfp
.
\documentclass{article}
\usepackage{xfp}
\begin{document}
\begingroup\def\x{1}\def\y{2}\fpeval{\x+\y}\endgroup
\end{document}
Solange Sie bei dem, was Sie tun, vorsichtig sind, können Sie es mit PGF mischen, aber Sie müssen daran denken, dass dadurch die Einheiten entfernt werden.
Antwort2
\begin{tikzpicture}[declare function={yeet(\a,\b)=\a+\b;}]
\draw (0,0)--(3,3);
\draw (0,3)--({yeet(2,1)},{yeet(5,3)});
\end{tikzpicture}
Das ist perfekt für das, was ich tun möchte. In diesem Skript zeichne ich eine Linie von (0,0)--(3,3) und eine weitere Linie von (0,3)--(3,8), weil 2+1=3 und 5+3=8