Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

trouble with _repr_latex_ and nbconvert #16

Closed
micahscopes opened this issue Apr 7, 2016 · 10 comments
Closed

trouble with _repr_latex_ and nbconvert #16

micahscopes opened this issue Apr 7, 2016 · 10 comments

Comments

@micahscopes
Copy link

I'm using Jupyter 4.1.2.

I have a simple class designed to make a separator. This helps me stay sane with cells that have a lot of output.

from IPython.display import display
class Separator(object):
    def __repr__(self):
        return "---------------------------------------------------------"
    def _repr_latex_(self):
        return "$\\noindent\\rule{8cm}{0.4pt}$"
    def _repr_html_(self):
        return "<hr style='border: solid black 1px'/>"

def separate():
    sep = Separator()
    display(sep)

Well, I'm having trouble getting nbconvert to call Separator._repr_latex_. Instead, in my tex output I get the __repr__ output, like this:

    \begin{verbatim}
---------------------------------------------------------
    \end{verbatim}

What am I missing? I've had this problem before in other situations involving every table library I could get my hands on. Now the issue is coming up in a very clean cut circumstance!

@micahscopes
Copy link
Author

P.S., I'm planning on implementing some "totally sweet horizontal rules" as part of this class 😮
a la: http://tex.stackexchange.com/questions/32711/totally-sweet-horizontal-rules-in-latex

@micahscopes
Copy link
Author

Somehow it's working now. I think it's because I also did pip install --upgrade nbconvert, but I'm not sure... I also killed and restarted the jupyter notebook process, althought I don't know how that would have affected running jupyter nbconvert ... from the command line. It's possible though that saving a jupyter notebook from the old version of jupyter might have interfered with nbconvert somehow. I don't know.

@Carreau
Copy link
Member

Carreau commented Apr 7, 2016

Happy you solved it by yourself ! Looking forward to "totally sweet hrules"!

@micahscopes
Copy link
Author

@Carreau unfortunately, I would need to have access to the preamble to include the fancy ornaments. I'm not aware of a way to do this aside from a custom template. It would be cool if objects using repr_latex could request packages to be included. Is this possible that you know of?

Anyway, for a basic hrule, it's working great!

@takluyver
Copy link
Member

Maybe the discussion of how HTML/JS outputs can use JS libraries without re-including them completely in each output should also try to solve for Latex outputs adding things to the preamble - they seem like similar conceptual things. The technical details may be different enough that it doesn't make sense to tackle them together, though.

@Carreau
Copy link
Member

Carreau commented Apr 8, 2016

Preamble can be modified with custom exporter, and apparently our preamble comes from a specific run of pandoc, but pandoc's preamble depend on the feature you use, so it should not be a static preamble...

So much fun...

@micahscopes
Copy link
Author

The final product 👍

from IPython.display import display
class Separator(object):
    def __init__(self,width=1,thickness=2):
        self.thickness=float(thickness)
        self.width=float(width)
    def __repr__(self):
        return "---------------------------------------------------------"
    def _repr_latex_(self):
        return r"\hfil\noindent\rule{%s\textwidth}{%spt}\hfil" % (self.width,self.thickness)
    def _repr_html_(self):
        return "<hr style='border-top: solid black "+str(self.thickness)+"px; width: "+str(self.width*100)+"%;'/>"

def hr(width=1,thickness=2):
    sep = Separator(width=width,thickness=thickness)
    display(sep)

separator_html_output.pdf
separator_pdf_output.pdf

@Carreau
Copy link
Member

Carreau commented Apr 8, 2016

sweet. Thanks for sharing.

@micahscopes
Copy link
Author

@takluyver yeah, that seems like the same kind of deal! where can I read more about progress on that? I was actually thinking I'd like to make some objects that output webgl canvases, and it'd be very nice to know about the progress on that feature.

@takluyver
Copy link
Member

That one has been knocking around for a while - see jupyter/notebook#116 and jupyter/notebook#319 for more information. I think it came up again recently, but I don't know where.

Another idea: maybe someone who's more versed in Latex and pandoc could create a Latex exporter that works by loading notebooks directly into pandoc, and having pandoc generate a whole Latex document at once. Currently we run pandoc on a per-cell basis to convert markdown cells to latex, and assemble it all in a template we provide. This allows for powerful customisation using the template structure, but it does make things like the preamble tricky, because pandoc expects that it can generate that as part of its conversion. It's probably worth exploring that trade-off.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants