Replace some commands and environments within a TeX document by evaluating code inside a jupyter kernel
Project description
TexSurgery
Replaces some commands and environments within a TeX document by evaluating code inside a jupyter kernel.
Much like sagetex, but with the following differences:
sagetexcollects all the code using LaTeX and only then runssageto get the LaTeX output, which definitely works, but this conflicts with some interesting LaTeX packages and is slower than a direct conversion.TexSurgeryworks in any language with a jupyter kernel
Installation
python3 -m pip install texsurgery
Testing
The following command will perform some common tests, and specific tests for some of the kernels that are installed:
python3 -m unittest tests
Selectors
New in version 0.1, texsurgery can also gather information using a limited choice of css-style selectors:
>>> from texsurgery.texsurgery import TexSurgery
>>> tex = open('tests/test_find.tex').read()
>>> TexSurgery(tex).findall('question,questionmultx runsilent')
[('questionmultx', [('runsilent', 'a = randint(1,10)\n')]),
('question',
[('runsilent', 'a = randint(2,10)\nf = sin(a*x)\nfd = f.derivative(x)\n')])]
>>> TexSurgery(tex).findall('question,questionmultx choices \correctchoice')
[('question', [('choices', [('\correctchoice', '$\sage{fd}$')])])]
>>> TexSurgery(tex).findall('questionmultx \AMCnumericChoices[_nargs=2]')
[('questionmultx', [('\\AMCnumericChoices',
['\\eval{8+a}', 'digits=2,sign=false,scoreexact=3'])]
)]
Example
Start with this LaTeX code:
% Any jupyter kernel is available
\usepackage[sagemath]{texsurgery}
% Compatible with any other LaTeX package
\usepackage[bloc,completemulti]{automultiplechoice}
% Example of user macros
\providecommand{\abs}[1]{\lvert#1\rvert}
\newcommand{\R}{\mathbb{R}}
% TexSurgery can replace some \commands before pdflatex runs
\begin{minipage}{.85\linewidth}
Student: {\bf \name \; \surname}, \quad ID: {\bf \id}
\end{minipage}
\begin{question}{derivative-sin}
\qvariant{1} \qtags{derivative}
% TexSurgery will run code in a jupyter kernel
\begin{runsilent}
set_random_seed(\seed)
a = randint(2,10)
f = sin(a*x)
fd = f.derivative(x)
\end{runsilent}
% TexSurgery will eval code in a jupyter kernel
% and replace \eval{expr} with the output from the kernel
% \sage{expr} is just an alias for \eval{latex(expr)}
What is the first derivative of $\sage{f}$?
\begin{choices}
\correctchoice{$\sage{fd}$}
\wrongchoice{$\sage{fd*a}$}
\wrongchoice{$\sage{fd + a}$}
\end{choices}
\begin{explain}
\begin{run}
# TexSurgery will run code in the jupyter kernel
# and replace this environment with the full output
\end{run}
\end{explain}
\end{question}
and run this python code:
from texsurgery.texsurgery import TexSurgery
student_vars = dict(name='Fulano', surname='de Tal', seed='1', id='314159')
ts = TexSurgery(tex_source).data_surgery(student_vars).code_surgery()
in order to transform it into this:
% Compatible with any other LaTeX package
\usepackage[bloc,completemulti]{automultiplechoice}
% Example of user macros
\providecommand{\abs}[1]{\lvert#1\rvert}
\newcommand{\R}{\mathbb{R}}
\begin{minipage}{.85\linewidth}
Student: {\bf Fulano \; de Tal}, \quad ID: {\bf 314159}
\end{minipage}
\begin{question}{derivative-sin}
\qvariant{1} \qtags{derivative}
What is the first derivative of $\sin\left(7 \, x\right)$?
\begin{choices}
\correctchoice{$7 \, \cos\left(7 \, x\right)$}
\wrongchoice{$49 \, \cos\left(7 \, x\right)$}
\wrongchoice{$7 \, \cos\left(7 \, x\right) + 7$}
\end{choices}
\begin{explain}
\begin{run}
# TexSurgery will run code in the jupyter kernel
# and replace this environment with the full output
\end{run}
\end{explain}
\end{question}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file texsurgery-0.3.0.tar.gz.
File metadata
- Download URL: texsurgery-0.3.0.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66b8ba13e3a8576426eafb27ce8a15bd8be6540f3a37e3dd1f3b7d44aecb083f
|
|
| MD5 |
7b51b50936d915fe0b05655dc95c2fa7
|
|
| BLAKE2b-256 |
1329bbe9322c9749a3ecdb109390724d412d014ba6ce2a35c11834f8fe8e81f7
|
File details
Details for the file texsurgery-0.3.0-py3-none-any.whl.
File metadata
- Download URL: texsurgery-0.3.0-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db8568773039e5f51859aec2bf901c6fd825ec7506d360dca5fb5c086568aafe
|
|
| MD5 |
157a91d43ce21a59431e68e58daa7632
|
|
| BLAKE2b-256 |
874e704767173a9991d222a8e1a48f5890acddca932f6951d4e262d44880e176
|