# Auto-completion of \ref doesn't work for my counter

I defined a counter and a command that defines examples and labels them. Unfortunately, when I use these definitions the \ref is not autocompleted by the texmaker or sharelatex.

What should I add in the definitions that the editor becomes aware of the existing labels?

MWE:

\documentclass{article}
\newcounter{examples}[section]
\renewcommand\theexamples{\arabic{section}.\arabic{examples}}

\newcommand{\xampl}[2]{\refstepcounter{examples}\par\medskip\noindent%
\begin{tabular}{@{}p{14mm}@{}l@{}}%
(\theexamples) & #1\\
\end{tabular}\label{#2}}

\begin{document}
\section{Introduction}

\xampl{The sentence is the first example}{l1}
\xampl{The sentence is the 2nd example}{l2}

\ref{l1}, \ref{l2}
\end{document}

• The editor or sharelatex rely on parsing the code, and since it is not explicitly see \label it does not now about these labels. It is probably a better idea to remove the \label from the macro definition and adding it manually. You probably have a similar problem with interfaces where the label is set using label=... – daleif Jul 10 '18 at 15:22
• Thanks. If you make a proper answer from this comment, I will accept it. – Fibo Kowalsky Jul 10 '18 at 15:48
• Off-topic: To play it safe, you may want to add a \par directive immediately after \end{tabular}. – Mico Jul 10 '18 at 16:36

Most editors (including sharelatex) provides auto completion for labels by parsing the source code for explicit \label{...}, so when you hide \label inside a macro, the label is no longer picked up by the parser.
The best solution is probably to not include \label in the macro definition and use something like
\xampl{The sentence is the first example}

The editors have a similar problem when an interface is setting \label through a key-value interface. For example the interface provided by the listings package.