I'm trying to read two functions of x from a file, and then get the result of f(8) / g(8) using pgfmathparse
.
Here is a minimal (not) working example:
\begin{filecontents}{data.dat}
f(x) g(x)
x x+1
\end{filecontents}
\documentclass{article}
\usepackage{xstring,pgfplotstable,filecontents}
\begin{document}
\newcommand{\getfx}[1]{
\pgfplotstablegetelem{#1}{f(x)}\of{data.dat}
}
\newcommand{\getgx}[1]{
\pgfplotstablegetelem{#1}{g(x)}\of{data.dat}
}
\def\feight{
\getfx{0}
\StrSubstitute[0]{\pgfplotsretval}{x}{8}
}
\def\geight{
\getgx{0}
\StrSubstitute[0]{\pgfplotsretval}{x}{8}
}
\pgfmathparse{
(\feight)/(\geight)
}
\pgfmathresult
\end{document}
Using this, I am able to print f(8)/g(8) easily (which is to say, "8/(8+1)"), but the pgfmathparse
bit gives me an error on compilation "Undefined control sequence." on L30.
What can I do to fix this code?
\pgfmathparse
is able to convert its input strings to math expressions and return the result as a string. So really the only representation I'm dealing with is strings. Also it's important to note that everything up until the maths works; I can do\geight
and it'll display8+1
for example.\par
s. You certainly wouldn't normally put\frac{}{}
inside\pgfmathparse
like that. Either you want it to evaluate the fraction and you give it the fraction or you want it to evaluate the numerator and denominator separately and you need to parse each expression before printing the combined fraction.\frac
, but even if I replace that with(\feight) / (\geight)
I still get the same error so the question unfortunately still stands. I'll update the question now though. I would totally be willing to modify the structure of this document in any way so that the functions are applied and divided; is there a better way to do it?