# [texworks] Wrapping a text in math mode

Alain Delmotte esperanto at swing.be
Wed Sep 25 16:06:48 CEST 2013

Hi!

Le 25/09/2013 09:43, Pavel Dvořák a écrit :
> H
> ​i all!
>
> Very often when I write a mathematical formula ​within a
> text paragraph, I often forget to enclose it in math
> environment \(...\). What I have to do then is to cut this
> expression with CTRL+X, introduce math environment
> \(...\)
> ​ and paste it back into it with CTRL+V.
>
> Is there a way to wrap this ​expression directly in the math
> environment
> \(...\)
> ​? For example, if I write an expression p_{12}+\log(x)
> \(
>> p_{12}+\log(x)
> \)
> ​, I would like just to select the first expression with my
> mouse, hit, for example, CTRL+Y and transform it to the
> second expression in math mode. ​

You can write a script following the example given below,
put the script in the script sub-folder of the TeXworks
resources folder (you didn't tell us which OS you are using
- see the manual).
It will appear in the scripts menu of TeXworks and you can

Regards,

Alain

====== my script "b-verb.lua"
--[[TeXworksScript
Title: b: verbatim \&verb
Shortcut: Alt+V
Description: Encloses the current selection in \verb||
Author: Alain Delmotte from Stefan Löffler & Jonathan Kew
Version: 0.1
Date: 2011-11-01
Script-Type: standalone
Context: TeXDocument
]]

txt = TW.target.selection
if (txt == nil) then txt = "" end
TW.target.insertText("\\verb|" .. txt .. "|")
TW.target.selectRange(TW.target.selectionStart -
string.len(txt) - 1, string.len(txt))
========
the keyboard short-cut is Alt+V
The & on the second line (Title) creates an accelerator in
the scripts menu (the v letter is underlined and pressing v
when the menu is open execute the script)
in the Title "b: verbatim", the b: is for sorting the
scripts in the menu, not necessary but useful (for me!!)
The action is in the 14th line (the third of the program
itself, here put the selection into the inline verbatim:
\verb|selection|