jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.72k stars 4.96k forks source link

Hard max length for latex code in markdown cells? #4773

Open viggotw opened 5 years ago

viggotw commented 5 years ago

There seems to be a max limit to the number of characters I can input in my latex-markdown cell. The code below compiles fine. If I, however, try to add a new line with more than 21 characters, it doesn't compile (see the bottom of the "not working"-file.

I am sorry for the horrible code I am posting below, but I was unable to reproduce it in a clean way and was forced to just anonymize the real code and replace it with this silly-looking latex code.

latex in markdown - working.txt latex in markdown - not working.txt

viggotw commented 5 years ago

I've investigated some more, and managed to destill the code causing trouble som more. Appearently, copying following line 75x times works, but 76x times causes it not to compile. \epsilon_i &: \textrm{Lorem ipsum posum kazaa!} & 3\cdot10^6 [kg]\\

So the full working code would look like this $$ \begin{align*} 75x \epsilon_i &: \textrm{Bindary docking variable} & 3\cdot10^6 [kg]\\ \end{align*} $$

viggotw commented 5 years ago

On a side note, I've noticed that spaces also can put you past this limit. I've used spaces to align the columns separated with &. I eventually had to remove them, because I needed to keep under this weird limit in order to fit all my information. An alternative could, of course, be to split my latex code into multiple cells. But then I lose the alignment...

sammorley-short commented 1 year ago

I am seeing this also. Please help!

sammorley-short commented 1 year ago

(Assuming @viggotw has moved on with his life at this point, but leaving for future Jupyter archaeologists.)

This seems to be a specific issue with only certain text environments, such as textrm, texttt, textit, textbf, but interestingly not text. I ran some test to show this. The following snippet creates a class which renders as LaTeX in jupyter, but with varying size and text field:

class Foo:
    def __init__(self, n, text_env):
        self.n = n
        self.text_env = text_env

    def _repr_latex_(self):
        l, s = 12, 12
        term = f'\\{self.text_env}' + '{' + 'a' * l + r'}'
        line = '&' + ' + '.join([term] * s)
        lines = [line] * self.n
        latex = '\\begin{align}\n' + '\\\\\n'.join(lines) + '\n\\end{align}'
        print(len(latex))
        return latex

We can then use the following snippet to find the points at which the render breaks, and for which text fields. I have narrowed the display to the breakage point, and am printing the length of the Latex string as an approximate number for the amount of text in the text fields.

text_env_tests = {
    'text': (35, 36),
    'textrm': (17, 19),
    'textbf': (17, 19),
    'texttt': (17, 19),
    'textit': (17, 19),
}
for text_env, line_range in text_env_tests.items():
    print(text_env)
    for i in range(*line_range):
        print(i)
        display(Foo(i, text_env))

As you can see, we see the breakage point at around the 5000 character mark for a number of these fields:

Screenshot 2023-02-17 at 10 15 01 Screenshot 2023-02-17 at 10 15 14 Screenshot 2023-02-17 at 10 16 22 Screenshot 2023-02-17 at 10 16 34

However, for the text environment, I couldn't find a breakage point. For example, the render is still going strong at almost double that of the others':

Screenshot 2023-02-17 at 10 16 40

I'm not sure where to go from here. The obvious remedy is to avoid such environments in our expressions, however that is also pretty unsatisfactory for those desiring more complex visual output.

🤷

sammorley-short commented 1 year ago

One very partial solution I discovered:

For outputs where you want a single line of text to be bold or italicised, you can simply prefix a \bf or \emph to the line to achieve this. For example, using this a bold version of the above works fine:

Screenshot 2023-02-17 at 10 41 16