Does anyone have an example for a search & replace lua script that I can use with TeXworks. I have a rather large document here (about 3'000 pages DIN A5) which I need to update continously. To change $
to \$
and various other replacements I would like to run a 'replace-all' script which makes all the replacements in a single run.
This could be quite useful when one has to take text from e.g. MS Word or the WWW.
Best Answer
Your question is very interesting, I promise to upvote it as soon as possible. As usual, I'm out of votes.
I'll try to write a humble answer. I'm really sorry if the solution might cause any trouble due to any bug or annoyance, I'm a Lua newbie.
:)
First things first: we need to enable the scripting language plugin in TeXworks. We can do it by simply going to the
Preferences
, in theScripts
tab:We just need to mark the checkbox.
:)
Now, let's see where to put our script. Go to
Scripts
,Scripting TeXworks
,Show Scripts Folder
:The operating system file manager will appear with the folder we want.
:)
In my Mac, it's under
~/Library/TeXworks/scripts
. Now, create a new file namedreplaceList.lua
and add the following content, as it is:My sincere apologies to Patrick, Taco and other Lua masters.
:)
Now, we have a new file in our folder,
replaceList.lua
:Now, back to TeXworks, we need to reload our list of scripts. It's easy, we need to go to
Scripts
,Scripting TeXworks
,Reload Scripts List
:Done.
:)
Let's create a test file:Time to run our script. Simply go to
Scripts
and selectReplace list
:Now, the magic of TeXworks scripting API will appear. First, we will define a list of patterns to look for:
Regex supported, I guess.
I'm telling our script to look for three words, separated by commas. After clicking
OK
, a new window will appear, with a list of replacement words:Of course, both lists have to have the very same size.
:)
After we clickOK
, our new text is presented:There we go!
:)
I hope my humble answer helps.:)