ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 17 Mar 2018 10:49:52 +0100Sagetex : how to capture sageblock output for future use.https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/[ This is more, I think, a \LaTeX question than a Sagemath question. But since it's centered on the use of Sagetex, it is probably the right place to ask... ]
I'd like to be able to do some (boring but necessary) computation without printing it, use its results, *then*, in an appendix, show the computation (without re-running it).
The obvious solution would be to run the computation in a `sagesilent` block, use its results, then later insert a `sageverbatim` environment with the same code :
\begin{sagesilent}
## Boring computation
\end{sagesilent}
%% Lotsa discussion
\appendix
\begin{sageverbatim}
## Boring computation again
\end{sageverbatim}
The problem is that now, I have two copies of the same code, with no consistency guarantee (as illustrated in the example, BTW).
I tried :
\let\foo={\begin{sageblock}
## boring computation
\end{sageblock}}
% Lotsadiscussion
\appendix
\foo
But that doesn't work : The boring computation is still printed before the discussion and nothing is printed in the appendix.
I'm not really surprised : the \LaTeX `verbatim` environments are full of such traps. And, no, using `\protect` is not enough.
Any idea ?Wed, 01 Feb 2017 16:41:51 +0100https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/Answer by Emmanuel Charpentier for <p>[ This is more, I think, a \LaTeX question than a Sagemath question. But since it's centered on the use of Sagetex, it is probably the right place to ask... ]</p>
<p>I'd like to be able to do some (boring but necessary) computation without printing it, use its results, <em>then</em>, in an appendix, show the computation (without re-running it).</p>
<p>The obvious solution would be to run the computation in a <code>sagesilent</code> block, use its results, then later insert a <code>sageverbatim</code> environment with the same code :</p>
<pre><code>\begin{sagesilent}
## Boring computation
\end{sagesilent}
%% Lotsa discussion
\appendix
\begin{sageverbatim}
## Boring computation again
\end{sageverbatim}
</code></pre>
<p>The problem is that now, I have two copies of the same code, with no consistency guarantee (as illustrated in the example, BTW).
I tried :</p>
<pre><code>\let\foo={\begin{sageblock}
## boring computation
\end{sageblock}}
% Lotsadiscussion
\appendix
\foo
</code></pre>
<p>But that doesn't work : The boring computation is still printed before the discussion and nothing is printed in the appendix.
I'm not really surprised : the \LaTeX <code>verbatim</code> environments are full of such traps. And, no, using <code>\protect</code> is not enough.</p>
<p>Any idea ?</p>
https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?answer=41596#post-id-41596A possible workaround is to use the [filecontents](https://ctan.org/pkg/filecontents) LaTeX package in order to :
* define *in the main file* the contents of a sage file in the working directory
* use it in a `sagesilent` block where needed,
* cleanly typeset in the appendix via `lstinputlisting` or `minted`.
It is not a perfect solution (the typesetting parameters used by `minted` or `lstinputlisting` need to be tweaked to get something "close to" the result of `sageblock`, but the consistency and ease of use are obtained : there is a *single* source for both the paper and the associated computations.Sat, 17 Mar 2018 10:49:52 +0100https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?answer=41596#post-id-41596Answer by kcrisman for <p>[ This is more, I think, a \LaTeX question than a Sagemath question. But since it's centered on the use of Sagetex, it is probably the right place to ask... ]</p>
<p>I'd like to be able to do some (boring but necessary) computation without printing it, use its results, <em>then</em>, in an appendix, show the computation (without re-running it).</p>
<p>The obvious solution would be to run the computation in a <code>sagesilent</code> block, use its results, then later insert a <code>sageverbatim</code> environment with the same code :</p>
<pre><code>\begin{sagesilent}
## Boring computation
\end{sagesilent}
%% Lotsa discussion
\appendix
\begin{sageverbatim}
## Boring computation again
\end{sageverbatim}
</code></pre>
<p>The problem is that now, I have two copies of the same code, with no consistency guarantee (as illustrated in the example, BTW).
I tried :</p>
<pre><code>\let\foo={\begin{sageblock}
## boring computation
\end{sageblock}}
% Lotsadiscussion
\appendix
\foo
</code></pre>
<p>But that doesn't work : The boring computation is still printed before the discussion and nothing is printed in the appendix.
I'm not really surprised : the \LaTeX <code>verbatim</code> environments are full of such traps. And, no, using <code>\protect</code> is not enough.</p>
<p>Any idea ?</p>
https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?answer=36424#post-id-36424Hmm, I don't know much about verbatim or defining new variables. You are right about fragility, though, I get that kind of thing in beamer a lot. What I would do is this.
\begin{sagesilent}
## Boring computation
## final result x = my_result
\end{sagesilent}
%% Lotsa discussion
\appendix
The answer is $\sage{x}$.
Probably this should work?Wed, 01 Feb 2017 20:27:16 +0100https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?answer=36424#post-id-36424Comment by Emmanuel Charpentier for <p>Hmm, I don't know much about verbatim or defining new variables. You are right about fragility, though, I get that kind of thing in beamer a lot. What I would do is this.</p>
<pre><code>\begin{sagesilent}
## Boring computation
## final result x = my_result
\end{sagesilent}
%% Lotsa discussion
\appendix
The answer is $\sage{x}$.
</code></pre>
<p>Probably this should work?</p>
https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?comment=36436#post-id-36436This should work, yes. But it's not what I want to do.
The computation has to be available (for proof) but is in itself of little interest for readers. I want to be able to use its results in the main text, the add it to an appendix (think lemma enunciation vs demonstration, when the lemma is of weaker interest than the main theorem...).Thu, 02 Feb 2017 08:04:09 +0100https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?comment=36436#post-id-36436Comment by kcrisman for <p>Hmm, I don't know much about verbatim or defining new variables. You are right about fragility, though, I get that kind of thing in beamer a lot. What I would do is this.</p>
<pre><code>\begin{sagesilent}
## Boring computation
## final result x = my_result
\end{sagesilent}
%% Lotsa discussion
\appendix
The answer is $\sage{x}$.
</code></pre>
<p>Probably this should work?</p>
https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?comment=36445#post-id-36445I see what you mean now. Unsure as to solution, I don't use `sageverbatim` myself.Thu, 02 Feb 2017 22:26:03 +0100https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/?comment=36445#post-id-36445