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!

1 Answer 1

5
+50

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

6
  • 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. Mar 1, 2021 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 1, 2021 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 1, 2021 at 23:41
  • 3
    Thou art a gentleman and a scholar, and thou getst the cookie! Brilliant! :) Mar 2, 2021 at 0:02
  • 1
    My apologies. This was my first bounty and I didn't know I had to manually award it. Thanks again! :) Mar 5, 2021 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.