How can I replace every global instance of “x[2]” with “x_2” Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Translate in-line equations to TeX code (Any Package?)Automatic LaTeX parsing to replace text with macrosWhat is going wrong with this attempt to use global?How can I automatically replace subscripts and highlight GoTo labels?How to replace textHow can own commands be aware of global?How to do search-and-replace in a macro?how to use a global foreach?How to make NewDocumentCommand global?How to represent just space-character in replace stringreplace text with variables

How to deal with a team lead who never gives me credit?

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?

Why aren't air breathing engines used as small first stages

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

Why do people hide their license plates in the EU?

Can a USB port passively 'listen only'?

51k Euros annually for a family of 4 in Berlin: Is it enough?

How to find all the available tools in mac terminal?

prime numbers and expressing non-prime numbers

Overriding an object in memory with placement new

What exactly is a "Meth" in Altered Carbon?

3 doors, three guards, one stone

Can I cast Passwall to drop an enemy into a 20-foot pit?

Why am I getting the error "non-boolean type specified in a context where a condition is expected" for this request?

How to find out what spells would be useless to a blind NPC spellcaster?

Should I discuss the type of campaign with my players?

The logistics of corpse disposal

What LEGO pieces have "real-world" functionality?

What does this icon in iOS Stardew Valley mean?

How do I stop a creek from eroding my steep embankment?

What would be the ideal power source for a cybernetic eye?

Error "illegal generic type for instanceof" when using local classes

A coin, having probability p of landing heads and probability of q=(1-p) of landing on heads.

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?



How can I replace every global instance of “x[2]” with “x_2”



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Translate in-line equations to TeX code (Any Package?)Automatic LaTeX parsing to replace text with macrosWhat is going wrong with this attempt to use global?How can I automatically replace subscripts and highlight GoTo labels?How to replace textHow can own commands be aware of global?How to do search-and-replace in a macro?how to use a global foreach?How to make NewDocumentCommand global?How to represent just space-character in replace stringreplace text with variables










10















Thanks to Bruno for suggesting that for my specific case, where my results come from Mathematica, it's simply much easier to change the output in there using trivial replacement rules. However, this stands alone as a general question.



This is a slightly different question to what I've seen before, where I can use something like newcommandgagamma to make shortcuts of things using .



I have a series of LONG equations with stuff like x[2] or a[3] in them. I want to be able to always replace any instance of them with something else, like a command that takes a string and replaces it like something similar to



newcommandx[2]x_2



and



newcommanda[2]beta



Ideally, this would take account of the number inside, but I could easily just do it for all instances that arise. Any guidance would be great (my document class is Report)










share|improve this question
























  • Sort of sounds like translating math equations from one syntax to another, which reminded me of my very strange answer here: tex.stackexchange.com/questions/332012/…. Not that it will help you in the present case, without significant rework.

    – Steven B. Segletes
    Mar 26 at 15:46






  • 2





    Do you absolutely need to convert as you typeset, or can you convert your source? If the latter, do you think it would be possible to unambiguously define all the situations in which this syntax would occur?

    – Chris H
    Mar 26 at 15:48











  • @ChrisH that's exactly what I'm hoping for. I'm copying some parts of equations from Mathematica, and is written in terms of coefficients like x[2] for example, where I avoided subscripts in my code. I now want to save myself effort and simply define x[2] = x_2 in Latex, since it'll only ever occur within an equation.

    – Brad
    Mar 26 at 15:50






  • 3





    My editor of choice supports regex find/replace so I'd simply find x[([0-9])] and replace with x_$1 (on a copy of course)

    – Chris H
    Mar 26 at 15:54






  • 1





    @CarlWitthoft thank you for your comment. There are plenty of options for going about solving this, but just to keep everything localised within TeX I was looking for a somewhat simple method of achieving this, for applications even beyond this current issue. Alas, it has been solved now, and also in a multitude of methods. One could argue all night about which way is both, however, I certainly see no issue in solving it externally (which I happen to be doing now in Mathematica).

    – Brad
    Mar 27 at 13:16















10















Thanks to Bruno for suggesting that for my specific case, where my results come from Mathematica, it's simply much easier to change the output in there using trivial replacement rules. However, this stands alone as a general question.



This is a slightly different question to what I've seen before, where I can use something like newcommandgagamma to make shortcuts of things using .



I have a series of LONG equations with stuff like x[2] or a[3] in them. I want to be able to always replace any instance of them with something else, like a command that takes a string and replaces it like something similar to



newcommandx[2]x_2



and



newcommanda[2]beta



Ideally, this would take account of the number inside, but I could easily just do it for all instances that arise. Any guidance would be great (my document class is Report)










share|improve this question
























  • Sort of sounds like translating math equations from one syntax to another, which reminded me of my very strange answer here: tex.stackexchange.com/questions/332012/…. Not that it will help you in the present case, without significant rework.

    – Steven B. Segletes
    Mar 26 at 15:46






  • 2





    Do you absolutely need to convert as you typeset, or can you convert your source? If the latter, do you think it would be possible to unambiguously define all the situations in which this syntax would occur?

    – Chris H
    Mar 26 at 15:48











  • @ChrisH that's exactly what I'm hoping for. I'm copying some parts of equations from Mathematica, and is written in terms of coefficients like x[2] for example, where I avoided subscripts in my code. I now want to save myself effort and simply define x[2] = x_2 in Latex, since it'll only ever occur within an equation.

    – Brad
    Mar 26 at 15:50






  • 3





    My editor of choice supports regex find/replace so I'd simply find x[([0-9])] and replace with x_$1 (on a copy of course)

    – Chris H
    Mar 26 at 15:54






  • 1





    @CarlWitthoft thank you for your comment. There are plenty of options for going about solving this, but just to keep everything localised within TeX I was looking for a somewhat simple method of achieving this, for applications even beyond this current issue. Alas, it has been solved now, and also in a multitude of methods. One could argue all night about which way is both, however, I certainly see no issue in solving it externally (which I happen to be doing now in Mathematica).

    – Brad
    Mar 27 at 13:16













10












10








10








Thanks to Bruno for suggesting that for my specific case, where my results come from Mathematica, it's simply much easier to change the output in there using trivial replacement rules. However, this stands alone as a general question.



This is a slightly different question to what I've seen before, where I can use something like newcommandgagamma to make shortcuts of things using .



I have a series of LONG equations with stuff like x[2] or a[3] in them. I want to be able to always replace any instance of them with something else, like a command that takes a string and replaces it like something similar to



newcommandx[2]x_2



and



newcommanda[2]beta



Ideally, this would take account of the number inside, but I could easily just do it for all instances that arise. Any guidance would be great (my document class is Report)










share|improve this question
















Thanks to Bruno for suggesting that for my specific case, where my results come from Mathematica, it's simply much easier to change the output in there using trivial replacement rules. However, this stands alone as a general question.



