• I'd like to define an custom function f(x).

  • For example \f{3} should print ln(3) + 3 if the function is set to ln(x) + 3.

  • One should be able change the function: \setfunc{sin(\x}}.

  • This should only affect future uses of \f{...}

  • And it should be possible to define the first three derivatives..

The commands do not have do be this way. There may be a more elegant/practical way. Warning: It should work in this environment: https://tex.stackexchange.com/a/299720/101053

Edit: Changed derivate to derivative; It's unclear what "define the derivative" should mean. I tried to say that I can simply add other functions (whether derivative or nor).

  • Your final point can also be done manually, right? Say, \func{n} for the function, \func[1]{n} for the first or derivative, \func[2]{n} for the second order derivative, ...
    – Werner
    Commented Mar 23, 2016 at 23:13
  • Please post a complete example. What's a derivate? Do you mean derivative? With Werner's suggestion, you could define the function using 4 arguments, say, \setfunc{}{}{}{}. I don't see how TeX can be expected to figure out the derivatives automatically. (Unless you restrict the set of possible functions so that they can be handled by an automatic algorithm, say.)
    – cfr
    Commented Mar 23, 2016 at 23:54
  • Do you want to evaluate the functions, or just print their algebraic representation?
    – Werner
    Commented Mar 24, 2016 at 0:44
  • @Werner is right. This could be done.cfr Yes I mean derivatives. Or in other words just other functions. They should be entered manually. But 4 arguments would be difficult to use because sometimes I may only use one or two functions. Commented Mar 24, 2016 at 1:52

2 Answers 2


If this works generally, I just got lucky. EDITED to do derivatives.

EDITED To be more true to math mode. EDITED to allow different function names with use of optional argument (default \f). EDITED to use more natural syntax \f(3) rather than \f{3}. EDITED to provide \listfunc macro. EDITED to work with amsmath.

Finally, EDITED to allow a more general syntax that can include primes, subscripts etc. in the function name itself.

\usepackage{amsmath}% BREAKS ORIGINAL CODE; REQUIRES \protected@edef IN \setfunc
\setfunc{\sin\x} I can list the function: $\listfunc f(3)$\par
or I can just print out $\f(x+y)$.\par
or with a general input syntax: $\func f(x+y)$\par
\setfunc[g'_y]{\ln\x + 3\x^2} Now we can have $\listfunc g'_y(7)$\par
\setfunc[y]{4\x^5 - 2\x^2 +3}
\setfunc[y']{20\x^4 - 4\x}
\setfunc[y'']{80\x^3 - 4}
$\listfunc y(2)$\par
$\listfunc y'(2)$\par
$\listfunc y''(2)$\par
$\listfunc y'''(2)$\par
$\listfunc y^{iv}(2)$\par

enter image description here

NOTE: Joel noted that the method can get confused if the evaluation value itself contains a term in parentheses, for example, $\f ( \ln(a + 1.5) )$. The workaround for this is to embrace the inner argument, such as $\f({\ln(a + 1.5)})$ or $\listfunc y''({\ln(a + 1.5)})$.

  • Really impressive. Is there a way to add more functions like setfunc[2]{}? Commented Mar 24, 2016 at 2:10
  • @JoelDuscha I've been tweaking the answer so I can name the target function \f or \g for example. But I'm not sure what functionality you mean by \setfunc[2]{}? Commented Mar 24, 2016 at 2:14
  • 1
    @JoelDuscha Fixed! Commented Mar 24, 2016 at 10:19
  • 1
    @JoelDuscha One last improvement... Function names can now have primes, subscripts, etc., and can be invoked, as in my MWE as \setfunc[g'_y]{\ln(\x) + 3\x^2} and displayed with \func. Commented Mar 24, 2016 at 10:35
  • 1
    @JoelDuscha You can use the existing code, if you embrace the argument inside the parens: $\listfunc y''({\ln(a + 1.5)})$ Commented Mar 24, 2016 at 17:23

This raises more complications than it solves, in my opinion, but here's an idea:




$\f{x}$ $\f'{1}$ $\f''{\pi}$ $\f'''{\pi/2}$



enter image description here

If you just want the function and not the derivatives, it's much simpler:


Full example:


\newcommand{\f}[1]{} % initialize





enter image description here

A different implementation, where you can set any name you like (but beware of not redefining already existing commands)






  • This got somehow more upvotes than the other answers. I don't quite understand why. For my purpose this would make thinks messy I guess. But Maybe I overlook something here. Commented Mar 24, 2016 at 10:20
  • 1
    @JoelDuscha Wasn't you who asked for supporting also setting the first three derivatives?
    – egreg
    Commented Mar 24, 2016 at 10:28
  • Which is just another function. I see I confused everyone. Sorry for that. Commented Mar 24, 2016 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .