sisl / tufte_algorithms_book

A template for textbooks in the same style as Algorithms for Optimization
350 stars 66 forks source link

workflow for including code #28

Open jlapeyre opened 3 years ago

jlapeyre commented 3 years ago

What is a good method for displaying code in a document and then using it in a juliaconsole block? The following seems to work, but I suspect there is a simpler way.

\begin{example}
\begin{juliablock}
some code
\end{juliablock}
\caption{example code}
\end{example}

Then I copy the code in the juliablock by hand into a file called example_code.jl. Then I do this:

\begin{juliaconsole}
include("example_code.jl"); nothing
... use code...
\end{juliaconsole}
tawheeler commented 3 years ago

In our book we have a custom script that takes any code in a juliaverbatim block and puts it into a Julia package source file. We add a jlcode block that loads this package:

\begin{jlcode}
using OurPackage
\end{jlcode}

You could do something similar. The pythontex docs state that the pyconcode will execute but typeset nothing, so maybe jlconcode could be used to load the package prior to your juliaconsole block:

\begin{jlconcode}
using YourPackage
\end{jlconcode}
\begin{juliaconsole}
... use code ...
\end{juliaconsole}

There is an optional session id parameter as well. I think it defaults to a single session, but it may need to be specified.

mykelk commented 3 years ago

I think @mossr has some experience with this.