# Parse a label defined inside a macro in TeXstudio

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!

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:

• 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
• 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? Mar 1, 2021 at 23:25
• 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: Mar 1, 2021 at 23:41
• Thou art a gentleman and a scholar, and thou getst the cookie! Brilliant! :) Mar 2, 2021 at 0:02
• 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