KaTeX: Add support for line breaks
Add support for line breaks. \\, \newline…
Thanks.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 59
- Comments: 44 (14 by maintainers)
Add support for line breaks. \\, \newline…
Thanks.
@spelufo @spicyj @sohailykhan94 After an hour of experimentation, I’ve came out with a code injection / script hacking method which injects my code into the KaTeX context. This function automatically splits the line breaks with ease, and is friendly towards single-line equations.
This function ought have been used only once, before any render operations are executed.
You may also use this one with some additional CSS tweaks, which are extremely useful in responsive designs. Inline equations wraps at the end of the line, and displayed equations scroll when being too long. Stick these code into the CSS stylesheet.
Hope this may help you do this. However, display effects might differ from TeX standards, and this is only a quick fix. May these tweaks be incorporated into the master branch, and leave a thumbs up if you find this useful.
5 April fix: The CSS works bad on desktops. Now it looks better for
.katex .display.I’m new to this thread. Neat hack!
In terms of workarounds, it seems that adding
\begin{aligned} ... \end{aligned}around your multi-line text would also be good. OK, to get left justification of each line, you have to add&to the beginning of each line too. But this input looks pretty good on the KaTeX tester:I was surprised to learn that
\\and\newlinework in math mode in LaTeX. Note, however, that they do not work in display environments ($$...$$and\[...\]) – or rather, they compile, but don’t break the line. (Their definition must be overridden by those environments.) They do work in$\displaystyle ...$though.Anyway, it seems that the way to implement this would be to somehow transform
line1 \\ line2into the equivalent of\begin{aligned} &line1 \\ &line2 \end{aligned}. I think this could be done via the infix operator mechanism inParser.js… though I’m not quite sure how it would work with multiple\\s.This is how I’m dealing with it in my react project:
Its a hack and I feel very dirty, but deadline soon. Might help someone else.
@tivewsalaeharad Please open a new issue, and include an example of the LaTeX code you’re expecting to wrap that isn’t (maybe with a screenshot).
PR #1298 implements
\\and\newlineat the top level of inline math.Some documentation on how to set up MathJax as a fallback for KaTeX in the mean time would probably be good.
@anirudha-banerjee Yes, KA uses MathJax when KaTeX fails.
OK, good to know there’s applications. I’m interested in working at this at some point, but don’t know when I’ll have time.