Skip to content

Instantly share code, notes, and snippets.

@hyoiutu
Created December 16, 2017 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyoiutu/3b36ffc3a1ddd88858ef243720c169b1 to your computer and use it in GitHub Desktop.
Save hyoiutu/3b36ffc3a1ddd88858ef243720c169b1 to your computer and use it in GitHub Desktop.
JupyTeX
\RequirePackage[makestderr]{pythontex}
\RequirePackage[many]{tcolorbox}
\tcbuselibrary{breakable}
\RequirePackage{color, xcolor}
\RequirePackage{xparse}
\newtcolorbox[]{outframebox}{enhanced, fuzzy halo=1.8pt with black!30, breakable=true,
colback=white,
boxrule=0.1pt,
top=0mm,
bottom=0mm,
right=0mm,
left=0mm,arc=0pt,boxsep=10mm,}
\newtcolorbox[auto counter]{inputframebox}[1][]{enhanced,tcbox raise base,breakable=true,
frame hidden,
top=0mm,
bottom=0mm,
right=0mm,
left=10mm,arc=1pt,boxsep=10mm,
overlay={\begin{tcbclipinterior}
\fill[white] ([xshift=10mm]frame.south west) rectangle node[text=blue,] {In[\thetcbcounter]:} (frame.north west);\end{tcbclipinterior}}}
\newtcolorbox[]{outputframebox}{
enhanced,enforce breakable=true,shrink break goal=15mm,
colback=white,
frame hidden,
top=0mm,
bottom=0mm,
right=0mm,
left=10mm,
boxsep=0mm,}
\begin{pycode}
import subprocess
def run_and_typeset(fname):
print(subprocess.check_output(['python', fname]).decode("utf-8"))
pytex.add_dependencies(fname)
\end{pycode}
\edef\runandtypeset#1{\pyc{run_and_typeset("#1")}}
\newcounter{pyfilenumber}
\setcounter{pyfilenumber}{0}
\NewDocumentEnvironment{jupytex}{}
{\edef\currentfile{jupytex\arabic{pyfilenumber}.py}\VerbatimOut{\currentfile}}
{\endVerbatimOut\edef\currentfile{jupytex\arabic{pyfilenumber}.py}
\begin{inputframebox}\inputpygments{python}{\currentfile}\end{inputframebox}
\begin{outputframebox}\expandafter\runandtypeset\expandafter{\currentfile}\end{outputframebox}\refstepcounter{pyfilenumber}}
@kamae
Copy link

kamae commented Dec 18, 2017

Can you add a sample or a simple tutorial showing how a latex math formula can be converted to a Jupyter python code window?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment