open

## mathtextile support?

Reported by emime | February 27th, 2009 @ 09:17 AM

Is it possible to support math?

Possible way:

Inside the plain textile, I can insert a tag (or something like) where insert latex math formulas.

When the converter to_html finds , it converts the latex into MATHML through MATHML library. When the convert to_latex finds , it just encloses math between a math environment.

Thank you, Emi

### Comments and changes to this ticket

• #### Jason GarberFebruary 27th, 2009 @ 09:34 AM

• Tag set to latex
• State changed from “new” to “open”

Sure. Work up a patch (with good tests) and I'll include it. You can fork on github.

• #### emimeFebruary 27th, 2009 @ 10:01 AM

Great. I would like. But I don't know where to start (new to ruby).

I had a look to the textile_doc.rb and formatter...not clear for me how to manage options and new tags.

Some help? Thanks in advance. Emi

• #### Jason GarberMarch 2nd, 2009 @ 10:27 AM

• State changed from “open” to “invalid”
• Milestone cleared.

If you're new to Ruby, I'm guessing you're probably new to Ragel and C and Java extensions as well. Adding a feature like you're talking about would take quite a bit of expertise and effort in all of these, plus LaTeX. Unless you can find someone with the necessary expertise and inclination, this will probably have to be one feature we live without.

• #### emimeMarch 3rd, 2009 @ 07:37 AM

