sisl / tufte_algorithms_book

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

Cannot use functions in REPL (juliaconsole environment) defined in juliaverbatim environment. #33

Open JamieMair opened 2 years ago

JamieMair commented 2 years ago

I have an algorithm defined as follows:

\begin{algorithm}
\begin{juliaverbatim}
function my_large_sum()
    s = 0
    for i=1:100000000000
        s += i
    end
    return s
end
\end{juliaverbatim}
\end{algorithm}

This algorithm is rendered correctly and I can use it in tests, but not in the console environment:

\begin{juliaconsole}
import BenchmarkTools: @btime;
@btime my_large_sum()
\end{juliaconsole}

Doing this produces the following: image

How can I access these functions in the console space?

JamieMair commented 2 years ago

A temporary fix I have found is to add the following to the top of the book.tex file:

\begin{juliaconcode}
include("support_code.jl")
\end{juliaconcode}

This isn't a proper solution, since this is included in the book, which is not what I want. Hopefully hiding it from the book will be an easy fix. I have tried wrapping it in an \ifx statement, but this removes it from the latex file and doesn't call the code.

tawheeler commented 2 years ago

Hello JamieMair. The pythontex documentation states: "Notice that there is not a command or environment for console content that parallels the block command and environment. That is, there is not a command or environment that both typesets and executes code in the console, but does not show the output. This is intentional. In most cases, if you are going to use the console, you should use it consistently, showing input and output together."

Unfortunately, as you have seen, there are cases where we do actually want to use the console without showing input and output together. This currently seems to require a change to pythontex to enable that functionality.