using the xfp package to calculate mathematical terms works fantastic and I am trying to automate some things to also make the \fp_eval:n input also presented properly with LaTeX notation.
My attempt is to therefore use l3regex commands to match the necessary expressions.
Now with the following command \regex_replace_all:nnN { (\() (.*) (\)/\() (.*) (\)) } { \c{frac} \cB\{ \2\cE\} \cB\{ \4\cE\} } \l_juergen_func_tl the first example works like a charm, however fails with the second example.
Code:
\documentclass{article}
\usepackage{xfp}
\begin{document}
\ExplSyntaxOn
\NewDocumentCommand{\showfuncandvalue}{ m m }
{% #1 = value, #2 = function
\tl_set:Nn \l_juergen_func_tl { #2 }
\regex_replace_all:nnN { \* } { \c{cdot} } \l_juergen_func_tl
\regex_replace_all:nnN { (\() (.*) (\)/\() (.*) (\)) } { \c{frac} \cB\{ \2\cE\} \cB\{ \4\cE\} } \l_juergen_func_tl
f(x) = \l_juergen_func_tl
;\quad
f(#1) = \juergen_showfunc_value:nn { #1 } { #2 }
}
\cs_new_protected:Nn \juergen_showfunc_value:nn
{
\tl_set:Nn \l_juergen_func_tl { #2 }
\regex_replace_all:nnN { x } { (x) } \l_juergen_func_tl
\regex_replace_all:nnN { x } { \cP\#1 } \l_juergen_func_tl
\cs_set:NV \__juergen_showfunc_f:n \l_juergen_func_tl
\fp_eval:n
{
round ( \__juergen_showfunc_f:n { ( \fp_eval:n { ( #1 ) } ) } , 4 )
}
}
\cs_generate_variant:Nn \cs_set:Nn { NV }
\ExplSyntaxOff
$$ \showfuncandvalue{2}{x-(4*x+3x^2)/(3-(2*x+x^2)) + 16} $$
$$ \showfuncandvalue{2}{(x+1)-(4*x+3x^2)/(3-(2*x+x^2)) - (16-x)} $$
The correct notation for the second example should be:
$$(x+1)-\frac{4\cdot x+3x^2}{3-(2\cdot x+x^2)}-(16-x)$$
\end{document}
Now my question is how to make the regEx pattern more intelligent to also make the second example work well. Note: There might be more pairs of parentheses in front of the fraction and of course after the fraction, which I didn't setup within the example.
My regEx pattern doesn't find the correct matching parentheses, that's obvious.
Any help to structure a regEx pattern that could translate xfp syntax into LaTeX syntax would be appreciated.



\[...\]preferable to$$...$$? – Werner Dec 26 '17 at 20:36l3regex) cannot match parentheses.xparsecan, so I've abused it in my answer, but obviously Ulrike's approach is much better. – Bruno Le Floch Dec 28 '17 at 13:02