Yes, you are true, I'm new to Ragel and C/Java ext as well. I didn't think a feature like that requires extensions... :(

Anyway I dont want waste your time. I will send a link to this ticket to the guy that implement the latex formatter. May be he's intersted.

I just attach the mathutil.rb file that does mathml by preprocessing the textile before using RedCloth.to_html. May be sometime someone finds it useful or you decide to impement the feature.

To use it just...:

latextextile = "My textile with my mathml display formula: $$x\rightarrow y$$"

# replace latex markup with mathml inside textile containing latex

textile = MathmlUtil.replacelatex latextextile

# convert textile to xhtml

xhtml = MathmlUtil.textile2xhtml textile

# write and save xhtml for displaying it in Mozilla

xml = MathmlUtil.writexml xhtml

• #### Jason GarberMarch 3rd, 2009 @ 07:56 AM

• State changed from “invalid” to “open”

Oh, I see. I was wrong. I thought it would need to recognize all the formula things, and that would require a lot of additions to the parser. I'll take a look at what you sent.

• #### emimeMarch 4th, 2009 @ 11:09 AM

I've found (may be) a bug in to_html during conversion. Please explain the following behavior:

I start writing this:


Math $f(x)$ and math $f(x)$


My code pre-processing code produces:


Math <notextile><math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo>
[/itex]</notextile> and math <notextile><math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo>
[/itex]</notextile>


That's fine...isn't? Now giving this to to_html I obtain the following:


<p>Math <math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo></p>
<p>[/itex]</notextile> and math <math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo></p>
<p>[/itex]</notextile></p>


Why this? It seems that to_html cuts a piece of text...

Thanks Emi

• #### emimeMarch 4th, 2009 @ 11:17 AM

I mean, I would expect this from to_html


<p>Math <math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo>
[/itex]</notextile> and math <math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo>[/itex]</notextile></p>

• #### emimeMarch 4th, 2009 @ 11:20 AM

Or better (forgot ) this is the expected result...


<p>Math <math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo>
[/itex] and math <math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'>
<mi>f</mi>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo>[/itex]</p>

• #### emimeMarch 4th, 2009 @ 11:35 AM

I've found the problem...it's due \n.... to work fine I need to use RedCloth like this....


rc = RedCloth.new(mymathtextile)
rc = rc.delete("\n")
rc.hard_breaks = false
xhtml = rc.to_html

• #### emimeMarch 5th, 2009 @ 09:22 AM

Hello, I made a big stresstest starting from a textile+latex document and things seem now to work fine.

The main problem in inline-mathml is that it comes with newlines and to_html interfers with it brokening the syntax (even if math is inside notextile..RedCloth bug?).

So, finally, I had to remove newlines inside mathml trees from the RedcCloth object and disable hardbreaks like this.


rc = RedCloth.new(textile)
#puts "\nRedCloth object...\n" + rc
rc = rc.gsub(/<math display(.+?)<\/math>/m) {|s| s.delete("\n")}
#puts "\nRedCloth object without newlines" + rc
rc.hard_breaks = false
xhtml = rc.to_html


With the hope you are interested in, I attach the new file modified, and the textile page I test with its output.

Emi

• #### emimeMarch 9th, 2009 @ 11:59 AM

I used '$' sign to delimit latex formulas (as latex itself uses)...but may be is not the right choice. Someone would like use '$' for its actual meaning (money).

So...I'm in doubt..which sign is better to delimit formulas?

Don't like the idea of a tag like latex because is heavy to write. I would like a simple delimiter like '$'. Any idea? May be this '\$'?

• #### Jason GarberMarch 17th, 2009 @ 04:49 AM

• Assigned user cleared.

Bill, do you care to weigh in? I haven't had any time to look at this and I'm afraid I won't for quite awhile.

• #### Bil KlebMarch 17th, 2009 @ 08:56 AM

• Tag changed from latex to html, latex, standard
• Assigned user set to “Bil Kleb”

I took a look at mathmulti.zip and like what I saw. To me the biggest question is how do we incorporate it into Textile?

• #### emimeMarch 17th, 2009 @ 11:49 AM

ok, I see.

If we want embed mathtextile into RedCloth, I think that the "option" way is the best choice.

May be we should proceed as follows...

First, we need to tell RedCloth to use math ext. according to a friend option:


rc = RedCloth.new(mathtextile)
rc.mathext = true


Second, because we don't want break standard methods, we could use overriden versions of the to_html and to_latex methods that assume extended textile is used and some pre-processing must be performed before invoking the original versions of to_html and to_latex.

If this is acceptable (please confirm or complain), we should furtherly discuss whether use the parser (don't know Ragel and extensions) or simply regex.

• #### Bil KlebMarch 17th, 2009 @ 12:29 PM

Yes, and/or something like


rc = RedCloth.new( mathtextile, :math_extensions => true )


As far as the parser implementation, I'll defer to Jason...

Looks like he just committed an EXTENSION_LANGUAGE option for possibly facilitating pluggable extensions?

http://github.com/jgarber/redclo...

• #### emimeMarch 18th, 2009 @ 03:39 AM

mmm...I had a look to it, but it's a little cryptic for me :(

Do you know how does it work? Is there some documentation?

• #### emimeMarch 19th, 2009 @ 10:51 AM

Hello, don't know really how redcloth uses .yml files but here there is a simple idea on how the things should be managed:


name: theorem
in: th. My theorem
html: <div class='theorem_env'> <p class='theorem_lbl'> Theorem. <span class='theorem_txt'> My theorem </span><p/></div>
latex: \begin{th} My theorem \end{th}


with the latex command \newtheorem{th}{Theorem.} inserted in the preamble upon to_latex execution. Because math (and non-math) texts can contain similar custom-structures, there should be the possiblity of reading from an extension file my structures and have automatically implemented them in to_html and to_latex. This means that I would need to tell Redcloth:


rc = RedCloth.new( mathtextile, :math_extensions => true )
rc.import_math_extenstions(file)


where file is something like:


name: theorem
extenstion: th.


With the hope that what above is understandable...I would like to know if this feature can be easily achieved...really don't know what should be changed.

• #### Tim HarperDecember 2nd, 2009 @ 10:29 AM

I'm interested in this and can help implement it. What's the status on this?

• #### emimeDecember 4th, 2009 @ 03:44 AM

• Tag changed from html, latex, standard to html, latex, mathml, standard

I'm happy you're interested on it. At the moment the things stopped here, no implementation. Just the a "code-draft" (see attachment mathmlutil.zip in this same ticket).

If you like, better..if you know redcloth you could fork and try to extend it with extension http://github.com/jgarber/redcloth/commit/f04cdbb645e0823def2a595cf... as suggested by Bill.

The mathml library, on which the idea is based, is in a good state and works fine.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

Help contribute to this project by taking a few moments to create your personal profile.

RedCloth is a Ruby library for converting Textile into HTML