[Tex/LaTex] TeXworks auto-complete

auto-completiontexstudiotexworks

TeXworks autocomplete capability is incomplete, messy and unpredictable. Is there any way to migrate TeXStudio auto-complete data to TeXworks?

Best Answer

TeXstudio and TeXworks take a somewhat different approach to auto-complete information, at least in part as the two programs have slightly different aims (TeXstudio is a LaTeX editor, TeXworks is a TeX editor and so has fewer LaTeX-specific parts at least in the core). That said, it certainly would be possible to use the auto-complete information from TeXstudio to augment or replace that in TeXworks.

TeXstudio stores auto-completion information on a package-by-package basis in a series of .cwl files. These are simple plain text files and are available from SourceForge (you have to pick and SVN revision: in the link I'd picked the latest one at time of writing). The information doesn't seem to be saved in plain text format in the release code: presumably it's 'baked in' to the binary/libraries. An example file is array.cwl

# mode: array.sty
# dani/2006-02-18
\extrarowheight
\extratabsurround
\firsthline
\lasthline
\newcolumntype{name}{argument}
\showcols

TeXworks stores the completion data in a series of files called tw-<name>.txt which live inside the TeXworks config tree (system-dependent location is a separate question). Some extracts from tw-basic.txt and tw-latex.txt show the format:

%%!TEX encoding = UTF-8 Unicode
xa:=\alpha
\xa:=\alpha
\bigskip#RET#
\hskip
ncm:=\newcommand{#INS#}{•}#RET#

The first line is present in all of the tw-<name>.txt files, and quite possibly required. After that, each line is one auto-completion entry. The format is:

  • Line starts with the (partial) text to match, e.g. xa or \hskip. This is the only required part.
  • A := separates out the 'short text' from the 'completion result', but is not needed if the two are the same
  • #RET# represents a carriage return
  • #INS# represents the place the cursor is left after auto-completion
  • is a marker to allow 'quick' moving of the cursor.

As you'll see, the TeXstudio format is actually less complex than that for TeXworks. As a result, conversion seems to require the following steps:

  • Download the .cwl files you want
  • Rename to tw-<name>.txt
  • Add an initial %%!TEX encoding = UTF-8 Unicode line (assuming you save them all as UTF-8)
  • Remove the first two lines, or at least replace the # by % to make them comments
  • Place the new files in the TeXworks completion folder
  • Restart TeXworks

An aside: the TeXworks auto-complete material is derived from that for TeXShop, I believe more-or-less verbatim. Almost certainly the TeXworks developers would welcome improvements in this area, so it would be worth considering discussing this with them. For example, an interface allowing selection of individual auto-complete files would bring TeXworks very close to TeXstudio in terms of auto-completion (scripting could be used to auto-enable completion files).

Related Question