[Tex/LaTex] Getting question mark in citation (Texstudio and zotero)

bibtex

\documentclass{article}
\bibliography{honoursbib.bib}
\begin{document}
Hello, this is a test.\cite{Jensen2012}
\end{document}

I get a question mark for Jensen2012. I am using Zotero(with betterbib(la)tex) to build the bib file and I use texstudio. Texstudio appears to be able to read the bib text because I get autocomplete suggestions for the bibliography.

Best Answer

It looks, from your question, as if you may be new to LaTeX, because you are making some assumptions and mistakes which are very understandable and easily corrected.

LaTeX isn't like a wordprocessor or an app where everything is integrated into a single program. It's more like a publishing firm where lots of different programs do lots of slightly different things.

In the basement there are two departments: TeX, which is a typesetting machine, which has been enhanced with a bunch of tools to make what we call LaTeX. It works with a file of text which has been marked up in various ways to tell it how to format the document. It doesn't care how that text file has been produced. It will quite happily function (in a terminal for instance) by just being given a marked-up file and told to typeset it. Usually this marked up file has the extension .tex.

Next door is the bibliography department. Its job is to cooperate with TeX to produce properly formatted bibliographies. It has an old but reliable machine called BibTeX, and a newer one called BibLaTeX/Biber. It needs an extra file which contains the bibliographical information about the works that may be being cited, which has the extension .bib. It works hand-in-hand with TeX, as described in this question about failing citations.

Down in the basement there are some other more minor departments, which deal with things like indexing and glossaries. Like the bibliography department they work very closely with TeX. In practice, as a document is being put into final form these various departments often have to pass files around from one to another: going from the marked up document which comes down to the basement to the final form sometimes takes several "passes" through TeX and into other departments.

All of this can be done "by hand" using nothing more than the most basic text editor and the command line. That, indeed, was the "old school" way. But to make life easier for users there are a whole bunch of agencies upstairs which help users interact with the grumpy technically minded people downstairs. What these do--in different ways--is help produce documents that are marked up the way TeX and its colleagues like to see them, and then pass them downstairs for typesetting. Those are what people are describing when they talk about "front-ends". In the end, all these agencies do is produce files in the format (hopefully) that TeX and its colleagues want to see, hand them over for typesetting, and present the result to the user. They all do it in slightly different ways. TeXstudio is one of those programs (there are others, and we all have our preferences). Zotero, in the end, is another: it's just a way of moving from a set of references kept in a format that (some!) humans find convenient, and put them in the format that they like to see down in the bibliography department. None of these "frontends" is responsible for what goes on downstairs, and they do better or worse jobs of interacting with it.

In your case, the first problem you are having is that you haven't marked up the document the way TeX and bibTeX want to see it.

 \documentclass{article}% Yup. That's there.
                         % TeX sees this and knows a lot about what the
                         % document should look like.
 \bibliography{honoursbib.bib} % Not happy. TeX and bibTeX want to see
                               % this at the point in the document where
                               % you want the bibliography to be printed.
                               % But everything that comes before
                               % \begin{document} is part of the
                               % "preamble", a set of /general/ rules
                               % about the document, not something to
                               % print out. So this is too soon. TeX
                               % can't "put" a bibliography here.
                               %
                               % This may seem counterintuitive: and
                               % indeed the more modern machine likes to
                               % be told earlier what file you are going
                               % to use for your citations. 
                               %
                               % Also, they don't really want to see the
                               % .bib extension on this. The /know/ it's
                               % got to be a .bib file, because that's all
                               % they can handle. Again, this seems silly,
                               % and the more modern Biber /does/ want it.
\begin{document}% Good! Now TeX knows it's looking at stuff you want
                % to have typeset.
Hello, this is a test.\cite{Jensen2012}% Fine. TeX is happy. The first
                                       % bit is some stuff to typeset. TeX
                                       % doesn't care about the stuff in
                                       % in the \cite command. When it
                                       % gets to it it sees that bibTeX
                                       % hasn't yet told it how to cite
                                       % "Jensen2012", so it makes a note
                                       % to bibTeX (on a different file)
                                       % "Work out how to cite this,
                                       % please", and puts a question-mark
                                       % in the file.
\end{document}% TeX is happy enough, but because it hasn't been told to
              % print a bibliography, it doesn't bother to tell bibTeX
              % where to look for citations! And it also doesn't have
              % anything it can use to tell bibTeX HOW to format them.

So when all this ends, TeX has written a note to bibTeX: "Please tell me how to cite 'Jensen2012' ": but it hasn't told it where to look that reference up, and it hasn't told it how to format it.

What needs to happen at this point is that you (or TeXstudio) also runs bibTeX. What's supposed then to happen is that bibTeX will write a note back to TeX saying "next time you typeset this, here is how to handle that 'cite', and here's a bibliography to print". To get that to work you need to run bibTeX, and then of course TeX again, so it can actually follow those instructions. TeXstudio will certainly provide an easy way to do that (under the Tools button, it seems). But in your case, it's going to fail anyway, because you haven't included the right information in your document to tell bibTeX what to do with the citation anyway!

You'd see that if you ran bibTeX and looked at its complaints:

I found no \bibdata command% <- I don't know where to look for data
I found no \bibstyle command% <- And I don't know how to format it

So the correct minimal document looks something like this.

\documentclass{article}

\begin{document}
Hello, this is a test.\cite{Jensen2012}

\bibliographystyle{plain}% When it sees this, TeX writes a note to tell
                         % bibTeX what sort of bibliography you want.
\bibliography{honoursbib}% This tells TeX to print the bibliography (if
                         % bibTeX has done its work) or to write a note to
                         % bibTeX telling it where to find the references
\end{document}

Then you will need:

  • To run TeX once, so that it can extract the information it needs to pass to bibTeX. At this point you /expect/ some question marks, because until TeX has read the thing once it doesn't know what to ask for.

  • Run bibTeX, which will scurry to the .bib file (which it now knows how to find) and look at a file called plain.bst (which tells it what citations should look like) and produce something that TeX can typeset.

  • Run TeX again, to get everything properly sorted. You may need, depending on the style of citation, to run it more than once. You will nearly always need multiple runs of TeX anyway, because there are some other things (like internal cross-references and a table of contents) that TeX can't do until it's had at least one go at producing the document.

Once you understand what's going on behind the scenes, it's much easier to work out how to coax your particular "front end" to do the right thing for you. This is a generally true if slightly frustrating fact about LaTeX. I think this is a pretty reasonable tutorial, which you may find helpful. bibtex Tutorial at LaTeX tutorials.

(By the way, I've been using "TeX" throughout this answer, when I should really have said "LaTeX", just to save time. Don't go running "plain TeX" on any of this. Plain TeX is a truly surly character who hangs out at the back of the print-shop, doing great work -- but you don't want to try talking to him directly.)

Related Question