## 4 Going further: Editing tools When you have had some practise with TEXworks, you’ll find the need for more effective tools. Many tools exist in TEXworks. We are going to see them now. ### 4.1 Creating a document from a template Most documents you will create will use the same instructions in the preamble, the same layout settings, similar heading and so on. You can use predefined templates or create your own with all of these settings already in place. Use File / New from template… or `[Ctrl+Shift+N]` (Mac OS X `[Cmd+Shift+N]`). A dialogue box opens to allow selection of the template. After selection and `OK` a document is created and you can start to work. If you want to create a personal template, you just have to create a suitable document with everything you always want to do (and perhaps marking places to fill in) and save it as a `.tex` file in the `<home>\TeXworks\templates` folder, or possibly a sub-folder of it. ### 4.2 Creating a project using several source files When the source becomes long, it is sometimes difficult to navigate in it. It is then useful to split the source in different smaller files: one file will be the main document, with the preamble, the `document` environment, as well as calls to the “sub-documents”.13 But there might be a problem if, in a sub-document, you start typesetting/compilation: as there is no preamble nor `document` environment there is an immediate error stop. To tell TEXworks that it should typeset the main document one adds at the very beginning of the sub-document the instruction: `% !TeX root = path/main_file.tex` for example: `% !TeX root = manual.tex` If the main file is in the same folder, its name is enough, as in the above example. Notice that the slash `/` and not the backslash `\` should be used even on Windows. Further, with MiKTeX, the call to a sub-document, `\input{name.tex}`, should include the extension `.tex` to have an active SyncTeX action. ### 4.3 Spell-check You can turn on automatic spell-checking of your source from Edit / Spelling / <language>: for example `en-GB` for UK English. During typing, if there is an error, the word is underlined by a red wavy line. A right-click on the word opens a contextual menu in which there are some replacement suggestions. Click on the desired word to make the replacement. Before using the spell-checker, you need to install dictionaries in the right folder of TEXworks: `<home>\TeXworks\dictionaries`. One can use the available dictionaries for OpenOffice and other free software;14, if you have Thunderbird with spell-check, you can copy the `.aff` and `.dic` files. It is possible to ask TEXworks to use by default a dictionary by Edit / Preferences…/ Editor option Spell-check language. ### 4.4 Search and replace #### 4.4.1 Standard functions The options of the menu Search: Find…, Find again, Replace… and Replace again (`[Ctrl+F]`, `[Ctrl+G]`, `[Ctrl+R]` and `[Ctrl+Shift+R]`, respectively) are standard actions (Mac OS X `[Cmd+F]`, `[Cmd+G]`, `[Cmd+R]` and `[Cmd+Shift+R]`); the first and the third open a dialogue box: (replace) There are the usual options: Find backwards, Wrap around or Replace within selection. The following options are also usual: Case sensitive and Whole words. By default the search is forward, towards the end of the document. The option Replace in all open files is also a frequent choice, but not as much as the others; this allows, for example, replacement in all the files of a project – pay attention as this is very powerful. The last option, Regular expression, is detailed in the next sub-section. In the Search menu there are other options: Copy to Find, copies the currently selected text into the Find zone of the Find or Replace dialogue; you still need to open Find or Replace separately; Copy to Replace, the same for the Replace dialogue; Find Selection, uses the current selection for a search without opening the Find dialogue: very fast; Show selection, scrolls the view to the currently selected text: useful if word wrapping is turned off and one moved in the document using the vertical scroll bar on the right. #### 4.4.2 Regular expressions The regular expressions provide a very powerful tool, but they require some effort to be well understood. To understand them fully would require a manual, so we’ll give some simple ideas of use: see also section 6.2. Suppose we have the following text:
    Voici du texte pour tester les expressions régulières
    dans du texte accentué.
    Voici du texte pour tester les expressions régulières dans
    du texte accentué.
    Voici du texte pour tester les expressions régulières. Voici
    du texte pour tester les expressions régulières.
    truc          truc
    tél.: 010-99-99-99
    tél.: 00.32.10.99.99.99
    tél.: 00/32-10/99.99.99
