1

When I convert a notebook to latex (then PDF), the images are moved to the bottom of the page. I figured that in latex, it would be possible to keep it in the current position using the 'h' placement specifier:

\begin{figure}[placement specifier]
... figure contents ...
\end{figure}

Is it possible to configure nbconvert to use that as a default value so I don't have to manually process is afterwards?

Reference: https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions

fatdragon
  • 2,211
  • 4
  • 26
  • 43
  • 2
    Possible duplicate of [How to force image to text when converting markdown to pdf using pandoc](https://stackoverflow.com/questions/49040322/how-to-force-image-to-text-when-converting-markdown-to-pdf-using-pandoc) – mb21 Dec 06 '18 at 13:24
  • Thanks, I have used the solution from the referenced article. – fatdragon Dec 29 '18 at 21:16

1 Answers1

0

I think you can get what you want by redefining the appropriate blocks in your template.

In document_contents.tplx, note that blocks data_png, data_jpg etc all call the same macro draw_figure.

I did something similar to what you're after by redefining the figure blocks in my custom template to call a different macro draw_figure_2 which I also defined in my custom template.

Adding something like

((*- block data_png -*))((( draw_figure_2(output.metadata.filenames['image/png']) )))((*- endblock -*))
((*- block data_jpg -*))((( draw_figure_2(output.metadata.filenames['image/jpeg']) )))((*- endblock -*))
((*- block data_svg -*))((( draw_figure_2(output.metadata.filenames['image/svg+xml']) )))((*- endblock -*))
((*- block data_pdf -*))((( draw_figure_2(output.metadata.filenames['application/pdf']) )))((*- endblock -*))


% copied macro draw_figure and made some naive changes; modify as needed
((* macro draw_figure_2(filename) -*))
((* set filename = filename | posix_path *))
((*- block figure scoped -*))
    \begin{figure}[h] % or whatever you want
    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{((( filename )))}
    \end{center}
    \end{figure}
    { \hspace*{\fill} \\}
((*- endblock figure -*))
((*- endmacro *))
butterwagon
  • 479
  • 3
  • 9
  • Thank you - this looks very useful. However, after copying your code into my template file I get an error message: `jinja2.exceptions.UndefinedError: 'output' is undefined` when running `jupyter nbconvert --to latex toplevel.ipynb --template latex-template.tplx` Any ideas? – Thorsten Altenkirch Apr 23 '19 at 11:59
  • Ok I tried to add `((*- extends 'article.tplx' -*))` which stops the error but doesn't output the modified latex code either. – Thorsten Altenkirch Apr 23 '19 at 12:14
  • @ThorstenAltenkirch I'm not able to replicate the problem you are having. You might try replacing "data_priority" (see https://nbconvert.readthedocs.io/en/latest/customizing.html) just to check that the template is being used. Just add this line to your template: `((*- block data_priority -*)) data_priority was here ((*- endblock -*))` and you should see all your outputs replaced by the text "data_priority was here". – butterwagon May 03 '19 at 15:38