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}
  • 1
    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 at 15:22
  • Thanks. If you make a proper answer from this comment, I will accept it. – Fibo Kowalsky Jul 10 at 15:48
  • 1
    Off-topic: To play it safe, you may want to add a \par directive immediately after \end{tabular}. – Mico Jul 10 at 16:36
up vote 2 down vote accepted

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}
\label{l1}

instead. This also make the source code easier to read.

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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