0

Please, help solve this issue.

The python code have the objective of calculus the derivative of an expression f(x).

If I uncomment the line

%\addplot [domain=0:10, samples=100, color=blue ]{\pyc{calc_derivad(func="x^2 + x")}};

I've got an error.

But the line

\node[draw] at (2,40) {\pyc{calc_derivad(func="x^2 + x")}};

doesn't

\documentclass[tikz]{standalone}

\usepackage{tikz}   % https://www.ctan.org/pkg/pgf
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage[gobble=auto]{pythontex} % https://ctan.org/pkg/pythontex

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$f(x)$},
]
\addplot [domain=0:10, samples=100, color=blue ]{x^2+x};
\addlegendentry{$x^2 + x$}
\node[draw] at (2,40) {\pyc{calc_derivad(func="x^2 + x")}};
%\addplot [domain=0:10, samples=100, color=blue ]{\pyc{calc_derivad(func="x^2 + x")}};
\end{axis}
\end{tikzpicture}

\begin{pythontexcustomcode}{py}
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sympy import *

x = symbols('x')

def calc_derivad(func):
    func = sympify(func)
    result = diff(func, x)
    print(result, end = '')
    return 0
\end{pythontexcustomcode}

\end{document}
  • I do not have pythontex but I would try \edef\temp{\noexpand\addplot [domain=0:10, samples=100, color=blue ]{\pyc{calc_derivad(func="x^2 + x")}};}\temp. – Schrödinger's cat Sep 9 at 21:13
  • @Schrödinger'scat, thank! (./pythontex-files-teste02/teste02.pytxmcr) Package newfloat Info: New float listing' with options fileext=lopytx' on inp ut line 8. \c@listing=\count153 \newfloat@ftype=\count154 Package newfloat Info: float type `listing'=8 on input line 8. (./pythontex-files-teste02/teste02.pytxpyg) ! Undefined control sequence. \pyc ...epythontex {cmd:pyc:ov:p}\xdef \pytx@type {py}\edef \pytx@cmd {c}\py... l.20 ...\pyc{calc_derivad(func="x^2 + x")}};}\temp ? X – Luis Ferreira Sep 9 at 21:26
  • Using \pyc within tikzpicture inside something like \node or \draw usually doesn't work. See tex.stackexchange.com/a/65294/10742 for some alternatives. – G. Poore Sep 10 at 10:06

Your Answer

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

Browse other questions tagged or ask your own question.