Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper syntax for LaTeX Equation Environment (and the related ones) #339

Closed
ickc opened this issue Oct 7, 2016 · 11 comments
Closed

Improper syntax for LaTeX Equation Environment (and the related ones) #339

ickc opened this issue Oct 7, 2016 · 11 comments

Comments

@ickc
Copy link

ickc commented Oct 7, 2016

In a old, messy document I wrote in ickc/markdown-variants: Testing LaTeX Environments Usage in MathJax From Markdown Convertion (including mmd and pandoc), there's a bunch of LaTeX environments that MathJax also support are expected to be bare (without $$).

Currently in typora, it has to be typed in

$$
\begin{equation}
E=mc^2
\end{equation}
$$

which will resulted in an error in LaTeX output.

@abnerlee
Copy link
Contributor

abnerlee commented Oct 9, 2016

#63

@ickc
Copy link
Author

ickc commented Oct 9, 2016

@abnerlee the issue you refer to is completely irrelevant. One is about alternative math delimiter, another is about wrong math delimiter.

@abnerlee
Copy link
Contributor

abnerlee commented Oct 9, 2016

They are same/similar math delimiter issue -> make math delimiter configurable.

From the link:

Unlike actual TeX, however, in order for MathJax to process these, they must be enclosed in math delimiters (since MathJax only processes macros in math-mode).

So, math delimiters should be used. And no "standard" math delimiter is given, and it is configurable for MathJax, so there's no "wrong" math delimiter.

