executablebooks / sphinx-jupyterbook-latex

Supporting LaTeX infrastructure for Jupyter Book
https://sphinx-jupyterbook-latex.readthedocs.io
BSD 3-Clause "New" or "Revised" License
28 stars 13 forks source link

Allow code cell output to be read as raw text by latex processor #4

Open amichuda opened 3 years ago

amichuda commented 3 years ago

When printing raw latex code with python, such as:

from tabulate import tabulate

table = [["spam",42],["eggs",451],["bacon",0]]
headers = ["item", "qty"]
print(tabulate(table, headers, tablefmt="latex"))

Output:

\begin{tabular}{lr}
\hline
 item   &   qty \\
\hline
 spam   &    42 \\
 eggs   &   451 \\
 bacon  &     0 \\
\hline
\end{tabular}

The output is treated as a code block when running jb build --builder pdflatex, but Rmarkdown, for example, you can allow the results to be read as raw text so that the latex processor can actually render the table.

Is something like that possible?

AakashGfude commented 3 years ago

Aah, nice. thanks for testing this one out @amichuda . I will add this to my list of tasks.

amichuda commented 3 years ago

Thinking about this a bit more, wouldn't this require a special node myst-nb?

https://github.com/executablebooks/MyST-NB/issues/295

AakashGfude commented 3 years ago

@amichuda was working on this a bit. the one major problem I faced was in Sphinx LaTex writer, which escapes characters, like \ with a \textbackslash{} for example.

The above was converted to

\textbackslash{}begin\{tabular\}\{lr\}
\textbackslash{}hline
 item   \&   qty \textbackslash{}\textbackslash{}
\textbackslash{}hline
 spam   \&    42 \textbackslash{}\textbackslash{}
 eggs   \&   451 \textbackslash{}\textbackslash{}
 bacon  \&     0 \textbackslash{}\textbackslash{}
\textbackslash{}hline
\textbackslash{}end\{tabular\}

not sure if the fix for https://github.com/executablebooks/MyST-NB/issues/117 is gonna take this into account. Maybe @chrisjsewell can shed some more light on this.

Else, the only option I could figure out to solve this was to handle it in the writing phase of literal_block. Where I can check if the output is latex code, and then prevent these escape characters.

amichuda commented 3 years ago

Thanks for looking into this! I wonder if this can also just be handled by putting doubling the backslashes when generating the table:

\​\begin{tabular}{lr}
\\hline
 item   &   qty \\\\
\\hline
 spam   &    42 \\\\
 eggs   &   451 \\\\
 bacon  &     0 \\\\
\\hline
\\end{tabular}
AakashGfude commented 3 years ago

yes, I do have created a post-transform to detect latex code. let me try doubling the backslashes and see. Thank you.

amichuda commented 3 years ago

Thank you so much! If this all works out I'm gonna add you and the jupyterbook team to the special thanks in my thesis!

AakashGfude commented 3 years ago

Thank you so much! If this all works out I'm gonna add you and the jupyterbook team to the special thanks in my thesis!

wohoo. need to work extra hard on this one then. :)

mmcky commented 3 years ago

@AakashGfude is this still an open issue?