0

I want to "plot" a simple line function, like f(x) = 3x+5. Actually, this is perfectly working:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
        \plot{3*x+5};
    \end{axis}
\end{tikzpicture}
\end{document}

But, for line function, I wanted to be able to create a custom command that would take the function as a parameter and evaluate two points on this line. Something like this:

\NewDocumentCommand\myline{m}{
    \draw (-5;\fx{#1}(-5)>) -- (5; \fx{#1}(7)>);
}
\NewDocumentCommand\fx{m r()}{
% this is where I'm missing what to do:
% #1 is the function: #1 = 3*x+5
% #2 is the value to use: x = #2
}

where

  • <#1(-5)> should parse and evaluate the line function at x=-5
  • <#1(7)> should parse and evaluate the line function at x=7

Notice that I want to keep the "plot" style function: 3*x+5 and I don't want to have to add a backslash to the x like this: 3*\x+5

I tried to use various pgfmath functions, but I must be missing something to make it work :(

Thanks for your help !

3
  • Take a look at this recent post: tex.stackexchange.com/questions/614848/…. Both answers use TikZ declared functions. Commented Sep 10, 2021 at 12:25
  • @JuanCastaño I'm aware of the declare function, but then, you need to write the function using \x, which I don't want. What I really would like to do is to declare a function that would allow me to simply write fx{3*x-5}(7) and would return 16.
    – Basil Gass
    Commented Sep 10, 2021 at 13:50
  • Like: replace x with 7, then integer evaluate? (using expl3)
    – Cicada
    Commented Sep 10, 2021 at 14:40

1 Answer 1

2

The following is a fully expandable function that allows you to evaluate a function using just x. Caveat: Every x is replaced with the value, even if you don't want it to be, so you couldn't use the exp() function of l3fp anymore with this. You can change the variable name which is replaced by using the optional argument, so \fx[x_1]{exp(x_1/5)}{2} would work.

\documentclass{article}

\usepackage{etl}

\ExplSyntaxOn
\NewExpandableDocumentCommand \fx { O{x} m m }
  {
    \fp_eval:n { \etl_replace_all_deep:nnn {#2} {#1} {#3} }
  }
\ExplSyntaxOff

\begin{document}
\fx{3*x+5}{-5} and \fx{3*x+5}{7}
\end{document}
2
  • Thanks a lot! That's perfect ! I just changed the function to: \NewExpandableDocumentCommand \fx { O{x} m r() } so I can write the function more naturally: \fx{3*x+5}(-5)
    – Basil Gass
    Commented Sep 12, 2021 at 9:21
  • @BasilGass this might be more natural to you, and xparse/ltcmd has no issues with it, but please bear in mind that you should follow standard LaTeX rules for argument types where possible. Those are: Required arguments should be in braces. The required arguments in parenthesis are there to support old stuff in LaTeX, like the coordinate arguments in picture mode. For this you should really use standard arguments (less so if it's your own file, more so if you share this with others). That's why I used m instead, even though your MWE showed r().
    – Skillmon
    Commented Sep 12, 2021 at 11:13

You must log in to answer this question.

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