[Tex/LaTex] Configuring auto-completion with TeXstudio

auto-completiontexstudio

TeXstudio does a pretty good job with autocompletion. For example, when I type \eqref{, it already comes with suggestions, see the example below.

TeXstudio does the auto-completion perfectly

However, suppose that I don't want to use \eqref but a customized command using newcommand (I do this so that I can quickly switch to different styles, e.g., some for journals/conferences "Eq. (1)" is needed, whereas sometimes "(1)" suffices). In this case, TeXstudio does not do the auto-completion, see the example below.

TeXstudio does not do the auto-completion

Is it possible to configure TeXstudio such that it does the auto-completion for custom commands that require a reference?

Note that one option is to redefine \eqref using \renewcommand, but I'm looking for another way so that I can use if for many more commands, not just for \eqref.

Here is my MWE:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\alternative}[1]{Eq.~(\ref{#1})}

\begin{document}
    \begin{equation}
        \label{eq:emc2}
        E = mc^2
    \end{equation}
    I can refer to the equation using \eqref{eq:emc2} and using \alternative{eq:emc2}.
\end{document}

Best Answer

For that, you will have to make use of a custom .cwl file (which, afaik, requires a recent version of TeXstudio).

Create a file, let's say mycwl.cwl, in either %appdata%\texstudio\completion\user or .config/texstudio/completion/user according to your system.

In it, place the following line:

\alternative{label}#r

and save it. Technically, you can put anything meaningful in the argument, but some terms are reserved and have special meaning. And, as you wish TeXstudio to recognize a label there, you require one of those, namely label. The comment at the end of the line tells TeXstudio what kind of command yours is. #r stands for this command declares a reference like "\ref{key}". For further commands, just add a new line to your mycwl.cwl.

Then, in TeXstudio, go to Options -> Configure TeXstudio -> Completion and check mycwl.cwl on the list (crucial). And that's about it.

use of custom command in TeXstudio

For more details on customizing autocompletion with cwl files, check the TeXstudio manual, section "4.13 Description of the cwl format".

A tip: If the manual seems complicated, and you'd like to emulate the behavior of some command's autocompletion you like, you might want to download the sourcecode tarball at http://texstudio.sourceforge.net/, unpack it and look at the built-in cwl files in the "completion" folder. From there, you can proceed by means of examples of your commands of interest.

Related Question