JuliaBooks / Books.jl

Create books with Julia
https://huijzer.xyz/Books.jl/
Other
270 stars 18 forks source link

Easy way to interpolate a value in the text with :compact = true enabled? #271

Closed dmbates closed 2 years ago

dmbates commented 2 years ago

I would like to be able to interpolate the value of a variable into the text using the backtick jl construction but with the compact display for floating point values.
The display defaults to the non-compact representation which produces more digits than I want. Is there a relatively simple way of setting :compact = true on the IO stream where these values are interpolated?

rikhuijzer commented 2 years ago

Is there a relatively simple way of setting :compact = true on the IO stream where these values are interpolated?

I'm not so sure. It would be possible by overriding the appropriate Books.convert_output method.

Another solution, without overriding Books.convert_output, would be to define something like

r1(x) = round(parse(Float64, x); digits=1)
export r1

in the main module of the book, and to call this function via the post keyword argument:

```jl
s = "1 + 1.234"
sco(s; post=output_block ∘ r1)
```

Which shows as

image

in the HTML output.

dmbates commented 2 years ago

Thanks for the suggestions.