5

Is there a possibility to have x instead of #1 for the name of the variable in the macro?

I tried it with \regex_replace_all:nnN, but that did not work.

Many thanks for your help.

enter image description here

\documentclass{article}
\usepackage{siunitx}

\begin{document}

\ExplSyntaxOn

\NewDocumentCommand{\FuncValue}{ O{} m m }
 {% #1 = option list, #2 = value, #3 = function
  \group_begin:
    \ensuremath{f(x)=x^2; \qquad 
    f\left(#2\right) = \thomas_function_value:nn { #2 } { #3 } }
  \group_end:
 }

\cs_new_protected:Nn \thomas_function_value:nn
 {
    \tl_set:Nn \l_thomas_funk_tl { #2 }
    \regex_replace_all:nnN { x } { \c{\#}1 } \l_thomas_funk_tl
    \cs_set:Nn \__thomas_functionValue_function:n  { #2 }
        \fp_eval:n
         {
          round( \__thomas_functionValue_function:n { \fp_eval:n { #1 } }, 3 )
         }
 }

\ExplSyntaxOff

\FuncValue{2^3}{#1^2}

\end{document}
  • \regex_replace_all:nnN { x } { (#1) } \l_thomas_funk_tl? – Manuel Dec 22 '17 at 10:33
  • @Manuel, Thank you. This works in the expected way. – user139826 Dec 22 '17 at 10:36
7

Although I do not really understand your steps in between, I guess the following code will do what you want (if you want to just calculate the function's value at a given point you call x).

functions

\documentclass{article}
\usepackage{siunitx}

\begin{document}

\ExplSyntaxOn

\NewDocumentCommand{\FuncValue}{ O{} m m }
 {% #1 = option list, #2 = value, #3 = function
  \group_begin:
    \ensuremath{f(x)=#3; \qquad 
    f\left(#2\right) = \thomas_function_value:nn { #2 } { #3 } }
  \group_end:
 }

\cs_new_protected:Nn \thomas_function_value:nn
 {
    \tl_set:Nn \l_thomas_funk_tl { #2 }
    \regex_replace_all:nnN { x } { ( #1 ) } \l_thomas_funk_tl
    \fp_eval:n
       {
         round( \l_thomas_funk_tl , 3 )
       }
 }

\ExplSyntaxOff

\FuncValue{2^3}{x^2}

\end{document}
  • -TeXnician, yes this is what I want. Thank you! – user139826 Dec 22 '17 at 10:34
5

Here's a solution along your lines, although there are simpler ways. The problem is in doing the correct substitution: \# does not stand for the parameter marker, whereas \cP\# does. Next, you have to set the replacement text for \__thomas_functionValue_function:n to be the value of the modified token list.

\documentclass{article}
\usepackage{siunitx}

\begin{document}

\ExplSyntaxOn

\NewDocumentCommand{\FuncValue}{ O{} m m }
 {% #1 = option list, #2 = value, #3 = function
  \ensuremath % <-- is it really needed?
   {
    f(x)=#3;
    \qquad 
    f(#2) = \thomas_function_value:nn { #2 } { #3 }
   }
 }

\cs_new_protected:Nn \thomas_function_value:nn
 {
  \tl_set:Nn \l_thomas_funk_tl { #2 }
  \regex_replace_all:nnN { x } { \cP\#1 } \l_thomas_funk_tl
  \cs_set:NV \__thomas_functionValue_function:n  \l_thomas_funk_tl
  \fp_eval:n
   {
    round( \__thomas_functionValue_function:n { \fp_eval:n { #1 } }, 3 )
   }
 }
\cs_generate_variant:Nn \cs_set:Nn { NV }

\ExplSyntaxOff

\FuncValue{2^3}{x^2}

\FuncValue{pi}{sin(x)+cos(x)}

\FuncValue{pi/2}{sin(x)+cos(x)}

\end{document}

The typesetting of the functions is horrible, of course; I believe that your options will take care for it.

enter image description here

  • -@egreg, I am excited about your solution. – user139826 Dec 22 '17 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy