TexSurgery¶
Replaces some commands and environments within a TeX document by evaluating code inside a jupyter kernel, and more…
Insert code in any language with a jupyter kernel.
Eval some code silently inside the kernel.
Insert the output into the LaTeX document.
TexSurgery
saves the images generated by the jupyter kernels in theimages
directory and embeds the corresponding LaTeX code to embed them.TexSurgery
also allows for other kinds of manipulation of a LaTeX source file, like find/replace/shuffle using CSS-style selectors.TexSurgery
works also as a shell command, that can be piped to other commands.
Similar to sagetex or pythontex, but we decided ot create texsurgery
to climb on the shoulders of the jupyter
project and to bring to LaTeX some powerful commands inspired by CSS.
Example¶
\begin{runsilent}
from sympy import sin, symbols, diff
x = symbols('x')
f = sin(x**2)
\end{runsilent}
The derivative is $\eval{f.diff(x)}$.
-> texsurgery input.tex ->
The derivative is $\displaystyle 2 x \cos{\left(x^{2} \right)}$.
Longer 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:
% 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}}
\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}
Thanks¶
To all our colleagues that gave feedback to the early versions, specially Fabricio from ETSIN.UPM and Carlos from ETSIAAB.UPM