We want to 1) insert an empty line between the paragraphs after “accentué” (to create paragraphs in LATEX) but not after the three telephone numbers; 2) replace each tab character between the two words “truc” of the fourth paragraph, by three spaces; and finally 3) make the telephone numbers consistent by replacing the various punctuation characters by spaces. In the dialog box we have to select the option: Regular expression. For 1) in the dialogue box Replace (`[Ctrl+R]`) for Find: we put `>\n<` 15 and in With: `>\n\n<`. `>\n<` is the code to match or insert a line feed. You will need to select the first four paragraphs and the beginning of the fifth (first telephone number) and to tick the Replace within selection; if this was not done and an empty line has been inserted after each line, select the telephone lines and do the reverse action: replace `>\n\n<` by `>\n<`. So we replace one line feed by two, creating an empty line. For 2) use `>\t<` and “`>___<`. `>\t<` is the code which represent a tab, while a space is typed in literally (here represented as `_`). For 3) find `>-|\.|/<` and replace with “`>_<`. Here `>|<` provides different possibilities (`- . /`); for the point we have used `>\.<` because the dot alone is a regular expression code which represents any character and we would have replaced all the characters by a space!! we then use a code to get the point. If one has strings of the same character but of different length (for example 2, 3, 4, 5 characters) and one wants to bring all strings to a string with less characters (for example 2), one can ask to replace the string `>e{3,5}<` by `>ee<`. If one wants to insert at the beginning of some paragraphs separated or not by an empty line the same string, for example `>\noindent_` or `>\item_`, one can replace `>\n\n<` or `>\n<` by “`>\n\n\\noindent_<` or “`>\n\\noindent_<`. Pay attention, we have double `\` in front of `noindent` to get one (`\noindent`) because `\` is an ecape character in regular expressions! If it was making sense, we could replace all the letters between “a” and “m” by “$” using `>[a-m]<` and `>$<`. ### 4.5 Other tools for editing and error tracking #### 4.5.1 Standard tools It is always possible to undo an action using Edit / Undo or `[Ctrl+Z]` (Mac OS X `[Cmd+Z]`): this way you can undo stepwise! The inverse action, redo, is available as Edit / Redo or `[Ctrl+Alt+Z]` (Mac OS X `[Cmd+Opt+Z]`). TEXworks also provides the standard editing tools such as the clipboard; therefore one can select, cut/copy and paste a piece of text. You can select with the mouse by dragging over the desired text, or by double-clicking to select a word. Using the keyboard, holding down `[Shift]` while moving using the arrow keys will select text. You can also move and select word by word moving left or right holding `[Ctrl+Shift]` down (`[Cmd+Shift]` on Mac OS X). The clipboard short-cuts are the ones you’ll finds in almost every program: `[Ctrl+X]` to cut, `[Ctrl+C]` to copy and `[Ctrl+V]` to paste (`[Cmd+X]`, `[Cmd+C]` and `[Cmd+V]`, respectively, on Mac OS X). You can easily change the case of a selection — put everything upper case or lower case — using Edit / Changer case / and next, depending on the desired effect, ALL UPPERCASE or all lowercase. It is also convenient to show the line numbers, as all errors messages refer to these numbers; you can toggle the line numbers, on the left of the editing panel, `on/of` by Format / Line Numbers. #### 4.5.2 Commenting When preparing a document with (LA)TEX it is often useful to prevent compilation of a portion of text to be able to locate an error; you can do this piece by piece until you find a part which creates an error. To do this, commenting the source block by block is needed. We have seen that the symbol `%` marks the beginning of a comment. To comment a big piece of text, it is sufficient to select it and ask to mark it as comment Format / Comment or “`Ctrl+Shift+]`” (Mac OS X “`Cmd+Shift+]`”). To remove the comment, select the lines and choose Format / Uncomment or “`Ctrl+Shift+[`” (Mac OS X “`Cmd+Shift+[`”) 16. #### 4.5.3 Matching delimiters A frequent error is to forget a closing symbol: parenthesis, bracket, square bracket, etc. TEXworks helps with a tool to show the pairs of symbols: when the cursor moves over one of these symbols, its partner is briefly highlighted in orange. You can also select an entire block using Edit / Balance Delimiters or by the short-cut `[Ctrl+B]` (Mac OS X `[Cmd+B]`). Thus you will immediately sees the scope of the block. #### 4.5.4 Smart quotes Another similar error, but this time semantic and having no influence on typesetting, is in the use of quotes when one wants to give focus to some text. There are two types of quotes, in English, the ‘single’ quotes and the “double” quotes. They are obtained by the ``` and `´`; these are not the quotes used in programming and found on the keyboard `"` and `'`. But using the TEXworks smart quotes system, one can use the latter to automatically produce the single/double opening and close quotes. In a `.tex` document, select one of the smart quotes system: Format / Smart Quotes / TeX Ligatures|TeX Commands|Unicode Characters. Then in your text when you want to start a quoted text, let say double quotes, type `"` then the text to be focused on and finish again by `"`; TEXworks will automatically insert the correct opening quotes ```` and later the correct closing ones `''`. The three options give the same result in the typeset document. Finally, it is possible to define personal quotes systems in the file containing the definitions (`smart-quotes-modes.txt` in the `configuration` folder of the resource folder). ### 4.6 Auto-completion Another tool which rapidly becomes indispensable is auto-completion. Indeed, when you use (LA)TEX, you have to continuously enter codes to, for example, create environments; you also have to remember to close every group you open. Auto-completion allows you to type a keyword, hit `[Tab]` and have TEXworks insert the (LA)TEX command or environment code automatically. As an example to insert “LATEX”, we have to type `\LaTeX`. This is not difficult, but entering “`\`”17 followed by the word “`LaTeX`” with alternating capitals and lower case letters could become annoying after a while. With auto-completion you just enter `latex` and hit `[Tab]` to get `\LaTeX`. You just have to take care that there is no letter glued in front or after `latex`. Another example is `bmin`, which gives gives:
    \begin{minipage}{}
    •
    \end{minipage}•
with the cursor between the empty pair of brackets where you need to enter the size of the minipage. See the section 6.3 for a list of the keywords for auto-completion. Notice the “•” in the minipage environment. They are place holders which can be reached by `[Ctrl+Tab]` (`[Option+Tab]` on the Mac), repeating this short-cut goes forward in the created structure and by `[Ctrl+Shift+Tab]` (`[Option+Shift+Tab]`) one goes backward. If a partial keyword is given, repeatedly hitting `[Tab]` will cycle through possible completions. For example, `bali` (the `b` means the beginning of an environment `\begin{}`) creates the `align` environment after one `[Tab]`, next `align*`, and after, in succession, `alignat`, `alignat*`, `aligned`, `alignedat`, `alignedat` with option; these last environments have their own code which starts by `bali` (`balis`, `baliat`, `baliats`, `balied`, `baliedat` and `baliedato`). If you want to create your own keywords, you can add a `.txt` file in the `completion` folder inside `<home>\TeXworks`. The entries in the file should have the following format:
    bfigo:=\begin{figure}[#INS#]#RET##RET#\end{figure}•
    \bibliography{#INS#}•
In the first case, `bfigo` is the assigned keyword (with `:=`) to be converted into a `figure` environment with an optional argument; there is a carriage return after the `begin`, one empty and the cursor is between the square brackets. The codes `#INS#` and `#RET#` represent the cursor insertion point and line returns, respectively, “•” is a place holder. In the second case we give ourselves a short-cut, which will let us type the first part of `\bibliography{}` and have TEXworks convert it to the full name plus braces (with the cursor between them). The keyword is the instruction itself. Of course it is always possible to use “•”! The `.txt` file containing the auto-completion information needs to be UTF-8 encoded: it can be created with TEXworks.