With \def\a{pi}, \pgfmathparse{\a r} or \draw (1,0) -- ({cos(\a r)}, {sin(\a r)}) will raise errors because i) pgfmath does \edef to the expression before parsing it and ii) the space after \a, a control word (a backslash followed by one or more characters with catcode 11), is skipped. Therefore the expression ready to be parsed always contain pir, not pi r.
In question comments,
- My first suggestion
\def\a{pi\space} makes sure that after \edef a space is left in between pi and r (\space expands to a normal space char). Other tricks commonly used to reserve space char after a control word may not work here:
\def\a{pi\ } doesn't work because \ is not expandable and the pgfmath parser can't handler it.
\def\a{pi} with \pgfmathparse{\a{} r} doesn't work either because {} won't disappear after \edef and curly braces denote array-like structures in pgfmath expression.
- The second suggestion is replacing the postfix operator
r with its function form deg: cos(\a r) -> cos(deg(pi)).
Here's a third one: You could define pseudo-constant as a zero-arg pgfmath function.
\documentclass{article}
\usepackage{tikz}
\makeatletter
% option 1
\tikzset{
declare function={
constA=pi;
}
}
% option 2
\pgfmathdeclarefunction{constB}{0}{%
\begingroup
\pgfmathparse{pi}%
\pgfmathreturn{\pgfmathresult pt}% assign to a length is the most robust way
\endgroup%
}%
\makeatother
\begin{document}
\pgfmathprint{constA r},
\pgfmathprint{constB r}
\tikz \draw (1,0) -- ({cos(constA r)}, {sin(constB r)}) ;
\end{document}
Note if the function has a single-letter name, like a in declare function={ a=pi; }, then you'll encounter the not-yet resolved pgf issue https://github.com/pgf-tikz/pgf/issues/1077.
BTW, \pgfmathparse is not fully expandable, hence writing \edef\tp{\pgfmathparse{...}} is dangerous and there's no warranty that this \edef and the following use of \tp will work without errors in future versions.
Another question is : is possible to use xfp to get the conversion or another tool ?
Yes it's possible. xfp's \fpeval (or the equivalent latex3 function \fp_eval:n) is fully expandable (and sometimes more accurate than pgfmath), hence it's valid to use \fpeval in a pgfmath expression, except it's a bit strange.
LaTeX3 module l3fp defines pi as the normal 3.1415... constant, but deg as another constant denoting radians per degree. Therefore \pgfmathprint{pi r} and \fpeval{pi/deg} do the same radian-to-degree conversion.
\documentclass{article}
\usepackage{pgfmath}
\usepackage{xfp}
\begin{document}
\def\a{pi}
\pgfmathprint{sin(\fpeval{\a/deg})}, % = 0.0
\pgfmathprint{cos(\fpeval{\a/deg})} % = -1.0
\end{document}
\def\a{pi\space}.\def\a{pi/1}(\space is better). Are there other possibilities?deg. I’m on mobile device, hence sorry I didn’t run tests before adding comments. Maybe related: github.com/pgf-tikz/pgf/issues/1077.degis a possiblity