Skip to main content

Section 21.1 SageMathCells

Including computational SageMath cells is pretty easy with <sage>, <input>, and <output>. The last tag is useful for producing formats intended for offline reading so that they can include the result of the code’s execution.
Listing 21.1.1. SageMath cell
<!--********************************************************************
Copyright (C) 2019-2026  Robert A. Beezer

This file is part of PreTeXt.

PreTeXt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 or version 3 of the
License (at your option).

PreTeXt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PreTeXt.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************-->

<sage>
<input>
2+2
</input>
<output>
4
</output>
</sage>
The code in Listing 21.1.1 produces the following output:
SageMathCells on a single HTML page are automatically linked so that a cell can use the results of computations done in earlier cells on the same page.