sphinx-jupyterbook-latex icon indicating copy to clipboard operation
sphinx-jupyterbook-latex copied to clipboard

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

Open amichuda opened this issue 4 years ago • 8 comments

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?

amichuda avatar Nov 10 '20 02:11 amichuda

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

AakashGfude avatar Nov 10 '20 22:11 AakashGfude

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

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

amichuda avatar Jan 21 '21 09:01 amichuda

@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.

AakashGfude avatar Jan 25 '21 06:01 AakashGfude

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}

amichuda avatar Jan 25 '21 06:01 amichuda

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

AakashGfude avatar Jan 25 '21 06:01 AakashGfude

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!

amichuda avatar Jan 25 '21 06:01 amichuda

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. :)

AakashGfude avatar Jan 25 '21 06:01 AakashGfude

@AakashGfude is this still an open issue?

mmcky avatar Jun 02 '21 05:06 mmcky