Is there an easy way to convert $$ tags to [tex][/tex]?

  • Thread starter Thread starter Simfish
  • Start date Start date
  • Tags Tags
    Convert Tags
AI Thread Summary
The discussion centers on creating a regular expression (regexp) to convert LaTeX-style dollar signs into a different format, specifically converting "$x^2$" into "[tex] x^2 [/tex]". Participants share various approaches, primarily using Perl, to achieve this transformation while addressing potential issues with greedy matching and stray dollar signs. A suggested solution involves using negative look-behind to prevent matching dollar signs preceded by a backslash, ensuring that escaped dollar signs are not altered. Additionally, users discuss the possibility of implementing similar regex in other programming languages like JavaScript and express the idea of compiling helpful regex examples for LaTeX conversion in forum help threads. Overall, the conversation highlights the challenges of regex syntax and the importance of precise pattern matching in text processing.
Simfish
Gold Member
Messages
811
Reaction score
2
I know that there's probably a way to do this with regexp, but I don't know regexp (yet). So does anyone have any tips on how to create the regexp expression for it? (and what other ways are there?)
 
Technology news on Phys.org
Simfish said:
I know that there's probably a way to do this with regexp, but I don't know regexp (yet). So does anyone have any tips on how to create the regexp expression for it? (and what other ways are there?)

What language are you using? Do you have an example of a string you'd like to convert from and what you'd like to convert it to?

DaveE
 
regexp would be sufficient enough (regexp works with lots of languages). Just a search-replace string that I could use for a program that uses regexp.

So convert $x^2$ into x^2[/ tex]
 
perl -e 'undef $/; $_ = <>; @a = split /\$/; while (@a) { print(shift @a); print("&quot;.(shift @a).&quot;") if (@a); }'

(This will choke if there are stray dollar signs.)

A pure regexp solution is likely to run into greedy-match problems if there is more than one $$ pair in the string.

EDIT: It wants to interpret my TEX tags as actual tex tags. Hit quote if you want to see the actual source.
 
Last edited:
Yeah, Perl-wise, I think I'd do:

Code:
$string =~ s/\$([^$]*)\$/\[xet\]$1\[\/xet\]/gs;

As pointed out, this won't always work if there are any extra $ signs floating around. So if you got a string of:

Code:
I paid $3 to Tim to get the answer $x^2$, but Bob only charged $2.

That obviously wouldn't work. It would change it into:

Code:
I paid [xet]3 to Tim to get the answer [/xet]x^2[xet], but Bob only charged [/xet]2.

But you REALLY wanted:

Code:
I paid $3 to Tim to get the answer [xet]x^2[/xet], but Bob only charged $2.

If there are rules about what can go inside the $$, you can write that into the regexp to make it more fool-proof, but I'm not sure what the rules might be. For instance, if there is no whitespace allowed within the $$, you could use the regexp:

Code:
$string =~ s/\$([^$\s]*)\$/\[xet\]$1\[\/xet\]/gs;

And that'd work on the above string.

[edit]Fought with IP board code about recognizing tags-- just pretend that "xet" is "tex", in the above.[/edit]

DaveE
 
Last edited:
davee123 said:
Yeah, Perl-wise, I think I'd do:

Code:
$string =~ s/\$([^$]*)\$/\[xet\]$1\[\/xet\]/gs;

As pointed out, this won't always work if there are any extra $ signs floating around.

I think you can get around this by using perl's ridiculous ?<! operator ("zero-length negative look-behind"!), something like:

Code:
$string =~ s/(?<!\\)\$([^\$]*[^\\])\$/\[xet\]$1\[\/xet\]/gs;

This should refuse to match $s that are preceded by a \, either inside or outside tex areas. So like you'd be able to write "I gave steve \$3" and it would safely understand this is not a tex delimiter.

I was worried greedy matching would mess you up (ie "I think $x^2$ is a better equation than $2^x$" and it tries to match "is a better equation than" as the tex area) but when I tested this actually wasn't a problem! So there's your solution. Although I'd take advantage of perl -p:

Code:
perl -pe 's/(?<!\\)\$([^\$]*[^\\])\$/\[xet\]$1\[\/xet\]/gs;'
 
Simfish: Try this substitute (s) command.

:set magic
:s;$\([^$]*\)\$;[noparse]\1[/noparse];g

If you want it to change only the first occurrence of $$ in the line, remove the "g" character. (The colon shown above just represents the command mode prompt, and is not part of the commands.)
 
Last edited:
To all of you fighting tags recognition by forum software - try [noparse][noparse][/noparse][/noparse] tags.
 
Thanks for all the replies, everyone! :) Based on the input, I've created my own regexp expression (for the JGSoft software that I use). Which is basically this:

search: \$([^$]*)\$

replace: [xet]$1[/xet]

===

It actually might be helpful to put sample regexp expressions on the PF Help threads about LaTeX (since it makes it much easier for people to convert their tags from their documents to the LaTeX demanded by PF).
 
  • #10
Borek, thanks, is that documented somewhere?

Simfish said:
Thanks for all the replies, everyone! :) Based on the input, I've created my own regexp expression (for the JGSoft software that I use). Which is basically this:

search: \$([^$]*)\$

replace: [xet]$1[/xet]

===

It actually might be helpful to put sample regexp expressions on the PF Help threads about LaTeX (since it makes it much easier for people to convert their tags from their documents to the LaTeX demanded by PF).

Or maybe just post a converter script somewhere.

Actually, I'm pretty sure you could do this particular regexp in Javascript. If I submit a "convert $tex$ to [xet] tags" javascript to will physicsforums post it somewhere?
 
Back
Top