Ask Your Question
1

Why can't I use the sagesilent environment in AtBeginDocument?

asked 2019-01-15 04:47:30 +0100

done_with_fish gravatar image

updated 2019-02-23 23:07:03 +0100

I'm trying to write a latex sty file to change the default behavior of sagetex.

My file is

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

\RequirePackage{sagetex}
\RequirePackage{etoolbox}

\AtBeginDocument{%
  \begin{sagesilent}
    A = 3
  \end{sagesilent}
}

\endinput

However, attempting to use this sty file in the following latex file produces the error ! File ended while scanning use of \next.

\documentclass[12pt]{article}

\usepackage{examplepackage}

\begin{document}
hello world!
\end{document}

What's wrong with my sty file?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2019-02-20 01:30:01 +0100

Juanjo gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-01-15 04:47:30 +0100

Seen: 671 times

Last updated: Feb 23 '19