Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The internal definition of the sagesilent environment involves the use of verbatim text, which never can be written in a macro argument. The same kind of problem arises if you try something like

\AtBeginDocument{
\begin{verbatim}
   Some verbatim text
\end{verbatim}
}

As a workaround, you can move to an auxiliary tex file all the code needed to config SageTeX. Then you can input that file in the argument of \AtBeginDocument. For example, let the contents of examplepackage.sty be

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{examplepackage}[2018/12/24 examplepackage]

\RequirePackage{sagetex}

\AtBeginDocument{\input{exampleconfig}}

\endinput

The file exampleconfig.tex contains the code to set up SageTeX:

\begin{sagesilent}
   # Set up some variables
   a = 3
   b = 5
   # Define a function
   var("x")
   g(x) = a*sin(x)+b*cos(x)
\end{sagesilent}

\endinput

The main tex file could be

\documentclass[12pt]{article}

\usepackage{examplepackage}

\begin{document}

% Do something with a and b

The value of $a$ is $\sage{a}$, while that of $b$
is $\sage{b}$. Their sum is $\sage{a+b}$.

% Do something with the function

\begin{sagesilent}
   gp(x) = diff(g(x),x)
   g_int(x) = integrate(g(x), x)
\end{sagesilent}

Let $g(x)=\sage{g(x)}$. It is easily seen that
\[
   g'(x)=\sage{gp(x)},
\]
whereas
\[
   \int g(x)\,dx=\sage{g_int(x)}.
\]

Observe also that $g'(2)\approx\sage{gp(2).n()}$.

\end{document}

Hope that helps.