# Autocomplete reference duplicates label in LaTeX documents

(NB: I opened an issue about this on the atom-latex tracker, but the actual problem could be upstream. I’m opening this discussion in hopes someone can put me on the right track or knows what’s going on.)

For a little while now I’ve been observing the following autocomplete behavior. I set my labels in the form eq:my-equation for equations, fig:my-figure for figures, etc.

If I type \eqref{ , the autocomplete dropdown shows up, I then type eq (without typing a colon), select a label, hit tab, and I get the correct completion.

However, if I type eq: with a colon and select the label I’m interested in (say eq:my-equation ), the completion ends up being \eqref{eq:eq:my-equation} instead of just \eqref{eq:my-equation} .

The moment I type : , the autocomplete pulldown visibly shifts to the right, so it seems like something is happening.

Has anyone else observed this? I only have official autocomplete packages installed and no other autocomplete package related to LaTeX.

See the original issue with a video illustrating the problem: https://github.com/James-Yu/Atom-LaTeX/issues/186

Thanks!

Do you have any snippets packages installed?

I tried disabling all three packages, but I continue to observe the same autocomplete behavior.

Well, if you disabled snippets and it still functions the same, that rules out the snippet idea.

I think what’s happening is that the colon is a non-word character. You type eq:, and autocomplete-plus is given the text just before the cursor, which doesn’t seem to care about the colon for some reason. When the package tries to insert the completion, I would expect it to run editor.selectToBeginningOfWord(), which is of course a zero-length string if the colon is a non-word character. You may be able to fix it without negative side effects by changing the setting (see the image below).

After checking the code: the above is a pretty theory, but I can’t support it with an actual reading of the source code. It’s still worth fiddling that setting to see if there’s an easy out. Based on that function, maybe autocomplete-plus does something erroneous like passing on a zero-length prefix when the final character isn’t a word character (you should be able to test this with any of the others on the list).

Thanks, that’s a good lead. Removing : from the non-word characters didn’t solve the issue unfortunately. If a label contains another non-word character, the same behavior occurs though. In addition, if I type \eqref{eq:my-e} and then hit TAB, the completion is eq:my-eq:my-equation, so it seems autocomplete-plus somehow backtracks to the most recent non-word character (in this case -) before inserting the label.

Unfortunately, it’s very common to name labels the way I do in LaTeX.

It seems that not typing the colon works just fine, though? Could you train yourself to not hit that button?

It seems that not typing the colon works just fine, though? Could you train yourself to not hit that button?

I could, but that would defeat the purpose of autocomplete. If only it performed a fuzzy search, avoiding to type : would be a good solution. If I don’t type :, I can’t narrow down the list of suggestions, which can grow quite large in long documents.

What LaTeX packages are you using?

Unfortunately, I don’t think that autocomplete-plus cares much about how to search the possible suggestions. autocomplete-plus relays the text near your cursor to any active providers, and the providers are the ones in control of sending back suggestions (unless you use the built-in provider, which does fuzzy search, as you can see in my image).

I think the solution would be a pull request to get a fuzzy search implemented in autocomplete-latex, perhaps as a setting that you can toggle. Then you can start typing from wherever in the name you want.

What LaTeX packages are you using?

I use atom-latex. See my original post.

What package do you call the “built-in provider”? I don’t have autocomplete-latex.

I wouldn’t ever be so vague as to call a package something other than its name. The built-in provider is part of autocomplete-plus.

Or atom-latex or whatever is providing the completions.