This is a slightly different question to what I've seen before, where I can use something like newcommandgagamma to make shortcuts of things using .



I have a series of LONG equations with stuff like x[2] or a[3] in them. I want to be able to always replace any instance of them with something else, like a command that takes a string and replaces it like something similar to



newcommandx[2]x_2



and



newcommanda[2]beta



Ideally, this would take account of the number inside, but I could easily just do it for all instances that arise. Any guidance would be great (my document class is Report)







macros shorthands






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 11:37







Brad

















asked Mar 26 at 15:39









BradBrad

637




637












  • Sort of sounds like translating math equations from one syntax to another, which reminded me of my very strange answer here: tex.stackexchange.com/questions/332012/…. Not that it will help you in the present case, without significant rework.

    – Steven B. Segletes
    Mar 26 at 15:46






  • 2





    Do you absolutely need to convert as you typeset, or can you convert your source? If the latter, do you think it would be possible to unambiguously define all the situations in which this syntax would occur?

    – Chris H
    Mar 26 at 15:48











  • @ChrisH that's exactly what I'm hoping for. I'm copying some parts of equations from Mathematica, and is written in terms of coefficients like x[2] for example, where I avoided subscripts in my code. I now want to save myself effort and simply define x[2] = x_2 in Latex, since it'll only ever occur within an equation.

    – Brad
    Mar 26 at 15:50






  • 3





    My editor of choice supports regex find/replace so I'd simply find x[([0-9])] and replace with x_$1 (on a copy of course)

    – Chris H
    Mar 26 at 15:54






  • 1





    @CarlWitthoft thank you for your comment. There are plenty of options for going about solving this, but just to keep everything localised within TeX I was looking for a somewhat simple method of achieving this, for applications even beyond this current issue. Alas, it has been solved now, and also in a multitude of methods. One could argue all night about which way is both, however, I certainly see no issue in solving it externally (which I happen to be doing now in Mathematica).

    – Brad
    Mar 27 at 13:16

















  • Sort of sounds like translating math equations from one syntax to another, which reminded me of my very strange answer here: tex.stackexchange.com/questions/332012/…. Not that it will help you in the present case, without significant rework.

    – Steven B. Segletes
    Mar 26 at 15:46






  • 2





    Do you absolutely need to convert as you typeset, or can you convert your source? If the latter, do you think it would be possible to unambiguously define all the situations in which this syntax would occur?

    – Chris H
    Mar 26 at 15:48











  • @ChrisH that's exactly what I'm hoping for. I'm copying some parts of equations from Mathematica, and is written in terms of coefficients like x[2] for example, where I avoided subscripts in my code. I now want to save myself effort and simply define x[2] = x_2 in Latex, since it'll only ever occur within an equation.

    – Brad
    Mar 26 at 15:50






  • 3





    My editor of choice supports regex find/replace so I'd simply find x[([0-9])] and replace with x_$1 (on a copy of course)

    – Chris H
    Mar 26 at 15:54






  • 1





    @CarlWitthoft thank you for your comment. There are plenty of options for going about solving this, but just to keep everything localised within TeX I was looking for a somewhat simple method of achieving this, for applications even beyond this current issue. Alas, it has been solved now, and also in a multitude of methods. One could argue all night about which way is both, however, I certainly see no issue in solving it externally (which I happen to be doing now in Mathematica).

    – Brad
    Mar 27 at 13:16
















Sort of sounds like translating math equations from one syntax to another, which reminded me of my very strange answer here: tex.stackexchange.com/questions/332012/…. Not that it will help you in the present case, without significant rework.

– Steven B. Segletes
Mar 26 at 15:46





Sort of sounds like translating math equations from one syntax to another, which reminded me of my very strange answer here: tex.stackexchange.com/questions/332012/…. Not that it will help you in the present case, without significant rework.

– Steven B. Segletes
Mar 26 at 15:46




2




2





Do you absolutely need to convert as you typeset, or can you convert your source? If the latter, do you think it would be possible to unambiguously define all the situations in which this syntax would occur?

– Chris H
Mar 26 at 15:48





Do you absolutely need to convert as you typeset, or can you convert your source? If the latter, do you think it would be possible to unambiguously define all the situations in which this syntax would occur?

– Chris H
Mar 26 at 15:48













@ChrisH that's exactly what I'm hoping for. I'm copying some parts of equations from Mathematica, and is written in terms of coefficients like x[2] for example, where I avoided subscripts in my code. I now want to save myself effort and simply define x[2] = x_2 in Latex, since it'll only ever occur within an equation.

– Brad
Mar 26 at 15:50





@ChrisH that's exactly what I'm hoping for. I'm copying some parts of equations from Mathematica, and is written in terms of coefficients like x[2] for example, where I avoided subscripts in my code. I now want to save myself effort and simply define x[2] = x_2 in Latex, since it'll only ever occur within an equation.

– Brad
Mar 26 at 15:50




3




3





My editor of choice supports regex find/replace so I'd simply find x[([0-9])] and replace with x_$1 (on a copy of course)

– Chris H
Mar 26 at 15:54





My editor of choice supports regex find/replace so I'd simply find x[([0-9])] and replace with x_$1 (on a copy of course)

– Chris H
Mar 26 at 15:54




1




1





@CarlWitthoft thank you for your comment. There are plenty of options for going about solving this, but just to keep everything localised within TeX I was looking for a somewhat simple method of achieving this, for applications even beyond this current issue. Alas, it has been solved now, and also in a multitude of methods. One could argue all night about which way is both, however, I certainly see no issue in solving it externally (which I happen to be doing now in Mathematica).

– Brad
Mar 27 at 13:16





@CarlWitthoft thank you for your comment. There are plenty of options for going about solving this, but just to keep everything localised within TeX I was looking for a somewhat simple method of achieving this, for applications even beyond this current issue. Alas, it has been solved now, and also in a multitude of methods. One could argue all night about which way is both, however, I certainly see no issue in solving it externally (which I happen to be doing now in Mathematica).

– Brad
Mar 27 at 13:16










6 Answers
6






active

oldest

votes


















10














An extensible set of replacements:



documentclassarticle
usepackageamsmath
usepackagexparse

ExplSyntaxOn
NewDocumentCommandtransm

tl_set:Nn l__brad_trans_tl #1
regex_replace_all:nnN [(.*?)] csbcB1cE l__brad_trans_tl
regex_replace_all:nnN a calpha l__brad_trans_tl
% other replacements
% ...
% deliver the new token list
l__brad_trans_tl

ExplSyntaxOff

begindocument

$transx[1]+x[2]^2+a+a[3]$

enddocument


enter image description here






share|improve this answer


















  • 1





    Same comment as I made on David's answer. Yes, my l3regex is sometimes useful, but here the right solution by far is to do the replacements in Mathematica, not TeX.

    – Bruno Le Floch
    Mar 26 at 19:34


















