5

Recently I have discovered the interesting package named luacas (compilable with LuaLaTeX). To pag. 14 of the manual

https://ctan.mirror.garr.it/mirrors/ctan/macros/luatex/latex/luacas/doc/luacas.pdf

I have not understood the necessary packages to put in the preamble to compile the MWE to plot f and f'.

Someone please, can help me?

Here the fragment of the code:

\directlua{
  tex.print(h:tolatex())
}
For Bob’s purposes, \fetch{h} is exactly what he needs:

\begin{tikzpicture}[scale=0.9]
  \begin{axis}[legend pos = north west]
    \addplot [domain=-3.5:1.5,samples=100]
      {\fetch{h}};
    \addlegendentry{$f$};
    \addplot[densely dashed]
      [domain=-3.25:1.25,samples=100]
      {\fetch{dh}};
    \addlegendentry{$df/dx$};
    \addplot[gray,dashed,thick]
      [domain=-3.5:1.5] {0};
  \end{axis}
\end{tikzpicture}

Alternatively, Bob could use \store. The \store command will fetch the contents of its mandatory argument and store it in a macro of the same name.

\store{h}
\store{dh}

2 Answers 2

3

As far as I understand, in the preamble it is sufficient to load only

\usepackage{luacas}

For your plotting fragment, you also need the pgfplots package. Then everything works. Here is a minimal example.

\documentclass{article}
\usepackage{luacas}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{CAS}
vars('x')
f = x^2+2*x-2
g = x^2-1
subs = {[x] = f}
dh = expand(substitute(subs,g))
h = simplify(int(dh,x)+10)
\end{CAS}

For Bob’s purposes, $\fetch{h}$ is exactly what he needs:

\medskip
\begin{tikzpicture}[scale=0.9]
  \begin{axis}[legend pos = north west]
    \addplot [domain=-3.5:1.5,samples=100]
      {\fetch{h}};
    \addlegendentry{$f$};
    \addplot[densely dashed]
      [domain=-3.25:1.25,samples=100]
      {\fetch{dh}};
    \addlegendentry{$df/dx$};
    \addplot[gray,dashed,thick]
      [domain=-3.5:1.5] {0};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

1
  • Thank you very very much. Commented 9 hours ago
2
  • you need to load luacas and define the functions you'd like to use

  • load pgfplots for plotting

  • compile with lualatex

A minimal compilable example:

% !TeX TS-program = lualatex -interaction=nonstopmode % | txs:///view-pdf | txs:///view-log

\documentclass{article}

\usepackage{luacas}
\usepackage{pgfplots}

\begin{document}

\begin{CAS}
vars('x')
h=x^2
\end{CAS}

\begin{tikzpicture}
  \begin{axis}[legend pos = north west]
    \addplot [domain=-3.5:1.5,samples=100] {\fetch{h}};
    \addlegendentry{$f$};
  \end{axis}
\end{tikzpicture}
    
\end{document}

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.