jfbercher / jupyter_latex_envs

(Some) LaTeX environments for Jupyter
Other
112 stars 28 forks source link

latex figcaptions #16

Closed olehwaa closed 7 years ago

olehwaa commented 7 years ago

Hi, Should figure captions generated with latex_envs.figcaption become figure captions in latex document? I didn't understand how so I modified your code to satisfy my needs. It also works with multiple figures in a output cell. The idea is:

Hope you find this way to generate figure captions interesting...

jfbercher commented 7 years ago

This is certainly interesting, thanks!

What I do not understand is that export of figures captions is already implemented. It seems that it doesn't work for you? If so, can you explain me what happens? The following snippet is an example:

import numpy as np
import matplotlib.pyplot as plt 
from latex_envs.latex_envs import figcaption 
%matplotlib inline
plt.plot(np.random.randn((100)))
figcaption("This is the caption", label="fig:attempt")

Then issue jupyter nbconvert --to latex_with_lenvs FILE.ipynb This is explained here and here in the documentation.

Anyway, your way of doing looks more interesting and systematic than the ad hoc search and replace I do in the code, so I will try to include it in a next version. Don't you want to make a pull request yourself?

jfbercher commented 7 years ago

@olehwaa

olehwaa commented 7 years ago

Great that you find my suggestions interesting!

I admit I didn't test the figcaption functionality in it simplest form, so it might work. What I wanted to do was to adapt my custom template based on Making publication ready Python Notebooks. In this template I'm producing a document without the code cells. If I have understood this correctly, the word figcaption you are searching for is the figcaption comand in the code cell. Obviously, there will be no figure captions in the latex document if I remove to code cell.... Another problem is that this approch will not work if you programically set the captions, for instance if you produce plots in a loop and you alter the figure captions for each plot. My solution has been tested to handle these cases, so I think it's more versatile.

The implementation of this concept should be discussed. Writing to the notebook file with display(JSON(d)) is a possibility since it is not actually displayed in HTML or Latex. However, someone might write an extension that display JSON cells, and this data is not meant for displaying. The preprocessing script copy this info into the data subcell metadata, and ideally the captions were writting into metadata of the data subcells with figures, but I don't know if this is possible to do at all, especially if figcaption is called before the figure is produced.

Another option is to write to the cell metadata as a list of captions, and copy this into metadata of the data subcells. I don't know how to do this, but I see that at least it is possible to write to Notebook metadata with traitlets.config.manager.BaseJSONConfigManager

Another thing I would like to have is references to figure set with label in function figcaption working in the notebook.

I'll make a poll request in a few days. (Since this is the first project I participate in on GitHub, I have learn how to make poll requests...)

jfbercher commented 7 years ago

Hi @olehwaa I just updated latex_envs to add some autocompletion capabilities. I understand your usecase and an update will be welcome. I was thinking that it is probably possible to directly write thinks to the cell metadata using a trick like Javascript("var c = IPython.notebook.get_selected_cell(); c.metadata.test = false; console.log(c.metadata)") (where I create a test key with a "false" value) For doing a pull request, you can proceed as follows :

jfbercher commented 7 years ago

Closed by #17