Load LaTeX macros from file in Jupyter notebook

870 views
Skip to first unread message

Keir Lockridge

unread,
Jul 23, 2016, 5:24:58 PM7/23/16
to Project Jupyter
Hi,

It seems that latex macros may be included in any markdown cell (between dollar signs), and these macros are then available throughout the notebook. Is there a way to load a file containing LaTeX macros in a markdown cell (or in some other location, if it gets the job done)? I am trying to avoid having to cut-and-paste a long list of LaTeX macros into every Jupyter Notebook I create.

Thanks!

--Keir

Thomas Kluyver

unread,
Jul 23, 2016, 6:43:05 PM7/23/16
to Project Jupyter
On 23 July 2016 at 22:24, Keir Lockridge <keirh...@gmail.com> wrote:
It seems that latex macros may be included in any markdown cell (between dollar signs), and these macros are then available throughout the notebook. Is there a way to load a file containing LaTeX macros in a markdown cell (or in some other location, if it gets the job done)? I am trying to avoid having to cut-and-paste a long list of LaTeX macros into every Jupyter Notebook I create.

You can construct an IPython.display.Latex object with a string - which you could read from a file. When that object is displayed, its content is added to the page as Latex.

Within the notebook interface, Latex is rendered by the Mathjax library, so only a subset of Latex works, but it sounds like what you want to do is already working.

Thomas

Keir Lockridge

unread,
Jul 24, 2016, 11:09:17 AM7/24/16
to Project Jupyter
Thanks! This worked -- I appreciate the help.

--Keir

Keir Lockridge

unread,
Jul 24, 2016, 11:40:15 AM7/24/16
to Project Jupyter
Actually, I find that when I reload the notebook I have to re-run all the markdown cells (after executing the code cell with the IPython.display.Latex object), otherwise any LaTeX code in the document using the macros doesn't display properly.

In the first cell of the notebook I have:

import IPython.display
IPython.display.display_latex(IPython.display.Latex(filename="LaTeXmacros"))

If I run this cell and then make markdown cells using my macros, it works. When I reload the notebook, none of my macros are rendered properly (even if I re-run the top cell) until I re-run each markdown cell.

Is there a way to avoid this?

--Keir

On Saturday, July 23, 2016 at 6:43:05 PM UTC-4, takowl wrote:

Thomas Kluyver

unread,
Jul 24, 2016, 5:42:00 PM7/24/16
to Project Jupyter
Sorry, no ideas spring to mind; I don't know much about the details of how Mathjax works. Someone else might be able to help you more.

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/f9c58601-14d7-44af-8c18-744ee93b54a3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

David Ketcheson

unread,
Mar 7, 2018, 12:59:18 AM3/7/18
to Project Jupyter
I wrote a nbextension that does exactly this.  See https://github.com/ipython-contrib/jupyter_contrib_nbextensions/pull/1257.
Reply all
Reply to author
Forward
0 new messages