Jump to content

LaTeX reformulation


Recommended Posts

Hi,

I'm really stuck on this problem. I'm using Maxima as my Computer Algebra System. Basically, I want Maxima to solve Mathematics problem(s), and then print out the result, and I'll take that result, and change it to LaTeX source code. And print out the answer.

Well, I have most of the things done. I can:

  • Communicate with Maxima (i.e, telling it to solve problems); and read the answer it returns.
  • Change everything to LaTeX code using the function tex in Maxima.

There's one thing that still troubles me is that, when I work with Maxima, it understands cube-root as power of one-third (or written mathematically, ^(1/3)). Say, cube-root of x is x^(1/3). And when it TeXifies the expression, it'll become something like this: x^{{{1}\over{3}}}. Whereas the cube-root in LaTeX should be: \sqrt[3]{x}. Writing x^{{{1}\over{3}}} is not incorrect (I may say), but it makes the output looks somewhat... ugly. :)

I'm currently working on some way to change {something}^{{{a}\over{b}}} to \sqrt[b]{\left( {something} \right) ^ {a}}

I think I can do this by scanning each character in the expression, and make some modification(s) where needed. But in my opinion this method may be very time-consuming, and it's a little bit messy.

Can StringRegExp, or StringRegExpReplace function help me in this situation? I think it's really close to what I'm trying to do. But I'm not really sure how to use it here. Since the something part is unknown, and it may have no pattern at all. There can be a couple of opening half-round brackets "{" and closing ones "}", or even a couple of \left( ... \right) in it. So I don't really how to search for the complete expression of the something part.

Sorry for my bad English, I'm trying to be as clear as I could.

If someone still finds it unclear, just tell me, and I'll explain every again.

Can someone please give me a push? :)

Thanks very much in advance,

And have a good day, :P

Edited by eEniquEe
Link to comment
Share on other sites

  • Moderators

eEniquEe,

Try something like this:

$sText = "{something}^{{{a}\over{b}}}"

$sNewText = StringRegExpReplace($sText, "{(.*)\}\^\{\{\{(.*)\}\\over\{(.*)\}\}\}", "\\sqrt[$3]{\\left({$1}\\right)^{$2}}")

ConsoleWrite($sNewText & @CRLF)
ConsoleWrite("\sqrt[b]{\left({something}\right)^{a}}" & @CRLF)

Does that do it for you? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

eEniquEe,

Try something like this:

$sText = "{something}^{{{a}\over{b}}}"

$sNewText = StringRegExpReplace($sText, "{(.*)\}\^\{\{\{(.*)\}\\over\{(.*)\}\}\}", "\\sqrt[$3]{\\left({$1}\\right)^{$2}}")

ConsoleWrite($sNewText & @CRLF)
ConsoleWrite("\sqrt[b]{\left({something}\right)^{a}}" & @CRLF)

Does that do it for you? :)

M23

Yay, thanks!!! That works like a charm!!! I think I get it, everything in the (...) part, if found will be stored in $1, $2, $3, $4,... respectively, right? And hence, we can use that to modify the expression.

I'm not very familiar with StringRegExp, and StringRegExpReplace. Thanks very much for your help. :)

But I still have one more question:

$sNewText = StringRegExpReplace($sText, "{(.*)\}\^\{\{\{(.*)\}\\over\{(.*)\}\}\}", "\\sqrt[$3]{\\left({$1}\\right)^{$2}}")

If I put a backslash at the beginning like this, will it make any different? I cannot see any difference in the result. So I'm guessing that we can omit the backslash at the very beginning of our pattern, am I correct?

$sNewText = StringRegExpReplace($sText, "\{(.*)\}\^\{\{\{(.*)\}\\over\{(.*)\}\}\}", "\\sqrt[$3]{\\left({$1}\\right)^{$2}}")

Thanks again very much, :P

Link to comment
Share on other sites

  • Moderators

eEniquEe,

everything in the (...) part, if found will be stored in $1, $2, $3, $4

You got it! :)

put a backslash at the beginning

Putting a single \ at the start of the pattern will have no effect. If you want to match a \ at the start of $sText then you have 2 choices:

- 1. Add \\ to the start of the patter (you need to escape the \ as it used by the RegEx engine).

- 2. Omit the \\ at the start of the replacement pattern - then you just keep the \ in the original.

All clear? :P

M23

P.S. This is a good place to start learning about SREs. They are tricky little things, but well worth trying to get to grips with - even if only to my pretty low level. :)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Aaaahh, thank you very very much for your help. :)

I'm reading that page. It's very informative. So... I'm a little bit overwhelmed. :) But don't worry, I'll try to digest all that. Just sooner or later. B-)

Thanks again, :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...