Sublime Forum

LaTeXTools: Smart auto completion of "cite[tab]" to insert Bibtex cite key

#1

I am writing LaTeX in Sublime Text using LaTeXTools. The standard auto completion behaviour of \cite commands is to type

\cite{

and then automatically a menu appears that allows me to select a BibTeX reference. However, I want to avoid having to type \ and {, and therefore have created in LaTeX.sublime-completions an entry

{ "trigger": "cite", "contents": "\\cite{$0}"}

Now I can type “cite[tab]”, and I get \cite{} with the cursor between the {}, but not the menu that allows me to select the BibTeX entry.

Is it possible to extend the above completion, such that when I type cite followed by [tab], then I get \cite{} with the cursor between the {}, and automatically the menu to select a BibteX key shows up?

Thank you!

0 Likes

#2

I don’t use LaTeX at all, but question: if you were in a situation where the content of the buffer was \cite{|}, where | is the cursor, and you manually invoke the autocomplete popup (ctrl+space on windows, for example), does the autocomplete provide you the correct options for things to insert?

0 Likes

#3

I don’t think you can get the BibTeX auto-completion list using a snippet. As @OdatNurd suggested, trigger it through a key binding. I suggest using Ctrl + l, Ctrl + space. For that, I believe you have to add the following in your user key bindings file:

{ "keys": ["ctrl+l","ctrl+space"], "command": "latex_fill_all", "args": {"overwrite": true}, "context": [ {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} ] },

I am no expert, but it seems another way would be to trigger both commands (the snippet and the latex_fill_all) using a macro or the chain command.

0 Likes

#4

Thanks! Interestingly, for ctrl+space inside \cite{|} I get a new type of autocomplete popup that I hadn’t seen before (I’ve switched from Emacs to Sublime only recently).

@rcopat, thanks for the suggestion! Could you explain what this key binding does, and how it compares to the menu showing up up when I hit ctrl+space inside \cite{|}? Still learning.

Would it be possible to define some kind of user-defined function in Sublime, which first puts the string \cite{|} into the buffer, placing the cursor between the {}, and then call the BibTeX auto-completion list? And then assign this to a key binding? I would prefer the cite+[tab], but maybe this is an alternative.

It’s a pity that this type of completion seems not possible, I thing this would be very convenient in many occasions.

0 Likes

#5

Maybe one follow-up question: In the now auto-complete popup showing up for ctrl+space inside \cite{|}, and also in the standard BibTeX auto-complete menu, only the first author is shown (plus an " and" if there are multiple authors. Any ideas how I can show all authors?

In my user LaTeXTools.sublime-settings file I have already set

"cite_panel_format": ["{keyword}: {author} - {title} ({year})",""],

and

"cite_autocomplete_format": "{keyword}: {author} - {title} ({year})",

So this should show the “long” version of the authors list, but still it shows and allows to search for only the first one.

0 Likes

#6

The theory behind my original question was that if the LaTeX package you’re using uses buffer contents to offer “proper” completions (i.e. things that you would put there yourself) then it’s easy to create a key binding that would first inject the text, putting the cursor in the middle of it, followed by opening the autocomplete popup.

Since the panel looks different, it sounds like maybe the package in question is doing something outside of the standard autocomplete (hover popup perhaps?)

It sounds like that’s not what it does, in which case it’s likely still possible, but much more work (when it comes to customizing Sublime, most things are possible one way or another). In this particular case that would mean that the package itself would have to change to some degree.

1 Like

#7

In LaTeXTools, { is a special key that triggers auto-completion. Particularly, after typing \cite{, auto-completion of citations is triggered. The LaTeXTools authors got the auto-completion list (which comes from the BibTeX file(s)) and put them in the command palette. This behavior is performed through the latex_fill_all command. The trigger from { can be mimicked by a keybinding, as I explained before. The native auto-completion in Sublime is also possible by Ctrl + space, as @OdatNurd explained. I particularly find the auto-completion created by LaTeXTools nicer, as it uses the command palette.

Using the chain command, you should be able to trigger both the snippet and the auto-completion in the command palette. Copy the code below and paste in the right pane of Preferences -> Key Bindings. This looks like a brute-force approach to me, and other people may have a more elegant solution.

{ "keys": ["c","i","t","e","tab"], "command": "chain", "args": {"commands": [
	[ "insert_snippet", {"contents": "\\cite{$0}" }],
	[ "latex_fill_all", {"overwrite": true}],
] }, "context": [ {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} ] },
2 Likes

#8

I cannot reproduce your issue. For me it shows the complete “author={…}” field in the BibTeX file.

1 Like

#9

This feels indeed quite hacky, but I like it and it works fine, thank you!

In fact, I had been looking for a command that does what “chain” does, but didn’t find it. This is very useful to automate things to make editing faster and more convenient. Is there some documentation that I didn’t find yet? At least the Sublime and LaTeXTools docs seem not to mention it.

0 Likes

#10

@rcopat, thanks for trying to reproduce the issue! Since you were not able to reproduce it, I kept experimenting with it, and noticed that it seems to be an issue with a certain set of BibTeX files that are not parsed correctly.

Concretely, with the BibTeX files from here

https://cryptobib.di.ens.fr/

I can reproduce the issue reliably, here’s a screenshot:

However, when I use BibTeX entries from DBLP, which does not then it works fine (I have another screenshot, but as a new user I can only post one screenshot at a time - just imagine the above screenshot with all authors in the form “Alice Armstrong and Bob Baker”).

My best guess for the root of this issue is that the files from https://cryptobib.di.ens.fr/ contain a line break after each “and” between authors, like this:

author = “Yan Huang and
Fangguo Zhang and
Zhi Hu and
Zhijie Liu”,

And it seems that LaTeXTools does not strip these line breaks, but stops reading the field after the first line break. In contrast, DBLP BibTeX entries don’t contain line breaks and work fine.

Since BibTeX allows for line breaks in the authors field, it seems that LaTeXTools does not yet parse these fields correctly, as it seems to stop reading the contents of the field at a line break.

It seems to be an issue with how LaTeXTools parses fields, and I have openend an issue there.

0 Likes

#11

You are right. I have now understood the difference between the “autocomplete popup” and the “panel” (I think), and the issue appears with both. It seems indeed that LaTeXTools does not parse BibTeX files with line breaks in the author field correctly.

0 Likes

#12

Unfortunately, the Github project seems somewhat on hold, at least there hasn’t been much activity recently. So I’d like to give it a go and fix it myself.

Could anyone point me into the right direction? It seems the BibTeX parser is in the file external/bibtex/parser.py of the LaTeXTools package, but I couldn’t find the code that might stop reading at a line break there. Is this even the part of LaTeXTools or is this an external library?

0 Likes

#13

Chain of Command is the name of an old sublime package. Description and example are provided here. My understanding is that in ST4 the command became part of native Sublime, so no external package is required. The syntax has not changed.

1 Like

#14

I am still unable to reproduce this issue. The auto-completion still shows all the authors when I have line breaks in the BibTeX file. Changing the field from curly braces to double quotes also creates no issue. Please, post one full citation that I can try.

1 Like

#15

Thank you very much for trying to reproduce it! I tried it again, with a simple bare bones LaTeX and a .bib file containing only this entry:

+++ START

@string{acisp20 = “ACISP20”}
@string{acisp20key = “ACISP 2020”}
@string{acisp20name = “ACISP 20” # acispname}
@string{acisp20ed = “Joseph K. Liu and Hui Cui”}
@string{acisp20vol = “12248”}
@string{acisp20addr = “”}
@string{acisp20month = nov # “~/~” # dec}

@InProceedings{TestKey,
author = “Huy Quoc Le and
Dung Hoang Duong and
Willy Susilo and
Ha Thanh Nguyen Tran and
Viet Cuong Trinh and
Josef Pieprzyk and
Thomas Plantard”,
title = “Lattice Blind Signatures with Forward Security”,
pages = “3–22”,
editor = acisp20ed,
booktitle = acisp20name,
volume = acisp20vol,
address = acisp20addr,
month = acisp20month,
publisher = acisppub,
series = mylncs,
year = 2020,
doi = “10.1007/978-3-030-55304-3_1”,
}

@InProceedings{TestKey2,
author = “Huy Quoc Le and
Dung Hoang Duong and
Willy Susilo and
Ha Thanh Nguyen Tran and
Viet Cuong Trinh and
Josef Pieprzyk and
Thomas Plantard”,
title = “Lattice Blind Signatures with Forward Security”,
pages = “3–22”,
editor = acisp20ed,
booktitle = acisp20name,
volume = acisp20vol,
address = acisp20addr,
month = acisp20month,
publisher = acisppub,
series = mylncs,
year = 2020,
doi = “10.1007/978-3-030-55304-3_1”,
}

+++ END

And I could reproduce it reliably. Here is a screenshot:

30

0 Likes

#16

PS: I added the same entry twice, with different cite keys, as otherwise the selection menu does not show up if there is only a single entry in the .bib file.

0 Likes

#17

For completeness, here’s the LaTeX document that I have used:

+++ START

\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Title}

\begin{document}
\maketitle

\cite{TestKey}

\bibliographystyle{plain}
\bibliography{extrarefs}

\end{document}

+++ END

0 Likes

#18

What does the # inside the @string{} do? That is preventing the auto-completion from showing up. If I remove that, it works well.

1 Like

#19

The # concatenates strings, so we have "foo" # "bar" = "foobar". It is used very intensively in the third-party provided bibtex files from https://cryptobib.di.ens.fr/ that I am using.

However, I cannot confirm that the issue disappears when the # is removed.

This bibtex file here doesn’t contain the #:

+++ START

@string{acisp20 = “ACISP20”}
@string{acisp20key = “ACISP 2020”}
@string{acisp20name = “ACISP 20”}
@string{acisp20ed = “Joseph K. Liu and Hui Cui”}
@string{acisp20vol = “12248”}
@string{acisp20addr = “”}

@InProceedings{TestKey,
author = “Huy Quoc Le and Dung Hoang Duong and
Willy Susilo and
Ha Thanh Nguyen Tran and
Viet Cuong Trinh and
Josef Pieprzyk and
Thomas Plantard”,
title = “Lattice Blind Signatures with Forward Security”,
pages = “3–22”,
editor = acisp20ed,
booktitle = acisp20name,
volume = acisp20vol,
address = acisp20addr,
publisher = acisppub,
series = mylncs,
year = 2020,
doi = “10.1007/978-3-030-55304-3_1”,
}

@InProceedings{TestKey2,
author = “Huy Quoc Le and
Dung Hoang Duong and
Willy Susilo and
Ha Thanh Nguyen Tran and
Viet Cuong Trinh and
Josef Pieprzyk and
Thomas Plantard”,
title = “Lattice Blind Signatures with Forward Security”,
pages = “3–22”,
editor = acisp20ed,
booktitle = acisp20name,
volume = acisp20vol,
address = acisp20addr,
publisher = acisppub,
series = mylncs,
year = 2020,
doi = “10.1007/978-3-030-55304-3_1”,
}

+++ END

and the issue still appears. Note that the only differences in the two bibtex entries are in the author field and the cite key. Both are displayed until the end of the first line of the author field, as in the screenshot below, therefore I came to the conclusion that it must be due to the line break.

50

0 Likes

#20

I used the same extrarefs.bib file above without the # symbols. I had to adjust the .tex file to the file below, otherwise it does not compile:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[authordate, giveninits=true, uniquename=false, backend=biber, maxcitenames=3, backref=false]{biblatex-chicago}
\addbibresource{extrarefs.bib}

\begin{document}
\title{Title}
\author{John Doe}
\maketitle

\cite{TestKey}

\printbibliography

\end{document}

Are you using Windows? Are you using Biber?

Try the same .tex file.

0 Likes