71

Can thebibliography be sorted by citation order? Or is there a solution with an inlined bibliography that can be sorted?

Not having an additional .bib file would be preferable.

My current bilbiography is inluded in the rest of the article inside a thebibliography block.

\begin{document}
\section{Example}
The citations in\cite{ne} the bibliography should\cite{flowers} be ordered 
according which order they appear in the text. 

\begin{thebibliography}{9}

\bibitem{fetebok} \emph{Internal Combustion Engine Fundamentals}

\bibitem{ne} \emph{Förbränningsmotorer}

\bibitem{flowers} \emph{HCCI Research Towards Development for Stationary Power 
Applications}

\end{thebibliography}

Should output:

The citations in[1] the bibliography should[2] be ordered 
according which order they appear in the text. 

Bilbiography
[1] Förbränningsmotorer
[2] HCCI Research Towards Development for Stationary Power Applications
[3] Internal Combustion Engine Fundamentals
  • Welcome to tex.sx! It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting. – Joseph Wright May 4 '11 at 9:41
  • 5
    The standard unsrt style does exactly this: a minimal example of your code would be very useful to give a more targeted answer. – Joseph Wright May 4 '11 at 9:42
  • 3
    This is a duplicate of tex.stackexchange.com/questions/14976/…. This qn is a bit more clearly phrased and easier to find than the older qn, so it might be an idea to close the older one as a duplicate of this one. – Charles Stewart May 4 '11 at 10:48
  • \bibliographystyle{unsrt} doesn't seem to work with \begin{thebibliography}. Nothing is changing anyway. – Robert Foss May 4 '11 at 17:10
  • 1
    You might need to delete your .aux and .bbl files before it will work properly... – Seamus May 4 '11 at 17:13
59

If you really need a bibliography sorted by citation order you have two choices:

(1) write the entries in thebibliography in citation order

(2) use a .bib file, \bibliographystyle{unsrt} and BibTeX.

There is a third choice, actually, but I did it just to prove it's possible:

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

