6

I defined this macro:

\newcommand\afigure[4][]{%
    \begin{figure}%
        \centering%
        \includegraphics[#1]{#2}%
        \caption{#3}%
        \label{#4}%
    \end{figure}%
}

TeXstudio has an auto-complete feature for labels, which is quite handy. However, when I use the above macro, the label I give isn't recognised by the editor, and so it does not appear on the list of auto-completable (sic) labels. Is there a way of telling TeXstudio to take the fourth argument there and consider it as being given inside a \label macro? Thanks!

mathbekunkus
  • 1,389

1 Answers1

5

TeXstudio uses .cwl files for auto-completion, which are usually auto-generated, but you can also create your own. According to the TeXstudio manual, they are stored in %appdata%\texstudio\completion\user or .config/texstudio/completion/user by default, depending on your OS. If you have made a custom installation, search for completion in your TeXstudio folder.

Then, all you have to do is to create a file called something like mycmds.cwl in that folder and write the two lines

\afigure{imagefile}{caption%text}{label%labeldef}#g
\afigure[keyvals]{imagefile}{caption%text}{label%labeldef}#g

Then restart TeXstudio, and the completion should work the way you want it to:

enter image description here

schoekling
  • 3,142
  • This is not quite the answer, although it is certainly handy. This allows me to have \afigure in the autocomplete list of macros, but not the fourth argument of this command added to the list of labels of my document. – mathbekunkus Mar 01 '21 at 23:19
  • 1
    I have edited my answer to show the behavior achieved with these instructions. Isn't that what you described as your goal in your question? – schoekling Mar 01 '21 at 23:25
  • 2
    Just in case, you could try to make sure the new .cwl is active by checking the respective box in Configure TeXstudio > Completion > Permanently active completion files: – schoekling Mar 01 '21 at 23:41
  • 3
    Thou art a gentleman and a scholar, and thou getst the cookie! Brilliant! :) – mathbekunkus Mar 02 '21 at 00:02
  • 1
    My apologies. This was my first bounty and I didn't know I had to manually award it. Thanks again! :) – mathbekunkus Mar 05 '21 at 22:44
  • Thank you for the answer :). Not knowing this TexStudio feature dissauded me from using custom \fref ref-like command to refer figures. – baalkikhaal Jun 14 '21 at 05:01