From https://github.com/ickc/mathjax-latex-md-mmd-pandoc

  • MWE-mmd.*: Written in MultiMarkdown, meaning that begin{... is typed as \\[begin{... instead. In case the environment really expects to be within another environment, a line break is used such that \\[ and begin{... is not consecutive. HTML and TeX are generated by MMD2HTML and MMD2TeX [^ where ickc/peg-multimarkdown-latex-support: Default support files for generating LaTeX documents with MMD 3 through MMD 5 is used for easy LaTeX setup (e.g. loading packages)]
  • MWE-pandoc.*: Written in pandoc's Markdown. $$ is used, but otherwise identical to MWE.md. --mathjax argument is used in the conversion to HTML: pandoc --mathjax -s -o .... [^ done in the way I did (that I load the MathJax in the md file), MathJax would be loaded twice. However, without `--mathjax` many equations would disappeared from the HTML output. Probably the best way is to change the template of how MathJax is loaded instead, but I didn't dig into it.]. TeX is generated by pandoc -s -o ... [^ and I ignored the error that complains about `amscd` is not loaded. Again probably one should change the template.].

So, MultiMarkdown use \[ and \], Pandoc use $$. We currently support "$" and "$$"

@ickc
Copy link
Author

ickc commented Oct 9, 2016

It's not... configuring MathJax delimiters can be done natively in MathJax, and can be configured right now in typora if you are willing to dive into the contents of topora.app.

The issue I was pointing out is that in a certain environments, no delimiters should be used. You can (use delimter) if you only want MathJax (in HTML) support, but if you want to export to LaTeX, it will throw an error.

Again, the difference is

  1. configuring Math delimiter that is configurable in MathJax
  2. given the math delimter required in typora, how it should be handled in LaTeX export. Or, if typora do not want to handle the extra task of removing the math delimiter that typora requires, then typora need to parsed bare LaTeX environment as well (this is actually more complicated, I would say). Both are valid MathJax delimter, so it is not about configuring it.

See? Completely different.

You could always try to draw similarities, but in terms of fixing the issue, it requires a totally different "patch". Hence, a different issue.

Edit: By the way, the section you quote from mine is not relevant to this issue...

@ickc
Copy link
Author

ickc commented Oct 10, 2016

Forget the point I mentioned about it can be done through modifying the .app. The app itself still need to recognize it before passing it to MathJax.

The other thing I said is still relevant, unless you want to allow \begin... to also be a math delimiter. By the way, pandoc starting from 1.17.2 does have this behavior (that allow bare LaTeX environment to be parsed as math). See jgm/pandoc#2758

But for typora, adding an extra math delimiter (which is already valid in typora) is easier. It just need a post-processor like this for LaTeX output.

@abnerlee
Copy link
Contributor

OK, so your requirement is that Typora should use same syntax as bare LaTeX regarding to LaTeX expressions?

I'm not pro LaTeX user, so I checked https://en.wikibooks.org/wiki/LaTeX/Mathematics, one example it provide is

Take $\sfrac{1}{2}$ cup of sugar, \dots

  3\times\sfrac{1}{2}=1\sfrac{1}{2}


Take ${}^1/_2$ cup of sugar, \dots

  3\times{}^1/_2=1{}^1/_2

It's just confusing to me that sometimes "$" is used, and sometimes it is not. So should typora also parse \dots or
3\times\sfrac{1}{2}=1\sfrac{1}{2} as LaTeX even when they are not wrapped within "$"? If so, it will be difficult to decide whether a text is LaTeX code or not.


About export, Typora provide a wrapped method in Export As -> LaTeX in menu bar to prevent such error, and be able to export styles like highlight or underline which is supported by Typora but not by Pandoc.

@ickc
Copy link
Author

ickc commented Oct 10, 2016

The example over there might have some problems. The wikibook about LaTeX sometimes introduces wrong/poor habit. I'm not sure if this one is intentional or a mistake. But I tried to compiled it and it throws an error.

The \ldots there is not math, so it is in "bare" LaTeX. It is exactly this kind of things that I don't recommend typora as a GUI editor to parse them.

May be I didn't make the point clearer, so let me explain it again:

There's a bunch of math environments that is not in math delimiters, e.g. equation. (These environments usually involve numbering. The full list is in the link in the original post)

So in LaTeX, they should be typed like this:

Syntax 1:

\begin{equation}
E=mc^2
\end{equation}

In MathJax, it is more tolerate (and has historical reason) and allow both the above syntax, or

Syntax 2:

$$\begin{equation}
E=mc^2
\end{equation}$$

But the problem is this later one is not a correct LaTeX syntax. If it is left as is when export to LaTeX, latex will throw an error, sometimes still compilable, sometimes halted (depending on exactly which environment we are talking about).

The problem is how to solve this problem? Either the source use syntax 1, and the parsing engine need to understand such syntaxes (a bunch of them and only them. The newest patch in pandoc fixes the bug that it includes too much \begin...\end... pairs.), or use syntax 2, and requires a post-processor when exporting to LaTeX (so in LaTeX output, syntax 2 becomes syntax 1).

pandoc chose the 1st approach, since pandoc has long been parsing some LaTeX in the md source (e.g. \newcommand). My suggestion to typora is to pick the 2nd approach instead.

The reason is that typora provides live-preview, if you choose the 1st approach, that your typora live-preview engine need to understand that syntax as well such that after the user hit enter after syntax 1, it will turns into math preview.

This could create problems since typora also uses pandoc, and the suggestion seem to contradict with pandoc's dicision.

The counter argument to this disadvantage are:

  1. nothing prevent one to use the 2nd in pandoc. I personally use it on my legacy files that used syntax 2. That's why I wrote that post-processor.
  2. It provides better compatibility to other variants of markdown, especially MultiMarkdown that expects syntax 1 only.
  3. typora is specifically said somewhere about the compatibility issue with pandoc. typora uses an internal engine to output an AST understood by pandoc. So typora's source not being compatible with pandoc is understood.

@abnerlee
Copy link
Contributor

abnerlee commented Oct 10, 2016

  1. I will investigate how other Markdown engine handler this, and whether they have compatibility issue with the expression $$\begin.. and \end..$$. But is there any other LaTeX syntax that should not be wrapped by "$$"?

  2. We can consider support the \begin ... \end block without $$, or provide "import/convert" function to allow user convert legacy files to Syntax 2: which Typora support. Since we are writing markdown, not LaTeX, wrap all LaTeX sentence with math dilemma seems acceptable to me, and easier for Typora ro parse

  3. Have you try Typora's Export -> LaTeX function from menu bar? Is the output LaTeX incorrect if there are math blocks wrapped with $$?

    There's the snip of LaTeX converted by Typora: (with Pandoc 1.16)

    \begin{document}
    
    \[\begin{equation}
    E=mc^2
    \end{equation}\]
    
    \end{document}
    

    If so, then it is a bug

@ickc
Copy link
Author

ickc commented Oct 11, 2016

  1. If you're talking about LaTeX environment supported by MathJax, no. The list (and preprocessor) I put there is according to MathJax's documentation. Probably I have a reference in my repository
  2. Agree. MultiMarkdown (and its GUI MMD Composer) took that approach.
  3. You are right, the snippet you quote is wrong in LaTeX. For some environments LaTeX will complain but successfully compiled, sometimes halted and failed to compile.

@abnerlee
Copy link
Contributor

We will consider it a bug if "Export" -> "LaTeX" provided by typora would generate invalid latex output

@abnerlee abnerlee added the bug label Oct 11, 2016
@abnerlee
Copy link
Contributor

In new version, "Export" -> "LaTeX" operation will not include the unnecessary math delimiter. So exported LaTeX can be compiled.

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

No branches or pull requests

2 participants