\newcounter{mycite}
\newtoks\citetoks
\makeatletter
\DeclareRobustCommand\unscite[1]{%
  \@ifundefined{uns@cite#1}
    {\refstepcounter{mycite}\label{citelabel@#1}%
     \expandafter\xdef\csname uns@cite#1\endcsname{\arabic{mycite}}%
     \toks\z@=\expandafter{\the\citetoks}%
     \toks\tw@=\expandafter\expandafter\expandafter{%
       \csname uns@bibitem#1\endcsname}%
     \edef\@tempcite{\the\toks\z@\the\toks\tw@}%
     \global\citetoks=\expandafter{\@tempcite}%
    }{}[\@nameuse{uns@cite#1}]}
\newcommand{\mybibitem}[2]{%
  \@namedef{uns@bibitem#1}{\bibitem[\ref{citelabel@#1}]{#1}#2}}
\makeatother


% Define here the bibliography entries
\mybibitem{fetebok}{\emph{Internal Combustion Engine Fundamentals}}
\mybibitem{ne}{\emph{Förbränningsmotorer}}
\mybibitem{flowers}{\emph{HCCI Research Towards Development for Stationary 
  Power Applications}}

\begin{document}
\section{Example}
The citations in~\unscite{ne} the bibliography should~\unscite{flowers}
be ordered according which order they appear in the text. 

\begin{thebibliography}{\arabic{mycite}}
\the\citetoks
\end{thebibliography}

\end{document}

Notice that this \unscite can't process lists of references or the optional argument like the usual \cite. It might be done, but I advise you against this approach.

  • 1
    @Argyll Usage of BibTeX is covered in any LaTeX primer. – egreg May 28 '16 at 8:09
  • 1
    Well, the options I use is [style=phys,sorting=none,backend=bibtex]. It sorts the references for most of my documents. Just not the one I am currently working on, which follows the order of references in the .bib file. I have no clue of the reason. – Argyll May 28 '16 at 8:12
  • 1
    @Argyll This is biblatex, a different thing – egreg May 28 '16 at 11:57
  • 3
    I'd strike the ''really'' - citation by order of occurrence is an absolute must-have. – reggie Sep 9 '16 at 8:27
  • 2
    @reggie The only reason for bibliographies in citation order was simplifying the life of typesetters in the olden times. This sorting makes no sense whatsoever for the reader who'd like to find things in the references and a long list in citation order obviously is of a hindrance. It's possible to get automatic alphabetic order and correct cross-references even with word processors, not only with BibTeX/biblatex, but academic inertia unfortunately wins. – egreg Sep 9 '16 at 8:35
4

I'd already created 150 references in a document using thebibliography, and was faced with the task of sorting them in order of first appearance in the text (a very common requirement in scientific journals).

you have two choices: (1) write the entries in thebibliography in citation order (2) use a .bib file, \bibliographystyle{unsrt} and BibTeX.

Both the above require a lot of work to either manually re-sort thebibliography or manually create a database .bib file.

https://code.google.com/p/latex-bibitemstyler/ seemed to be the solution, but I couldn't get it to work on my system and was working to a deadline. So I've written a brief and very inelegant Python 2.7 workaround which does the job and might help someone with a similar problem. If there could be a standard LaTeX command to sort thebibliography by order of first citation in the text in future releases, I'd be very grateful.

print"\n\nBibSort_v1\nSorts LaTeX 'thebibliography' in order of first citation in text\nDave Williams 26Mar2017\nRun the code in the same directory as your LaTeX source file\nUsing Python 2.x"
filename = raw_input("\n\nEnter LaTeX source filename (e.g. 'input.tex'): ")
fileobject = (open(filename, 'r'))
rawtext = fileobject.read()
fileobject.close()
start = '\\begin{document}'
end = '\\begin{thebibliography}'
bodytext = rawtext[rawtext.find(start)+len(start):rawtext.rfind(end)]
start = '\\begin{thebibliography}'
end = '\\end{thebibliography}'
bibliography = rawtext[rawtext.find(start)+len(start):rawtext.rfind(end)]
authorlist = []
for char in range(0,len(bodytext) - 10):
    if bodytext[char:char+6] == '\\cite{':
        author = ''
        char +=6
        while (bodytext[char] != '}'):
            if (bodytext[char] == ' '):
                char+=1
            elif(bodytext[char] == ','):
                char +=1 
                if author in authorlist:
                    author = ''
                else:
                    authorlist.append(author)
                    author=''
            else:
                author += (bodytext[char])
                char +=1    
        if author in authorlist:
            pass
        else:
            authorlist.append(author) 

labellist = []
reflist = []
for char in range(0,len(bibliography) - 7):
    ref =''
    if bibliography[char:char+9] == '\\bibitem{':
        char+=9
        label =''
        while(bibliography[char] != '}'): 
            label += bibliography[char] 
            char+=1 
        labellist.append(label)
        char+=1
        while (bibliography[char:char+9] != '\\bibitem{'):
            if char == len(bibliography) -1:
                break
            elif (bibliography[char]) == '\n':
                char +=1
            else:
                ref += (bibliography[char])
                char +=1
        reflist.append(ref) 

dictionary = dict(zip(labellist, reflist))
output =''
orphanlist =''
try:
    for name in authorlist:
        output += '\\bibitem{' + name + '}\n' + dictionary[name] + '\n\n'
    if len(authorlist) < len(labellist):
        output+="%" + "-"*80 + "\n"
        output+= "%"+ " The following %d references are in thebibliography but not cited in the text:\n\n" % (len(reflist) - len(authorlist)) 
        orphanlist = list(set(labellist)-set(authorlist))
        for name in range(0, len(orphanlist)-1):
            output += '\\bibitem{' + orphanlist[name] + '}\n' + dictionary[orphanlist[name]] + '\n\n'
    print("\n\n%d unique references were found in the text and sorted in order of appearance in the text" % (len(authorlist)))
    print("%d additional references were found in \\thebibliography which were not used in the text" % (len(orphanlist)))
    print("\nNew bibliography saved as:\n%s\n\nUsing a text editor, Copy all contents of this file,\nand Paste over the existing mybibliography in your LaTeX source file:\n%s\n" % (filename[:-4]+'_NewBib.txt',filename))
    fileobject = open(filename[:-4]+'_NewBib.txt', 'w')
    fileobject.write(output)
    fileobject.close
except (TypeError, ValueError), e:
    print e
    print "\nThere may be a mis-match between the labels in your bibliography\n and one or more labels in your in-text citations.\nPerhaps there is a typo, or you've used a capital letter in one\nbut not the other.\nPlease check your the flagged citation using 'find' in your\n Latex editor and run this Python script again."
2

Yes there is a way to sort \bibitems in the order order of appearance in the text. Have a look at https://code.google.com/p/latex-bibitemstyler/, a nice little program that does this job like a charm.

protected by Community Apr 19 '18 at 11:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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