Own mathematical function f(x)
Solution 1
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.
\documentclass{article}
\usepackage{amsmath}% BREAKS ORIGINAL CODE; REQUIRES \[email protected] IN \setfunc
\makeatletter
\newcommand\setfunc[2][f]{\expandafter\[email protected]\csname#1\endcsname(##1){#2}}
\makeatother
\def\func#1(#2){\csname#1\endcsname(#2)}
\def\listfunc#1(#2){#1(#2)=\func#1(#2)}
\newcommand\x{(##1)}
\begin{document}
\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
\medskip
Derivatives:\par
\setfunc[y]{4\x^5  2\x^2 +3}
\setfunc[y']{20\x^4  4\x}
\setfunc[y'']{80\x^3  4}
\setfunc[y''']{240\x^2}
\setfunc[y^{iv}]{480\x}
$\listfunc y(2)$\par
$\listfunc y'(2)$\par
$\listfunc y''(2)$\par
$\listfunc y'''(2)$\par
$\listfunc y^{iv}(2)$\par
\end{document}
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)})$
.
Solution 2
This raises more complications than it solves, in my opinion, but here's an idea:
\documentclass{article}
\makeatletter
\newcommand{\setfunc}[4]{%
\@namedef{[email protected]}##1{#1}%
\@namedef{[email protected]'}##1{#2}%
\@namedef{[email protected]''}##1{#3}%
\@namedef{[email protected]'''}##1{#4}%
}
\def\f#1#{\@nameuse{[email protected]#1}}
\makeatother
\begin{document}
\setfunc{\sin(#1)}{\cos(#1)}{\sin(#1)}{\cos(#1)}
$\f{x}$ $\f'{1}$ $\f''{\pi}$ $\f'''{\pi/2}$
\setfunc{\log(#1)+1}{}{}{}
$\f{3}$
\end{document}
If you just want the function and not the derivatives, it's much simpler:
\newcommand{\setfunc}[1]{\renewcommand\f[1]{#1}}
Full example:
\documentclass{article}
\newcommand{\f}[1]{} % initialize
\newcommand{\setfunc}[1]{\renewcommand{\f}[1]{#1}}
\begin{document}
\setfunc{\sin(#1)}
$\f{x}+\f{3}$
\setfunc{\log(#1)+1}
$\f{3}$
\end{document}
A different implementation, where you can set any name you like (but beware of not redefining already existing commands)
\documentclass{article}
\newcommand{\setfunc}[2][\f]{\def#1##1{#2}}
\begin{document}
\setfunc{\sin(#1)}
$\f{x}+\f{3}$
\setfunc[\g]{\log(#1)+1}
$\g{3}$
\end{document}
Related videos on Youtube
Joel Duscha
Updated on March 25, 2020Comments

Joel Duscha over 2 years
I'd like to define an custom function f(x).
For example
\f{3}
should println(3) + 3
if the function is set toln(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).

Werner over 6 yearsYour 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, ... 
cfr over 6 yearsPlease 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.) 
Werner over 6 yearsDo you want to evaluate the functions, or just print their algebraic representation?

Joel Duscha over 6 years@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.

Joel Duscha over 6 yearsReally impressive. Is there a way to add more functions like
setfunc[2]{}
? 
Steven B. Segletes over 6 years@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]{}
? 
Steven B. Segletes over 6 years@JoelDuscha Please see my revision. The syntax has changed slightly (for the better).

Joel Duscha over 6 yearsNaming them /g /f was exactly what I needed. New syntax is even better. EDIT works now.

Joel Duscha over 6 yearsUnfortunately, I get this error only in my document: "Use of \\setfunc doesn't match its definition. \@ifnextchar ... \[email protected] =#1\def \[email protected] { #2}\def \[email protected] {#3}\f... l.21 \setfunc{\sin(\x)}"

Steven B. Segletes over 6 years@JoelDuscha Did you update your syntax to use parens instead of braces? If you can't locate the error in your syntax, maybe edit your question and post a small MWE at the end. I could then have a look.

Joel Duscha over 6 yearsThe error is caused by
\usepackage{amsmath}
. Is this helpful or do you still need a small MWE? 
Steven B. Segletes over 6 years@JoelDuscha That helps. Let me consider it more.

Steven B. Segletes over 6 years@JoelDuscha Fixed!

Joel Duscha over 6 yearsThis 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.

egreg over 6 years@JoelDuscha Wasn't you who asked for supporting also setting the first three derivatives?

Steven B. Segletes over 6 years@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
. 
Joel Duscha over 6 yearsWhich is just another function. I see I confused everyone. Sorry for that.

Steven B. Segletes over 6 years@JoelDuscha Derivates are now possible, with latest improvements.

Joel Duscha over 6 years
\f ( \ln(a + 1.5) )
causes the definition to end at the first)
Maybe it would make more sense to implement\f {...}
or some special brackets to avoid this. But I agree that\f(3)
is a more natural syntax. 
Steven B. Segletes over 6 years@JoelDuscha I could implement a braced format, if that were the case. However, the function definition is done with
\setfunc{}
which already uses a braced format. Your example,\f (\ln(a + 1.5))
, is it really plausible? What it is asking is to take a function defined in\setfunc{}
, and replace every instance of\x
with\ln(a + 1.5)
. Correct me if I am mistaken, but that does not seem like a plausibly desired use of the method, is it? 
Joel Duscha over 6 yearsYes it is exactly what I want. The value of the function at x = ln(a + 5). I'm doing function group. Every value for an 'a' gives another function.

Steven B. Segletes over 6 years@JoelDuscha You can use the existing code, if you embrace the argument inside the parens:
$\listfunc y''({\ln(a + 1.5)})$