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.Wed, 21 Mar 2018 21:01:53 +0100Code highlighting and execution in sagetexhttps://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/Is there a version of
```
\begin{sageblock}
my multiline code
\end{sageblock}```
that has code highlighting and line numbering like the `sagecommandline` environment **and** code execution?
Of course typesetting with `lstlistings` and execution with `sagesilent` could be a workaround, but it would seem to require me to have the same code in my document twice. And that I find unacceptable.Tue, 05 Sep 2017 14:44:22 +0200https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/Comment by slelievre for <p>Is there a version of
<code>
\begin{sageblock}
my multiline code
\end{sageblock}</code>
that has code highlighting and line numbering like the <code>sagecommandline</code> environment <strong>and</strong> code execution? </p>
<p>Of course typesetting with <code>lstlistings</code> and execution with <code>sagesilent</code> could be a workaround, but it would seem to require me to have the same code in my document twice. And that I find unacceptable.</p>
https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41685#post-id-41685Recap of the various SageTeX commands and environments:
- `\sage{code}`
- code is executed
- code is not displayed
- output is inserted to be displayed by LaTeX
- `sageblock`
- code is executed
- code is displayed verbatim, no syntax highlighting, no line numbers
- output is not displayed
- `sagesilent`
- code is executed
- code is not displayed
- output is not displayed
- `sagecommandline`
- code lines with sage prompt are executed
- code is displayed verbatim with syntax highlighting
- output is inserted in the displayed code block
- lines with no sage prompt are ignored when typesetting
- lines with no sage prompt are useful for doctesting
- line numbering is on by default, can be disabled
- inserting of the output is on by default, can be disabledWed, 21 Mar 2018 15:51:58 +0100https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41685#post-id-41685Comment by slelievre for <p>Is there a version of
<code>
\begin{sageblock}
my multiline code
\end{sageblock}</code>
that has code highlighting and line numbering like the <code>sagecommandline</code> environment <strong>and</strong> code execution? </p>
<p>Of course typesetting with <code>lstlistings</code> and execution with <code>sagesilent</code> could be a workaround, but it would seem to require me to have the same code in my document twice. And that I find unacceptable.</p>
https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41686#post-id-41686With the straighforward `lstlistings`, you would get:
- code is displayed verbatim, optionally with syntax highlighting and line numbering
- code is not executedWed, 21 Mar 2018 15:53:03 +0100https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41686#post-id-41686Answer by slelievre for <p>Is there a version of
<code>
\begin{sageblock}
my multiline code
\end{sageblock}</code>
that has code highlighting and line numbering like the <code>sagecommandline</code> environment <strong>and</strong> code execution? </p>
<p>Of course typesetting with <code>lstlistings</code> and execution with <code>sagesilent</code> could be a workaround, but it would seem to require me to have the same code in my document twice. And that I find unacceptable.</p>
https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?answer=38743#post-id-38743Is `sagecommandline` what you are looking for?
I found it by searching the sagetex github repo for "highlighting":
- https://github.com/sagemath/sagetex/search?q=highlighting
Try this (from the documentation).
\begin{sagecommandline}
sage: 1+1
2
sage: factor(x^2 + 2*x + 1)
(x + 1)^2
\end{sagecommandline}
For function definitions or loops, use the continuation prompt `....: ` as follows:
\begin{sagecommandline}
sage: def square(x):
....: return x * x
....:
sage: square(12)
144
sage: first_few_squares = []
sage: for n in range(4):
....: first_few_squares.append(square(n))
....:
sage: first_few_squares
[0, 1, 4, 9]
\end{sagecommandline}
To get rid of the output, change the value of `sagecommandlinetextoutput`:
\renewcommand{\sagecommandlinetextoutput}{False}
The documentation of SageTeX and its example file have a lot of information.
One comment in the example file is:
> The Sage input and output is typeset using the `listings` package with the styles
> `SageInput` and `SageOutput`, respectively. If you don't like the defaults
> you can change them.
and there is a recommendation on how to change the defaults if you want to.
Another wonderful feature (which justifies including the `sage: ` prompt) is
> You can include output, but it will be ignored. This is useful for doctesting, as all
> the `sagecommandline` environment things get put into the `_doctest.sage` file.
The SageTeX documentation and example file are worth reading and studying!
If you have Sage installed and if `/path/to/sagedir` is the directory
where it is installed, open a terminal and run the following:
SAGE_ROOT='/path/to/sagedir'
cd $SAGE_ROOT/local/share/texmf/tex/latex/sagetex
export TEXINPUTS="$SAGE_ROOT/local/share/texmf//:"
pdflatex example.tex
sage example.sagetex.sage
pdflatex example.tex
open example.pdfWed, 06 Sep 2017 03:54:41 +0200https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?answer=38743#post-id-38743Comment by Björn for <p>Is <code>sagecommandline</code> what you are looking for?</p>
<p>I found it by searching the sagetex github repo for "highlighting":</p>
<ul>
<li><a href="https://github.com/sagemath/sagetex/search?q=highlighting">https://github.com/sagemath/sagetex/s...</a></li>
</ul>
<p>Try this (from the documentation).</p>
<pre><code>\begin{sagecommandline}
sage: 1+1
2
sage: factor(x^2 + 2*x + 1)
(x + 1)^2
\end{sagecommandline}
</code></pre>
<p>For function definitions or loops, use the continuation prompt <code>....:</code> as follows:</p>
<pre><code>\begin{sagecommandline}
sage: def square(x):
....: return x * x
....:
sage: square(12)
144
sage: first_few_squares = []
sage: for n in range(4):
....: first_few_squares.append(square(n))
....:
sage: first_few_squares
[0, 1, 4, 9]
\end{sagecommandline}
</code></pre>
<p>To get rid of the output, change the value of <code>sagecommandlinetextoutput</code>:</p>
<pre><code>\renewcommand{\sagecommandlinetextoutput}{False}
</code></pre>
<p>The documentation of SageTeX and its example file have a lot of information.</p>
<p>One comment in the example file is:</p>
<blockquote>
<p>The Sage input and output is typeset using the <code>listings</code> package with the styles
<code>SageInput</code> and <code>SageOutput</code>, respectively. If you don't like the defaults
you can change them.</p>
</blockquote>
<p>and there is a recommendation on how to change the defaults if you want to.</p>
<p>Another wonderful feature (which justifies including the <code>sage:</code> prompt) is</p>
<blockquote>
<p>You can include output, but it will be ignored. This is useful for doctesting, as all
the <code>sagecommandline</code> environment things get put into the <code>_doctest.sage</code> file.</p>
</blockquote>
<p>The SageTeX documentation and example file are worth reading and studying!</p>
<p>If you have Sage installed and if <code>/path/to/sagedir</code> is the directory
where it is installed, open a terminal and run the following:</p>
<pre><code>SAGE_ROOT='/path/to/sagedir'
cd $SAGE_ROOT/local/share/texmf/tex/latex/sagetex
export TEXINPUTS="$SAGE_ROOT/local/share/texmf//:"
pdflatex example.tex
sage example.sagetex.sage
pdflatex example.tex
open example.pdf
</code></pre>
https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41667#post-id-41667It is a partial workaround, but does not solve the original question, really. The `sage:` is a bit in the way, and this doesn't seem to support block code like function definitions or loops. Sorry for not noticing your answer earlier, I haven't logged in in a while.Wed, 21 Mar 2018 08:39:50 +0100https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41667#post-id-41667Comment by slelievre for <p>Is <code>sagecommandline</code> what you are looking for?</p>
<p>I found it by searching the sagetex github repo for "highlighting":</p>
<ul>
<li><a href="https://github.com/sagemath/sagetex/search?q=highlighting">https://github.com/sagemath/sagetex/s...</a></li>
</ul>
<p>Try this (from the documentation).</p>
<pre><code>\begin{sagecommandline}
sage: 1+1
2
sage: factor(x^2 + 2*x + 1)
(x + 1)^2
\end{sagecommandline}
</code></pre>
<p>For function definitions or loops, use the continuation prompt <code>....:</code> as follows:</p>
<pre><code>\begin{sagecommandline}
sage: def square(x):
....: return x * x
....:
sage: square(12)
144
sage: first_few_squares = []
sage: for n in range(4):
....: first_few_squares.append(square(n))
....:
sage: first_few_squares
[0, 1, 4, 9]
\end{sagecommandline}
</code></pre>
<p>To get rid of the output, change the value of <code>sagecommandlinetextoutput</code>:</p>
<pre><code>\renewcommand{\sagecommandlinetextoutput}{False}
</code></pre>
<p>The documentation of SageTeX and its example file have a lot of information.</p>
<p>One comment in the example file is:</p>
<blockquote>
<p>The Sage input and output is typeset using the <code>listings</code> package with the styles
<code>SageInput</code> and <code>SageOutput</code>, respectively. If you don't like the defaults
you can change them.</p>
</blockquote>
<p>and there is a recommendation on how to change the defaults if you want to.</p>
<p>Another wonderful feature (which justifies including the <code>sage:</code> prompt) is</p>
<blockquote>
<p>You can include output, but it will be ignored. This is useful for doctesting, as all
the <code>sagecommandline</code> environment things get put into the <code>_doctest.sage</code> file.</p>
</blockquote>
<p>The SageTeX documentation and example file are worth reading and studying!</p>
<p>If you have Sage installed and if <code>/path/to/sagedir</code> is the directory
where it is installed, open a terminal and run the following:</p>
<pre><code>SAGE_ROOT='/path/to/sagedir'
cd $SAGE_ROOT/local/share/texmf/tex/latex/sagetex
export TEXINPUTS="$SAGE_ROOT/local/share/texmf//:"
pdflatex example.tex
sage example.sagetex.sage
pdflatex example.tex
open example.pdf
</code></pre>
https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41672#post-id-41672I edited my answer to address your comment.Wed, 21 Mar 2018 13:11:50 +0100https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41672#post-id-41672Comment by Emmanuel Charpentier for <p>Is <code>sagecommandline</code> what you are looking for?</p>
<p>I found it by searching the sagetex github repo for "highlighting":</p>
<ul>
<li><a href="https://github.com/sagemath/sagetex/search?q=highlighting">https://github.com/sagemath/sagetex/s...</a></li>
</ul>
<p>Try this (from the documentation).</p>
<pre><code>\begin{sagecommandline}
sage: 1+1
2
sage: factor(x^2 + 2*x + 1)
(x + 1)^2
\end{sagecommandline}
</code></pre>
<p>For function definitions or loops, use the continuation prompt <code>....:</code> as follows:</p>
<pre><code>\begin{sagecommandline}
sage: def square(x):
....: return x * x
....:
sage: square(12)
144
sage: first_few_squares = []
sage: for n in range(4):
....: first_few_squares.append(square(n))
....:
sage: first_few_squares
[0, 1, 4, 9]
\end{sagecommandline}
</code></pre>
<p>To get rid of the output, change the value of <code>sagecommandlinetextoutput</code>:</p>
<pre><code>\renewcommand{\sagecommandlinetextoutput}{False}
</code></pre>
<p>The documentation of SageTeX and its example file have a lot of information.</p>
<p>One comment in the example file is:</p>
<blockquote>
<p>The Sage input and output is typeset using the <code>listings</code> package with the styles
<code>SageInput</code> and <code>SageOutput</code>, respectively. If you don't like the defaults
you can change them.</p>
</blockquote>
<p>and there is a recommendation on how to change the defaults if you want to.</p>
<p>Another wonderful feature (which justifies including the <code>sage:</code> prompt) is</p>
<blockquote>
<p>You can include output, but it will be ignored. This is useful for doctesting, as all
the <code>sagecommandline</code> environment things get put into the <code>_doctest.sage</code> file.</p>
</blockquote>
<p>The SageTeX documentation and example file are worth reading and studying!</p>
<p>If you have Sage installed and if <code>/path/to/sagedir</code> is the directory
where it is installed, open a terminal and run the following:</p>
<pre><code>SAGE_ROOT='/path/to/sagedir'
cd $SAGE_ROOT/local/share/texmf/tex/latex/sagetex
export TEXINPUTS="$SAGE_ROOT/local/share/texmf//:"
pdflatex example.tex
sage example.sagetex.sage
pdflatex example.tex
open example.pdf
</code></pre>
https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41700#post-id-41700I had a similar problem, wore which I proposed a [workaround](https://ask.sagemath.org/question/36422/sagetex-how-to-capture-sageblock-output-for-future-use/), thar you my find useful.Wed, 21 Mar 2018 21:01:53 +0100https://ask.sagemath.org/question/38742/code-highlighting-and-execution-in-sagetex/?comment=41700#post-id-41700