Followers 0

# LaTeX reformulation

## 5 posts in this topic

#1 ·  Posted (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?

And have a good day,

Edited by eEniquEe

##### 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 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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 #### Share this post ##### Link to post ##### 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,

##### Share on other sites

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?

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.

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

##### 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,

## Create an account

Register a new account