# Using mathematical function in PGF/TikZ

I want to use ProbXpos for example to position tic marks and to position line ends. It seems to calculate as I intend, but how can I use the calculated value? The example below prints a sample result value. If I un-comment the commented lines it won't even compile.

Also -- minor point -- some is there a ln 10 function? Doesn't work for me.

\documentclass{article} %maybe a better one?
\usepackage{tikz}
\newcommand{\ProbXpos}[1]{\pgfmathparse{ln(#1/(1-#1))/ln 10}\pgfmathresult }
\begin{document}
\begin{tikzpicture}
\draw (0,1) node {\ProbXpos{0.1} result prints ok} ;
%   \draw (\ProbXpos{.0001},0) -- (\ProbXpos{0.999},0)
%                   node    {cannot use result numerically} ;
\end{tikzpicture}
\end{document}

-
ln10 function? ln is a e base logarithm. The usual convention is ln only. – jpayansomet Mar 28 '13 at 22:14
@jpayansomet ln 10 as in the logarithm of the base 10? It's called log10(x), or is the constant value of ln(10) actually meant? – Qrrbrbirlbel Mar 28 '13 at 22:22
I don't know. Usually, you can use ln like logarithm base e (neperian number), neperian logarithm. In fact, ln(10) is a constant, neperian logarithm of 10 = 2,302585... – jpayansomet Mar 28 '13 at 22:32
Thanks -- log10(x) does the trick. Dividing by ln(10) as I was doing is a synthetic way to find the 10-logarithm. – Charles Brenner Mar 28 '13 at 22:47

Maybe you are interested in making ProbXpos in a function that can be used inside a coordinate.

The extra pair of braces are needed as ProbXpos(…) contains parentheses itself.

You could also use a definition as in

\newcommand{\ProbXpos}[1]{{log10(#1/(1-#1))}}


which can be used inside a coordinate, too.
But it can not be modified, i.e. (2*\ProbXpos{…}, 0), because it already contains the braces.

Then again, we could leave out the extra pair of braces, but then we need to use { } in the coordinate.

## Code (declare function variant)

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[declare function={ProbXpos(\x)=log10(\x/(1-\x));}]% could be global …
\draw (0,1) node {\pgfmathparse{ProbXpos(0.1)}\pgfmathresult\ result prints ok} ;
\draw ({ProbXpos(.0001)},0) -- ({ProbXpos(0.9999)},0);
\end{tikzpicture}
\end{document}


## Code (PGF math function)

\documentclass[tikz]{standalone}
\makeatletter
\pgfmathdeclarefunction{ProbXpos}{1}{%
\begingroup
\pgfmathparse{log10(#1/(1-#1))}%
\pgf@x=\pgfmathresult pt\relax
\pgfmathreturn\pgf@x
\endgroup
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw (0,1) node {\pgfmathparse{ProbXpos(0.1)}\pgfmathresult\ result prints ok} ;
\draw ({ProbXpos(.0001)},0) -- ({ProbXpos(0.9999)},0);
\end{tikzpicture}
\end{document}


## Code (LaTeX macro)

\documentclass[tikz]{standalone}
\newcommand{\ProbXpos}[1]{log10(#1/(1-#1))}
\begin{document}
\begin{tikzpicture}
\draw (0,1) node {\ProbXpos{0.1} result prints ok} ;
\draw ({\ProbXpos{.0001}},0) -- ({\ProbXpos{0.999}},0);
\end{tikzpicture}
\end{document}


## Output (LaTeX macro)

-
Thanks. That seems to be what I want. I'll need to study it a lot more. So far my testing gives haphazard results -- picture usually mixed up -- but I did manage to draw a version of the line one time. By the way I'm on Windows -- maybe not the most natural platform for this purpose. – Charles Brenner Mar 28 '13 at 23:52

there may be a more tikz-way but it works to move the assignment out of the path syntax.

  \documentclass{article} %maybe a better one?
\usepackage{tikz}
\newcommand{\ProbXpos}[2]{\pgfmathparse{ln(#2/(1-#2))/ln 10}\let#1\pgfmathresult }
\begin{document}
\begin{tikzpicture}
\ProbXpos\tmpa{.0001}
\ProbXpos\tmpb{0.999}
\draw (0,1) node {\ProbXpos\tmp{0.1}\tmp result prints ok} ;
\draw (\tmpa,0) -- (\tmpb,0)
node    {cannot use result numerically} ;
\end{tikzpicture}
\end{document}

-
Thanks, that is great. I assume that understanding why it is better involves understanding the underlying pgf that Tikz is built on. I accidently posted before adding a further aspect of my question -- I'd like to make a set of tic marks whose positions are calculated by using ProbXpos in a \foreach loop. Therefore I am interested in the "more tikz-way" if anyone can supply it. – Charles Brenner Mar 28 '13 at 22:41
See @Qrrbrbirlbel's answer I think he may have an advantage over me of having used Tikz before:-) – David Carlisle Mar 28 '13 at 22:58

Variant from Qrrbrbirlbel's solution with \pgfmath@smuggleone\pgfmathresult:

\documentclass{scrartcl}
\usepackage{tikz}
\makeatletter
\pgfmathdeclarefunction{ProbXpos}{1}{%
\begingroup
\pgfmathparse{ln(#1/(1-#1))/ln 10}%
\pgfmath@smuggleone\pgfmathresult
\endgroup
}%
\makeatother

\begin{document}
\pgfmathparse{ProbXpos(0.999)}\pgfmathresult

\begin{tikzpicture}
\draw (ProbXpos{.0001},0) -- (ProbXpos{0.9999},0)  ;
%or
\draw ({ProbXpos(.0001)},-1) -- ({ProbXpos(0.9999)},-1);
\end{tikzpicture}
\end{document}


-
That is very helpful too, as it shows me how I can convert ProbXpos() to a display value. Thank you for it. – Charles Brenner Apr 3 '13 at 6:23