9














Inasmuch as I love doing crazy things in TeX, do the replacements in Mathematica! This will save you a huge amount of pain. For instance,



expr = (a[1] + a[2])/Sqrt[a[3]]
expr /. a[1] -> [Alpha], a[2] -> [Beta], a[3] -> [Gamma] // TeXForm


gives fracalpha +beta sqrtgamma with no need to tweak the output.






share|improve this answer























  • This is actually much more efficient than I realised! I was specifically trying to avid all those dirty symbols in my code but that's a much better overall solution. Big +1, thank you for the advice! I have edited my question to reflect this. I assume one would need to replace x[2] with the full subscript notation to achieve the $x_2$ necessary as well?

    – Brad
    Mar 27 at 11:36












  • Yeah, it's just as easy to do the subscript replacements as well. Thank you for pointing this out Bruno; I will keep the answer above since I guess it does answer the specific question in particular, but I appreciate your solution greatly.

    – Brad
    Mar 27 at 11:45


















7














I really can not recommend doing this, but as you ask...



enter image description here



documentclassarticle

begindocument


mathcode`[="8000
mathcode`]="8000

catcode`[=active gdef[_bgroup
catcode`]=active gdef]egroup


hmmm
[
a[1]+a[2]+b[c+d[3]] + x
]

enddocument





share|improve this answer























  • Thank you for your answer - this is not quite what I'm after. But since this is not quite a conventional method, perhaps I am better simply going through and replacing things manually... I am moreso hoping to simply find a particular string and replace it with something else.

    – Brad
    Mar 26 at 16:02











  • @Brad that really isn't how tex works:-) (unless you are using luatex)

    – David Carlisle
    Mar 26 at 16:05











  • No worries at all! I have no problem sorting through everything, but I was hoping to save myself a bit of eyesight. Thank you for your help regardless!

    – Brad
    Mar 26 at 16:06











  • @DavidCarlisle Come on, don't advocate crazy solutions to problems that can be trivially solved on the other side of the conversion. Brad should simply do the replacement in Mathematica.

    – Bruno Le Floch
    Mar 26 at 19:33






  • 3





    @BrunoLeFloch I'd already said that in comments under the question, or just use perl or any other sane mechanism, but it's easy for you to see that this is beyond what's reasonable to do in tex as you know more than most on where tex's limits are, but I think it is still useful as a general rule to sketch how far you can get (and in particular what you can not reasonably do) in tex for this kind of question.

    – David Carlisle
    Mar 26 at 19:39


















5














Along the vein of my cited Translate in-line equations to TeX code (Any Package?), but a much simpler parsing request, I provide translate.



No catcode changes required. It will retain surrounding math style, etc.



