eEniquEe Posted April 5, 2011 Posted April 5, 2011 (edited) 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, Edited April 5, 2011 by eEniquEe
Moderators Melba23 Posted April 5, 2011 Moderators Posted April 5, 2011 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
eEniquEe Posted April 5, 2011 Author Posted April 5, 2011 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,
Moderators Melba23 Posted April 5, 2011 Moderators Posted April 5, 2011 eEniquEe,everything in the (...) part, if found will be stored in $1, $2, $3, $4You got it! put a backslash at the beginningPutting 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? M23P.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. 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
eEniquEe Posted April 5, 2011 Author Posted April 5, 2011 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,
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now