Franklin.jl
Franklin.jl copied to clipboard
Parse math/latex in table input.
How do I make Franklin parse Latex or math equations when using the \tableinput{}{}
command?
Currently if I have a CSV file
Equation, Formula
Euler, $e^{ix} = \cos x + i \sin x$
Einstein, $e=mc^2$
it doesn't get parsed to math when input into a markdown file using \tableinput{}{}
command.
Hello @iabraham right, the \tableinput
does not do a "secondary" pass (i.e. after having parsed the CSV file it writes HTML assuming there's no more parsing to be done). We could change that, just no-one had brought a use cases before 😄
In your case we can just write a short function that does this for you, it'll be a bit different than \tableinput
but I'm hoping it'll also be more transparent so you can adjust it to your needs; if you put this in utils.jl
using DelimitedFiles
function hfun_mytableinput(params)
fpath = joinpath("_assets", params[1])
content = readdlm(fpath, ',')
io = IOBuffer()
write(io, """
<table>
<thead>
<tr>
""")
header = content[1, :]
for col in header
write(io, """
<th>$col</th>
""")
end
write(io, "</thead><tbody>")
for rowidx in 2:size(content, 1)
row = content[rowidx, :]
write(io, "<tr>")
for col in row
write(io, "<td>$(fd2html(col; internal=true, nop=true))</td>")
end
write(io, "</tr>")
end
write(io, """
</tbody>
</table>
""")
return String(take!(io))
end
What it will do is it will read a CSV file assumed to be in _assets/
and write a table having processed each entry as if it was markdown (that's the internal fd2html
call). It also assumes the first row is the header, I let you adjust that if you see the need.
So as an example if you put the exact content you showed in a file _assets/example.csv
and have an index.md
like:
+++
hasmath = true
+++
# Hello
{{mytableinput example.csv}}
The result will be:

Note: you have to explicitly specify that hasmath = true
here as there's no "actual" equations on the page so that Franklin wouldn't know it has to include KaTeX on the page.
@tlienart Perfect, that does the job (note: the above would with thead
and th
ended up designating first two rows as header so I had to modify the HTML) but it works.
Thanks for this.
Could you point me to how to make Franklin look for the CSV file in the appropriate folder based on where the index.md
file is written? For example if the index.md
file is in: _assets/<subfolder>/<page1>/index.md
then the \input
command knows to look in _assets/<subfolder>/<page1>/
and then appropriately code
and output
folders.
Is there someway for me to make hfun
s work in a similar fashion?
In the hfun you can use locvar(:fd_rpath)
(sorry it's obscure but actually in the docs under page variables) and it will give you the relative path to the current file (e.g foo/bar/index.md
, so the relative path to the file that calls the hfun)
Note that if you place .md files in the _assets
folder they will not be processed (that folder is just taken as a "copy this content in the appropriate location doing nothing else with it")
Oh right ... my bad. I had mean to say that if index.md
is in /<category>/<page1>/index.md
then Franklin knows to look for files in /_assets/<category>/<page1>/
when using the \input
command. I will take a look at using locvar(:fd_path)
. Thanks!