documentclassarticle
usepackagelistofitems
newtokseqtoks
newcommandaddtoeqtoks[1]expandaftereqtoksexpandaftertheeqtoks#1
newcommandtranslate[1]%
setsepchar[%
readlistmyeqn#1%
eqtoks%
foreachitemxinmyeqn[]%
ifnumxcnt=1else%
if[myeqnsep[numexprxcnt-1]addtoeqtoks_bgroupfi%
if]myeqnsep[numexprxcnt-1]addtoeqtoksegroupfi%
fi%
expandafteraddtoeqtoksexpandafterx%
%
theeqtoks%

begindocument
[
translatea[1]+a[2]+b[c+d[3]] + x
]
enddocument


enter image description here






share|improve this answer






























    4














    For the sake of variety, here's a LuaLaTeX-based solution. It sets up a Lua function, called trans, which does most of the work with the help of Lua's versatile string-handling routines. The LaTeX macro trans is a wrapper that invokes the Lua function.



    enter image description here



    documentclassarticle
    usepackageluacode % for "luacode" environment and "luastringN" macro
    beginluacode


    function makesubscr ( s )
    s = s:gsub ( "(%b[])", function(x)
    return ( "_" .. string.sub ( x , 2 , -2 ) .. "" )
    end )
    if s:find ( "%b[]" ) then
    s = makesubscr ( s )
    end
    return s
    end
    function trans ( s )
    s = makesubscr ( s )
    s = s:gsub ( "a" , "\alpha" )
    s = s:gsub ( "b" , "\beta" )
    tex.sprint ( s )
    end


    endluacode

    %% TeX-side code
    newcommandtrans[1]directluatrans(luastringN#1)

    begindocument
    $transx[1]+x[2]^2+a+a[3]$

    $transa[1]+a[2]+b[c+d[3]]+x$
    enddocument





    share|improve this answer






























      2














      Here's a solution. The command addreplacementruletext-to-replacereplacement-text specifies replacements to make. Then the command replacesome math does the replacements.



      This would probably be better done with the l3regex package or really find and replace as many other people mentioned.



      documentclassarticle
      makeatletter
      newtoksri@activechars
      defaddreplacementrule#1#2addreplacementrule@#1@nil#2
      defaddreplacementrule@#1#2@nil#3%
      ri@activecharsexpandaftertheri@activechars\#1%
      ri@maketransitions#2@nil#1#3%


      defreplace#1%
      begingroup
      def\##1%
      bgrouplccode`~=`##1lowercaseegroup
      def~ri@continue##1%
      %
      catcode`##1=active
      %
      theri@activechars
      scantokens#1%
      endgroup


      defri@maketransitions#1#2@nil#3#4%
      deftemp#2%
      ifxtempempty
      expandafterdefcsname ri@lookup@detokenize#3#1endcsname#4%
      letnextrelax
      else
      expandafterdefcsname ri@lookup@detokenize#3#1endcsnameri@continue#3#1%
      defnextri@maketransitions#2@nil#3#1#4%
      fi
      next



      defri@continue#1#2%
      @ifundefinedri@lookup@detokenize#1#2%
      detokenize#1#2%
      %
      csname ri@lookup@detokenize#1#2endcsname
      %



      begindocument
      addreplacementrulex[2]x_2
      addreplacementrulex[3]x^2

      $x+1+x[2]+x[3]+x[1]$

      replace$x+1+x[2]+x[3]+x[1]$
      enddocument


      enter image description here






      share|improve this answer

























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "85"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f481546%2fhow-can-i-replace-every-global-instance-of-x2-with-x-2%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10














        An extensible set of replacements:



        documentclassarticle
        usepackageamsmath
        usepackagexparse

        ExplSyntaxOn
        NewDocumentCommandtransm

        tl_set:Nn l__brad_trans_tl #1
        regex_replace_all:nnN [(.*?)] csbcB1cE l__brad_trans_tl
        regex_replace_all:nnN a calpha l__brad_trans_tl
        % other replacements
        % ...
        % deliver the new token list
        l__brad_trans_tl

        ExplSyntaxOff

        begindocument

        $transx[1]+x[2]^2+a+a[3]$

        enddocument


        enter image description here






        share|improve this answer


















        • 1





          Same comment as I made on David's answer. Yes, my l3regex is sometimes useful, but here the right solution by far is to do the replacements in Mathematica, not TeX.

          – Bruno Le Floch
          Mar 26 at 19:34















        10














        An extensible set of replacements:



        documentclassarticle
        usepackageamsmath
        usepackagexparse

        ExplSyntaxOn
        NewDocumentCommandtransm

        tl_set:Nn l__brad_trans_tl #1
        regex_replace_all:nnN [(.*?)] csbcB1cE l__brad_trans_tl
        regex_replace_all:nnN a calpha l__brad_trans_tl
        % other replacements
        % ...
        % deliver the new token list
        l__brad_trans_tl

        ExplSyntaxOff

        begindocument

        $transx[1]+x[2]^2+a+a[3]$

        enddocument


        enter image description here






        share|improve this answer


















        • 1





          Same comment as I made on David's answer. Yes, my l3regex is sometimes useful, but here the right solution by far is to do the replacements in Mathematica, not TeX.

          – Bruno Le Floch
          Mar 26 at 19:34













        10












        10








        10







        An extensible set of replacements:



        documentclassarticle
        usepackageamsmath
        usepackagexparse

        ExplSyntaxOn
        NewDocumentCommandtransm

        tl_set:Nn l__brad_trans_tl #1
        regex_replace_all:nnN [(.*?)] csbcB1cE l__brad_trans_tl
        regex_replace_all:nnN a calpha l__brad_trans_tl
        % other replacements
        % ...
        % deliver the new token list
        l__brad_trans_tl

        ExplSyntaxOff

        begindocument

        $transx[1]+x[2]^2+a+a[3]$

        enddocument


        enter image description here






        share|improve this answer













        An extensible set of replacements:



        documentclassarticle
        usepackageamsmath
        usepackagexparse

        ExplSyntaxOn
        NewDocumentCommandtransm

        tl_set:Nn l__brad_trans_tl #1
        regex_replace_all:nnN [(.*?)] csbcB1cE l__brad_trans_tl
        regex_replace_all:nnN a calpha l__brad_trans_tl
        % other replacements
        % ...
        % deliver the new token list
        l__brad_trans_tl

        ExplSyntaxOff

        begindocument

        $transx[1]+x[2]^2+a+a[3]$

        enddocument


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 18:09









        egregegreg

        734k8919333257




        734k8919333257







        • 1





          Same comment as I made on David's answer. Yes, my l3regex is sometimes useful, but here the right solution by far is to do the replacements in Mathematica, not TeX.

          – Bruno Le Floch
          Mar 26 at 19:34












        • 1





          Same comment as I made on David's answer. Yes, my l3regex is sometimes useful, but here the right solution by far is to do the replacements in Mathematica, not TeX.

          – Bruno Le Floch
          Mar 26 at 19:34







        1




        1





        Same comment as I made on David's answer. Yes, my l3regex is sometimes useful, but here the right solution by far is to do the replacements in Mathematica, not TeX.

        – Bruno Le Floch
        Mar 26 at 19:34





        Same comment as I made on David's answer. Yes, my l3regex is sometimes useful, but here the right solution by far is to do the replacements in Mathematica, not TeX.

        – Bruno Le Floch
        Mar 26 at 19:34











        9














        Inasmuch as I love doing crazy things in TeX, do the replacements in Mathematica! This will save you a huge amount of pain. For instance,



        expr = (a[1] + a[2])/Sqrt[a[3]]
        expr /. a[1] -> [Alpha], a[2] -> [Beta], a[3] -> [Gamma] // TeXForm


        gives fracalpha +beta sqrtgamma with no need to tweak the output.






        share|improve this answer























        • This is actually much more efficient than I realised! I was specifically trying to avid all those dirty symbols in my code but that's a much better overall solution. Big +1, thank you for the advice! I have edited my question to reflect this. I assume one would need to replace x[2] with the full subscript notation to achieve the $x_2$ necessary as well?

          – Brad
          Mar 27 at 11:36












        • Yeah, it's just as easy to do the subscript replacements as well. Thank you for pointing this out Bruno; I will keep the answer above since I guess it does answer the specific question in particular, but I appreciate your solution greatly.

          – Brad
          Mar 27 at 11:45















        9














        Inasmuch as I love doing crazy things in TeX, do the replacements in Mathematica! This will save you a huge amount of pain. For instance,



        expr = (a[1] + a[2])/Sqrt[a[3]]
        expr /. a[1] -> [Alpha], a[2] -> [Beta], a[3] -> [Gamma] // TeXForm


        gives fracalpha +beta sqrtgamma with no need to tweak the output.






        share|improve this answer























        • This is actually much more efficient than I realised! I was specifically trying to avid all those dirty symbols in my code but that's a much better overall solution. Big +1, thank you for the advice! I have edited my question to reflect this. I assume one would need to replace x[2] with the full subscript notation to achieve the $x_2$ necessary as well?

          – Brad
          Mar 27 at 11:36












        • Yeah, it's just as easy to do the subscript replacements as well. Thank you for pointing this out Bruno; I will keep the answer above since I guess it does answer the specific question in particular, but I appreciate your solution greatly.

          – Brad
          Mar 27 at 11:45













        9












        9








        9







        Inasmuch as I love doing crazy things in TeX, do the replacements in Mathematica! This will save you a huge amount of pain. For instance,



        expr = (a[1] + a[2])/Sqrt[a[3]]
        expr /. a[1] -> [Alpha], a[2] -> [Beta], a[3] -> [Gamma] // TeXForm


        gives fracalpha +beta sqrtgamma with no need to tweak the output.






        share|improve this answer













        Inasmuch as I love doing crazy things in TeX, do the replacements in Mathematica! This will save you a huge amount of pain. For instance,



        expr = (a[1] + a[2])/Sqrt[a[3]]
        expr /. a[1] -> [Alpha], a[2] -> [Beta], a[3] -> [Gamma] // TeXForm


        gives fracalpha +beta sqrtgamma with no need to tweak the output.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 19:32









        Bruno Le FlochBruno Le Floch

        34.3k5115211




        34.3k5115211












        • This is actually much more efficient than I realised! I was specifically trying to avid all those dirty symbols in my code but that's a much better overall solution. Big +1, thank you for the advice! I have edited my question to reflect this. I assume one would need to replace x[2] with the full subscript notation to achieve the $x_2$ necessary as well?

          – Brad
          Mar 27 at 11:36












        • Yeah, it's just as easy to do the subscript replacements as well. Thank you for pointing this out Bruno; I will keep the answer above since I guess it does answer the specific question in particular, but I appreciate your solution greatly.

          – Brad
          Mar 27 at 11:45

















        • This is actually much more efficient than I realised! I was specifically trying to avid all those dirty symbols in my code but that's a much better overall solution. Big +1, thank you for the advice! I have edited my question to reflect this. I assume one would need to replace x[2] with the full subscript notation to achieve the $x_2$ necessary as well?

          – Brad
          Mar 27 at 11:36












        • Yeah, it's just as easy to do the subscript replacements as well. Thank you for pointing this out Bruno; I will keep the answer above since I guess it does answer the specific question in particular, but I appreciate your solution greatly.

          – Brad
          Mar 27 at 11:45
















        This is actually much more efficient than I realised! I was specifically trying to avid all those dirty symbols in my code but that's a much better overall solution. Big +1, thank you for the advice! I have edited my question to reflect this. I assume one would need to replace x[2] with the full subscript notation to achieve the $x_2$ necessary as well?

        – Brad
        Mar 27 at 11:36






        This is actually much more efficient than I realised! I was specifically trying to avid all those dirty symbols in my code but that's a much better overall solution. Big +1, thank you for the advice! I have edited my question to reflect this. I assume one would need to replace x[2] with the full subscript notation to achieve the $x_2$ necessary as well?

        – Brad
        Mar 27 at 11:36














        Yeah, it's just as easy to do the subscript replacements as well. Thank you for pointing this out Bruno; I will keep the answer above since I guess it does answer the specific question in particular, but I appreciate your solution greatly.

        – Brad
        Mar 27 at 11:45





        Yeah, it's just as easy to do the subscript replacements as well. Thank you for pointing this out Bruno; I will keep the answer above since I guess it does answer the specific question in particular, but I appreciate your solution greatly.

        – Brad
        Mar 27 at 11:45











        7














        I really can not recommend doing this, but as you ask...



        enter image description here



        documentclassarticle

        begindocument


        mathcode`[="8000
        mathcode`]="8000

        catcode`[=active gdef[_bgroup
        catcode`]=active gdef]egroup


        hmmm
        [
        a[1]+a[2]+b[c+d[3]] + x
        ]

        enddocument





        share|improve this answer























        • Thank you for your answer - this is not quite what I'm after. But since this is not quite a conventional method, perhaps I am better simply going through and replacing things manually... I am moreso hoping to simply find a particular string and replace it with something else.

          – Brad
          Mar 26 at 16:02











        • @Brad that really isn't how tex works:-) (unless you are using luatex)

          – David Carlisle
          Mar 26 at 16:05











        • No worries at all! I have no problem sorting through everything, but I was hoping to save myself a bit of eyesight. Thank you for your help regardless!

          – Brad
          Mar 26 at 16:06











        • @DavidCarlisle Come on, don't advocate crazy solutions to problems that can be trivially solved on the other side of the conversion. Brad should simply do the replacement in Mathematica.

          – Bruno Le Floch
          Mar 26 at 19:33






        • 3





          @BrunoLeFloch I'd already said that in comments under the question, or just use perl or any other sane mechanism, but it's easy for you to see that this is beyond what's reasonable to do in tex as you know more than most on where tex's limits are, but I think it is still useful as a general rule to sketch how far you can get (and in particular what you can not reasonably do) in tex for this kind of question.

          – David Carlisle
          Mar 26 at 19:39















        7














        I really can not recommend doing this, but as you ask...



        enter image description here



        documentclassarticle

        begindocument


        mathcode`[="8000
        mathcode`]="8000

        catcode`[=active gdef[_bgroup
        catcode`]=active gdef]egroup


        hmmm
        [
        a[1]+a[2]+b[c+d[3]] + x
        ]

        enddocument





        share|improve this answer























        • Thank you for your answer - this is not quite what I'm after. But since this is not quite a conventional method, perhaps I am better simply going through and replacing things manually... I am moreso hoping to simply find a particular string and replace it with something else.

          – Brad
          Mar 26 at 16:02











        • @Brad that really isn't how tex works:-) (unless you are using luatex)

          – David Carlisle
          Mar 26 at 16:05











        • No worries at all! I have no problem sorting through everything, but I was hoping to save myself a bit of eyesight. Thank you for your help regardless!

          – Brad
          Mar 26 at 16:06











        • @DavidCarlisle Come on, don't advocate crazy solutions to problems that can be trivially solved on the other side of the conversion. Brad should simply do the replacement in Mathematica.

          – Bruno Le Floch
          Mar 26 at 19:33






        • 3





          @BrunoLeFloch I'd already said that in comments under the question, or just use perl or any other sane mechanism, but it's easy for you to see that this is beyond what's reasonable to do in tex as you know more than most on where tex's limits are, but I think it is still useful as a general rule to sketch how far you can get (and in particular what you can not reasonably do) in tex for this kind of question.

          – David Carlisle
          Mar 26 at 19:39













        7












        7








        7







        I really can not recommend doing this, but as you ask...



        enter image description here



        documentclassarticle

        begindocument


        mathcode`[="8000
        mathcode`]="8000

        catcode`[=active gdef[_bgroup
        catcode`]=active gdef]egroup


        hmmm
        [
        a[1]+a[2]+b[c+d[3]] + x
        ]

        enddocument





        share|improve this answer













        I really can not recommend doing this, but as you ask...



        enter image description here



        documentclassarticle

        begindocument


        mathcode`[="8000
        mathcode`]="8000

        catcode`[=active gdef[_bgroup
        catcode`]=active gdef]egroup


        hmmm
        [
        a[1]+a[2]+b[c+d[3]] + x
        ]

        enddocument






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 15:57









        David CarlisleDavid Carlisle

        499k4111451896




        499k4111451896












        • Thank you for your answer - this is not quite what I'm after. But since this is not quite a conventional method, perhaps I am better simply going through and replacing things manually... I am moreso hoping to simply find a particular string and replace it with something else.

          – Brad
          Mar 26 at 16:02











        • @Brad that really isn't how tex works:-) (unless you are using luatex)

          – David Carlisle
          Mar 26 at 16:05











        • No worries at all! I have no problem sorting through everything, but I was hoping to save myself a bit of eyesight. Thank you for your help regardless!

          – Brad
          Mar 26 at 16:06











        • @DavidCarlisle Come on, don't advocate crazy solutions to problems that can be trivially solved on the other side of the conversion. Brad should simply do the replacement in Mathematica.

          – Bruno Le Floch
          Mar 26 at 19:33






        • 3





          @BrunoLeFloch I'd already said that in comments under the question, or just use perl or any other sane mechanism, but it's easy for you to see that this is beyond what's reasonable to do in tex as you know more than most on where tex's limits are, but I think it is still useful as a general rule to sketch how far you can get (and in particular what you can not reasonably do) in tex for this kind of question.

          – David Carlisle
          Mar 26 at 19:39

















        • Thank you for your answer - this is not quite what I'm after. But since this is not quite a conventional method, perhaps I am better simply going through and replacing things manually... I am moreso hoping to simply find a particular string and replace it with something else.

          – Brad
          Mar 26 at 16:02











        • @Brad that really isn't how tex works:-) (unless you are using luatex)

          – David Carlisle
          Mar 26 at 16:05











        • No worries at all! I have no problem sorting through everything, but I was hoping to save myself a bit of eyesight. Thank you for your help regardless!

          – Brad
          Mar 26 at 16:06











        • @DavidCarlisle Come on, don't advocate crazy solutions to problems that can be trivially solved on the other side of the conversion. Brad should simply do the replacement in Mathematica.

          – Bruno Le Floch
          Mar 26 at 19:33






        • 3





          @BrunoLeFloch I'd already said that in comments under the question, or just use perl or any other sane mechanism, but it's easy for you to see that this is beyond what's reasonable to do in tex as you know more than most on where tex's limits are, but I think it is still useful as a general rule to sketch how far you can get (and in particular what you can not reasonably do) in tex for this kind of question.

          – David Carlisle
          Mar 26 at 19:39
















        Thank you for your answer - this is not quite what I'm after. But since this is not quite a conventional method, perhaps I am better simply going through and replacing things manually... I am moreso hoping to simply find a particular string and replace it with something else.

        – Brad
        Mar 26 at 16:02





        Thank you for your answer - this is not quite what I'm after. But since this is not quite a conventional method, perhaps I am better simply going through and replacing things manually... I am moreso hoping to simply find a particular string and replace it with something else.

        – Brad
        Mar 26 at 16:02













        @Brad that really isn't how tex works:-) (unless you are using luatex)

        – David Carlisle
        Mar 26 at 16:05





        @Brad that really isn't how tex works:-) (unless you are using luatex)

        – David Carlisle
        Mar 26 at 16:05













        No worries at all! I have no problem sorting through everything, but I was hoping to save myself a bit of eyesight. Thank you for your help regardless!

        – Brad
        Mar 26 at 16:06





        No worries at all! I have no problem sorting through everything, but I was hoping to save myself a bit of eyesight. Thank you for your help regardless!

        – Brad
        Mar 26 at 16:06













        @DavidCarlisle Come on, don't advocate crazy solutions to problems that can be trivially solved on the other side of the conversion. Brad should simply do the replacement in Mathematica.

        – Bruno Le Floch
        Mar 26 at 19:33





        @DavidCarlisle Come on, don't advocate crazy solutions to problems that can be trivially solved on the other side of the conversion. Brad should simply do the replacement in Mathematica.

        – Bruno Le Floch
        Mar 26 at 19:33




        3




        3





        @BrunoLeFloch I'd already said that in comments under the question, or just use perl or any other sane mechanism, but it's easy for you to see that this is beyond what's reasonable to do in tex as you know more than most on where tex's limits are, but I think it is still useful as a general rule to sketch how far you can get (and in particular what you can not reasonably do) in tex for this kind of question.

        – David Carlisle
        Mar 26 at 19:39





        @BrunoLeFloch I'd already said that in comments under the question, or just use perl or any other sane mechanism, but it's easy for you to see that this is beyond what's reasonable to do in tex as you know more than most on where tex's limits are, but I think it is still useful as a general rule to sketch how far you can get (and in particular what you can not reasonably do) in tex for this kind of question.

        – David Carlisle
        Mar 26 at 19:39











        5














        Along the vein of my cited Translate in-line equations to TeX code (Any Package?), but a much simpler parsing request, I provide translate.



        No catcode changes required. It will retain surrounding math style, etc.



        documentclassarticle
        usepackagelistofitems
        newtokseqtoks
        newcommandaddtoeqtoks[1]expandaftereqtoksexpandaftertheeqtoks#1
        newcommandtranslate[1]%
        setsepchar[%
        readlistmyeqn#1%
        eqtoks%
        foreachitemxinmyeqn[]%
        ifnumxcnt=1else%
        if[myeqnsep[numexprxcnt-1]addtoeqtoks_bgroupfi%
        if]myeqnsep[numexprxcnt-1]addtoeqtoksegroupfi%
        fi%
        expandafteraddtoeqtoksexpandafterx%
        %
        theeqtoks%

        begindocument
        [
        translatea[1]+a[2]+b[c+d[3]] + x
        ]
        enddocument


        enter image description here






        share|improve this answer



























          5














          Along the vein of my cited Translate in-line equations to TeX code (Any Package?), but a much simpler parsing request, I provide translate.



          No catcode changes required. It will retain surrounding math style, etc.



          documentclassarticle
          usepackagelistofitems
          newtokseqtoks
          newcommandaddtoeqtoks[1]expandaftereqtoksexpandaftertheeqtoks#1
          newcommandtranslate[1]%
          setsepchar[%
          readlistmyeqn#1%
          eqtoks%
          foreachitemxinmyeqn[]%
          ifnumxcnt=1else%
          if[myeqnsep[numexprxcnt-1]addtoeqtoks_bgroupfi%
          if]myeqnsep[numexprxcnt-1]addtoeqtoksegroupfi%
          fi%
          expandafteraddtoeqtoksexpandafterx%
          %
          theeqtoks%

          begindocument
          [
          translatea[1]+a[2]+b[c+d[3]] + x
          ]
          enddocument


          enter image description here






          share|improve this answer

























            5












            5








            5







            Along the vein of my cited Translate in-line equations to TeX code (Any Package?), but a much simpler parsing request, I provide translate.



            No catcode changes required. It will retain surrounding math style, etc.



            documentclassarticle
            usepackagelistofitems
            newtokseqtoks
            newcommandaddtoeqtoks[1]expandaftereqtoksexpandaftertheeqtoks#1
            newcommandtranslate[1]%
            setsepchar[%
            readlistmyeqn#1%
            eqtoks%
            foreachitemxinmyeqn[]%
            ifnumxcnt=1else%
            if[myeqnsep[numexprxcnt-1]addtoeqtoks_bgroupfi%
            if]myeqnsep[numexprxcnt-1]addtoeqtoksegroupfi%
            fi%
            expandafteraddtoeqtoksexpandafterx%
            %
            theeqtoks%

            begindocument
            [
            translatea[1]+a[2]+b[c+d[3]] + x
            ]
            enddocument


            enter image description here






            share|improve this answer













            Along the vein of my cited Translate in-line equations to TeX code (Any Package?), but a much simpler parsing request, I provide translate.



            No catcode changes required. It will retain surrounding math style, etc.



            documentclassarticle
            usepackagelistofitems
            newtokseqtoks
            newcommandaddtoeqtoks[1]expandaftereqtoksexpandaftertheeqtoks#1
            newcommandtranslate[1]%
            setsepchar[%
            readlistmyeqn#1%
            eqtoks%
            foreachitemxinmyeqn[]%
            ifnumxcnt=1else%
            if[myeqnsep[numexprxcnt-1]addtoeqtoks_bgroupfi%
            if]myeqnsep[numexprxcnt-1]addtoeqtoksegroupfi%
            fi%
            expandafteraddtoeqtoksexpandafterx%
            %
            theeqtoks%

            begindocument
            [
            translatea[1]+a[2]+b[c+d[3]] + x
            ]
            enddocument


            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 26 at 16:21









            Steven B. SegletesSteven B. Segletes

            162k9206418




            162k9206418





















                4














                For the sake of variety, here's a LuaLaTeX-based solution. It sets up a Lua function, called trans, which does most of the work with the help of Lua's versatile string-handling routines. The LaTeX macro trans is a wrapper that invokes the Lua function.



                enter image description here



                documentclassarticle
                usepackageluacode % for "luacode" environment and "luastringN" macro
                beginluacode


                function makesubscr ( s )
                s = s:gsub ( "(%b[])", function(x)
                return ( "_" .. string.sub ( x , 2 , -2 ) .. "" )
                end )
                if s:find ( "%b[]" ) then
                s = makesubscr ( s )
                end
                return s
                end
                function trans ( s )
                s = makesubscr ( s )
                s = s:gsub ( "a" , "\alpha" )
                s = s:gsub ( "b" , "\beta" )
                tex.sprint ( s )
                end


                endluacode

                %% TeX-side code
                newcommandtrans[1]directluatrans(luastringN#1)

                begindocument
                $transx[1]+x[2]^2+a+a[3]$

                $transa[1]+a[2]+b[c+d[3]]+x$
                enddocument





                share|improve this answer



























                  4














                  For the sake of variety, here's a LuaLaTeX-based solution. It sets up a Lua function, called trans, which does most of the work with the help of Lua's versatile string-handling routines. The LaTeX macro trans is a wrapper that invokes the Lua function.



                  enter image description here



                  documentclassarticle
                  usepackageluacode % for "luacode" environment and "luastringN" macro
                  beginluacode


                  function makesubscr ( s )
                  s = s:gsub ( "(%b[])", function(x)
                  return ( "_" .. string.sub ( x , 2 , -2 ) .. "" )
                  end )
                  if s:find ( "%b[]" ) then
                  s = makesubscr ( s )
                  end
                  return s
                  end
                  function trans ( s )
                  s = makesubscr ( s )
                  s = s:gsub ( "a" , "\alpha" )
                  s = s:gsub ( "b" , "\beta" )
                  tex.sprint ( s )
                  end


                  endluacode

                  %% TeX-side code
                  newcommandtrans[1]directluatrans(luastringN#1)

                  begindocument
                  $transx[1]+x[2]^2+a+a[3]$

                  $transa[1]+a[2]+b[c+d[3]]+x$
                  enddocument





                  share|improve this answer

























                    4












                    4








                    4







                    For the sake of variety, here's a LuaLaTeX-based solution. It sets up a Lua function, called trans, which does most of the work with the help of Lua's versatile string-handling routines. The LaTeX macro trans is a wrapper that invokes the Lua function.



                    enter image description here



                    documentclassarticle
                    usepackageluacode % for "luacode" environment and "luastringN" macro
                    beginluacode


                    function makesubscr ( s )
                    s = s:gsub ( "(%b[])", function(x)
                    return ( "_" .. string.sub ( x , 2 , -2 ) .. "" )
                    end )
                    if s:find ( "%b[]" ) then
                    s = makesubscr ( s )
                    end
                    return s
                    end
                    function trans ( s )
                    s = makesubscr ( s )
                    s = s:gsub ( "a" , "\alpha" )
                    s = s:gsub ( "b" , "\beta" )
                    tex.sprint ( s )
                    end


                    endluacode

                    %% TeX-side code
                    newcommandtrans[1]directluatrans(luastringN#1)

                    begindocument
                    $transx[1]+x[2]^2+a+a[3]$

                    $transa[1]+a[2]+b[c+d[3]]+x$
                    enddocument





                    share|improve this answer













                    For the sake of variety, here's a LuaLaTeX-based solution. It sets up a Lua function, called trans, which does most of the work with the help of Lua's versatile string-handling routines. The LaTeX macro trans is a wrapper that invokes the Lua function.



                    enter image description here



                    documentclassarticle
                    usepackageluacode % for "luacode" environment and "luastringN" macro
                    beginluacode


                    function makesubscr ( s )
                    s = s:gsub ( "(%b[])", function(x)
                    return ( "_" .. string.sub ( x , 2 , -2 ) .. "" )
                    end )
                    if s:find ( "%b[]" ) then
                    s = makesubscr ( s )
                    end
                    return s
                    end
                    function trans ( s )
                    s = makesubscr ( s )
                    s = s:gsub ( "a" , "\alpha" )
                    s = s:gsub ( "b" , "\beta" )
                    tex.sprint ( s )
                    end


                    endluacode

                    %% TeX-side code
                    newcommandtrans[1]directluatrans(luastringN#1)

                    begindocument
                    $transx[1]+x[2]^2+a+a[3]$

                    $transa[1]+a[2]+b[c+d[3]]+x$
                    enddocument






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 27 at 7:02









                    MicoMico

                    287k32392781




                    287k32392781





















                        2














                        Here's a solution. The command addreplacementruletext-to-replacereplacement-text specifies replacements to make. Then the command replacesome math does the replacements.



                        This would probably be better done with the l3regex package or really find and replace as many other people mentioned.



                        documentclassarticle
                        makeatletter
                        newtoksri@activechars
                        defaddreplacementrule#1#2addreplacementrule@#1@nil#2
                        defaddreplacementrule@#1#2@nil#3%
                        ri@activecharsexpandaftertheri@activechars\#1%
                        ri@maketransitions#2@nil#1#3%


                        defreplace#1%
                        begingroup
                        def\##1%
                        bgrouplccode`~=`##1lowercaseegroup
                        def~ri@continue##1%
                        %
                        catcode`##1=active
                        %
                        theri@activechars
                        scantokens#1%
                        endgroup


                        defri@maketransitions#1#2@nil#3#4%
                        deftemp#2%
                        ifxtempempty
                        expandafterdefcsname ri@lookup@detokenize#3#1endcsname#4%
                        letnextrelax
                        else
                        expandafterdefcsname ri@lookup@detokenize#3#1endcsnameri@continue#3#1%
                        defnextri@maketransitions#2@nil#3#1#4%
                        fi
                        next



                        defri@continue#1#2%
                        @ifundefinedri@lookup@detokenize#1#2%
                        detokenize#1#2%
                        %
                        csname ri@lookup@detokenize#1#2endcsname
                        %



                        begindocument
                        addreplacementrulex[2]x_2
                        addreplacementrulex[3]x^2

                        $x+1+x[2]+x[3]+x[1]$

                        replace$x+1+x[2]+x[3]+x[1]$
                        enddocument


                        enter image description here






                        share|improve this answer





























                          2














                          Here's a solution. The command addreplacementruletext-to-replacereplacement-text specifies replacements to make. Then the command replacesome math does the replacements.



                          This would probably be better done with the l3regex package or really find and replace as many other people mentioned.



                          documentclassarticle
                          makeatletter
                          newtoksri@activechars
                          defaddreplacementrule#1#2addreplacementrule@#1@nil#2
                          defaddreplacementrule@#1#2@nil#3%
                          ri@activecharsexpandaftertheri@activechars\#1%
                          ri@maketransitions#2@nil#1#3%


                          defreplace#1%
                          begingroup
                          def\##1%
                          bgrouplccode`~=`##1lowercaseegroup
                          def~ri@continue##1%
                          %
                          catcode`##1=active
                          %
                          theri@activechars
                          scantokens#1%
                          endgroup


                          defri@maketransitions#1#2@nil#3#4%
                          deftemp#2%
                          ifxtempempty
                          expandafterdefcsname ri@lookup@detokenize#3#1endcsname#4%
                          letnextrelax
                          else
                          expandafterdefcsname ri@lookup@detokenize#3#1endcsnameri@continue#3#1%
                          defnextri@maketransitions#2@nil#3#1#4%
                          fi
                          next



                          defri@continue#1#2%
                          @ifundefinedri@lookup@detokenize#1#2%
                          detokenize#1#2%
                          %
                          csname ri@lookup@detokenize#1#2endcsname
                          %



                          begindocument
                          addreplacementrulex[2]x_2
                          addreplacementrulex[3]x^2

                          $x+1+x[2]+x[3]+x[1]$

                          replace$x+1+x[2]+x[3]+x[1]$
                          enddocument


                          enter image description here






                          share|improve this answer



























                            2












                            2








                            2







                            Here's a solution. The command addreplacementruletext-to-replacereplacement-text specifies replacements to make. Then the command replacesome math does the replacements.



                            This would probably be better done with the l3regex package or really find and replace as many other people mentioned.



                            documentclassarticle
                            makeatletter
                            newtoksri@activechars
                            defaddreplacementrule#1#2addreplacementrule@#1@nil#2
                            defaddreplacementrule@#1#2@nil#3%
                            ri@activecharsexpandaftertheri@activechars\#1%
                            ri@maketransitions#2@nil#1#3%


                            defreplace#1%
                            begingroup
                            def\##1%
                            bgrouplccode`~=`##1lowercaseegroup
                            def~ri@continue##1%
                            %
                            catcode`##1=active
                            %
                            theri@activechars
                            scantokens#1%
                            endgroup


                            defri@maketransitions#1#2@nil#3#4%
                            deftemp#2%
                            ifxtempempty
                            expandafterdefcsname ri@lookup@detokenize#3#1endcsname#4%
                            letnextrelax
                            else
                            expandafterdefcsname ri@lookup@detokenize#3#1endcsnameri@continue#3#1%
                            defnextri@maketransitions#2@nil#3#1#4%
                            fi
                            next



                            defri@continue#1#2%
                            @ifundefinedri@lookup@detokenize#1#2%
                            detokenize#1#2%
                            %
                            csname ri@lookup@detokenize#1#2endcsname
                            %



                            begindocument
                            addreplacementrulex[2]x_2
                            addreplacementrulex[3]x^2

                            $x+1+x[2]+x[3]+x[1]$

                            replace$x+1+x[2]+x[3]+x[1]$
                            enddocument


                            enter image description here






                            share|improve this answer















                            Here's a solution. The command addreplacementruletext-to-replacereplacement-text specifies replacements to make. Then the command replacesome math does the replacements.



                            This would probably be better done with the l3regex package or really find and replace as many other people mentioned.



                            documentclassarticle
                            makeatletter
                            newtoksri@activechars
                            defaddreplacementrule#1#2addreplacementrule@#1@nil#2
                            defaddreplacementrule@#1#2@nil#3%
                            ri@activecharsexpandaftertheri@activechars\#1%
                            ri@maketransitions#2@nil#1#3%


                            defreplace#1%
                            begingroup
                            def\##1%
                            bgrouplccode`~=`##1lowercaseegroup
                            def~ri@continue##1%
                            %
                            catcode`##1=active
                            %
                            theri@activechars
                            scantokens#1%
                            endgroup


                            defri@maketransitions#1#2@nil#3#4%
                            deftemp#2%
                            ifxtempempty
                            expandafterdefcsname ri@lookup@detokenize#3#1endcsname#4%
                            letnextrelax
                            else
                            expandafterdefcsname ri@lookup@detokenize#3#1endcsnameri@continue#3#1%
                            defnextri@maketransitions#2@nil#3#1#4%
                            fi
                            next



                            defri@continue#1#2%
                            @ifundefinedri@lookup@detokenize#1#2%
                            detokenize#1#2%
                            %
                            csname ri@lookup@detokenize#1#2endcsname
                            %



                            begindocument
                            addreplacementrulex[2]x_2
                            addreplacementrulex[3]x^2

                            $x+1+x[2]+x[3]+x[1]$

                            replace$x+1+x[2]+x[3]+x[1]$
                            enddocument


                            enter image description here







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 26 at 16:49

























                            answered Mar 26 at 16:40









                            Hood ChathamHood Chatham

                            4,3841428




                            4,3841428



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f481546%2fhow-can-i-replace-every-global-instance-of-x2-with-x-2%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Lowndes Grove History Architecture References Navigation menu32°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661132°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661178002500"National Register Information System"Historic houses of South Carolina"Lowndes Grove""+32° 48' 6.00", −79° 57' 58.00""Lowndes Grove, Charleston County (260 St. Margaret St., Charleston)""Lowndes Grove"The Charleston ExpositionIt Happened in South Carolina"Lowndes Grove (House), Saint Margaret Street & Sixth Avenue, Charleston, Charleston County, SC(Photographs)"Plantations of the Carolina Low Countrye

                                random experiment with two different functions on unit interval Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Random variable and probability space notionsRandom Walk with EdgesFinding functions where the increase over a random interval is Poisson distributedNumber of days until dayCan an observed event in fact be of zero probability?Unit random processmodels of coins and uniform distributionHow to get the number of successes given $n$ trials , probability $P$ and a random variable $X$Absorbing Markov chain in a computer. Is “almost every” turned into always convergence in computer executions?Stopped random walk is not uniformly integrable

                                How should I support this large drywall patch? Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How do I cover large gaps in drywall?How do I keep drywall around a patch from crumbling?Can I glue a second layer of drywall?How to patch long strip on drywall?Large drywall patch: how to avoid bulging seams?Drywall Mesh Patch vs. Bulge? To remove or not to remove?How to fix this drywall job?Prep drywall before backsplashWhat's the best way to fix this horrible drywall patch job?Drywall patching using 3M Patch Plus Primer