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

  • Thread starter Thread starter Simfish
  • Start date Start date
  • Tags Tags
    Convert Tags
Click For Summary

Discussion Overview

The discussion revolves around methods for converting mathematical expressions enclosed in dollar signs ($$) to a different format using [tex][/tex] tags. Participants explore various programming languages and regular expression (regexp) techniques to achieve this conversion, discussing both theoretical and practical aspects.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Some participants suggest using regular expressions (regexp) for the conversion, noting that they can be implemented in various programming languages.
  • One participant provides a Perl command that splits strings based on dollar signs but warns about potential issues with stray dollar signs.
  • Another participant proposes a Perl regexp solution but acknowledges that it may not work correctly if extra dollar signs are present in the string.
  • There is a discussion about refining the regexp to avoid greedy matching and to handle escaped dollar signs, with one participant suggesting the use of negative look-behind assertions.
  • Some participants share their own regexp expressions and discuss their effectiveness in different contexts, including a specific example for JGSoft software.
  • One participant mentions the possibility of creating a JavaScript solution for the conversion and suggests posting it on the forum.

Areas of Agreement / Disagreement

Participants express varying opinions on the best approach to achieve the conversion, with no consensus on a single solution. Different programming languages and methods are proposed, indicating multiple competing views.

Contextual Notes

Limitations include potential issues with extra dollar signs in the input strings and the need for specific rules regarding what can be included within the dollar signs for the regexp to function correctly.

Who May Find This Useful

This discussion may be useful for individuals interested in programming, particularly those looking to manipulate text with regular expressions, as well as users of forums that require specific formatting for mathematical expressions.

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 [tex]x^2[/ tex][/tex]
 
perl -e 'undef $/; $_ = <>; @a = split /\$/; while (@a) { print(shift @a); print("[tex]".(shift @a)."[/tex]") 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][tex]\1[/tex][/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?
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
28
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K