TransWikia.com

Converting to bibitem in LaTeX

TeX - LaTeX Asked by Topdombili on April 11, 2021

For a conference I have to put the references in bibitem format. After, Googling I came up with this solution by Web page:

Create a refs.bib file with all the BibTeX entries, which are easily available from Google Scholar or similar

Create a “dummy” .tex file with the following entries:

documentclass{article}
begin{document}
nocite{*}
bibliography{refs}
bibliographystyle{plain}
end{document}

Now, do the following:

$ latex dummy
$ bibtex dummy
$ bibtex dummy
$ latex dummy

You will see a dummy.bbl file containing all your BibTeX entries in bibitem format.

but, the expected results was not observed for me. Any other solution or the problem with the mentioned process.

4 Answers

I agree whith the general process explained in the comments, but i think that they don't fully address the final task that you must do you for the conference, which very likely want a single self contained .tex file.

Let's assume that you have mypaper.tex which is your text with some cite{<key>} and a refs.bib file. Then :

  1. Firstly, put in mypaper.tex the two lines :

    bibliography{refs} bibliographystyle{plain}

  2. Secondly run (instead of using the nocite which has two drawbacks: (1) ordering as in refs.bib and (2) cites refs. that are not cite-d in your paper) :

    (pdf)latex mypaper bibtex mypaper

    If this step is successful you will get mypaper.bbl containing the bibitem-s and a mypaper.blg which is BiBTeX's log file. (Nota : latex reads the mypaper.tex -- and when present the mypaper.bbl file -- but bibtex reads the mypaper.aux created by latex).

  3. Thirdly, (optional but recommended) make sure that all the reference are correctly inserted and displayed in the file with :

    (pdf)latex mypaper

  4. Fourthly, open mypaper.tex, comment out or discard the two biblio... lines and paste the whole content of mypaper.bbl at the place where you want to get the bibliography. You then have the final self-contained file.

    You can run (pdf)latex mypaper at least two times to get the final .dvi or .pdf.

Edit: This copy-paste holds if he .bbl file content start with the regular begin{thebibliography}. If it starts by loading packages with usepackage{<name>} or even by input{<name>.sty} (where <name>= csquote, url, etc.) you must move them in your preamble. If it starts by defining commands, your can keep them at this place or move them to the preamble.

Note for the OP: you use plain as the format, which looks strange for me. Actually each conference/organization generally has its own .bst style file which produces bibitem formated accordingly to their editorial rules. More specifically, make sure that you need an alphabetical-ordered or a citation-ordered bibliography. In the later case, you must use the unsrt.bst (or a variant) in place of plain.bst.

Correct answer by Jhor on April 11, 2021

The Python code to convert refs.bib file to bibitem file

python2 bibtex2item.py < refs.bib > bibitem.txt

# filename: bibtex2item.py
import sys

bibtex = sys.stdin.read()
r = bibtex.split('n')
i = 0
while i < len(r):
  line = r[i].strip()
  if not line: i += 1
  if '@' == line[0]:
    code = line.split('{')[-1][:-1]
    title = venue = volume = number = pages = year = publisher = authors = None
    output_authors = []
    i += 1
    while i < len(r) and '@' not in r[i]:
      line = r[i].strip()
      #print(line)
      if line.startswith("title"):
        title = line.split('{')[-1][:-2]
      elif line.startswith("journal"):
        venue = line.split('{')[-1][:-2]
      elif line.startswith("volume"):
        volume = line.split('{')[-1][:-2]
      elif line.startswith("number"):
        number = line.split('{')[-1][:-2]
      elif line.startswith("pages"):
        pages = line.split('{')[-1][:-2]
      elif line.startswith("year"):
        year = line.split('{')[-1][:-2]
      elif line.startswith("publisher"):
        publisher = line.split('{')[-1][:-2]
      elif line.startswith("author"):
        authors = line[line.find("{")+1:line.rfind("}")]
        for LastFirst in authors.split('and'):
          lf = LastFirst.replace(' ', '').split(',')
          if len(lf) != 2: continue
          last, first = lf[0], lf[1]
          output_authors.append("{}, {}.".format(last.capitalize(), first.capitalize()[0]))
      i += 1

    print "bibitem{%s}" % code
    if len(output_authors) == 1:
      print output_authors[0] + " {}. ".format(title),
    else:
      print ", ".join(_ for _ in output_authors[:-1]) + " & " + output_authors[-1] + " {}. ".format(title),
    if venue:
      print "{{em {}}}.".format(" ".join([_.capitalize() for _ in venue.split(' ')])),
      if volume:
        sys.stdout.write(" textbf{{{}}}".format(volume))
      if pages:
        sys.stdout.write(", {}".format(pages) if number else " pp. {}".format(pages))
      if year:
        sys.stdout.write(" ({})".format(year))
    if publisher and not venue:
      print "({},{})".format(publisher, year)
    print
    print

sample input

@article{karrer2011stochastic,
  title={Stochastic blockmodels and community structure in networks},
  author={Karrer, Brian and Newman, Mark EJ},
  journal={Physical Review E},
  volume={83},
  number={1},
  pages={016107},
  year={2011},
  publisher={APS}
}

sample output

bibitem{karrer2011stochastic}
Karrer, B. & Newman, M. Stochastic blockmodels and community structure in networks.  {em Physical Review E}. textbf{83}, 016107 (2011)

Answered by lxy on April 11, 2021

Well, I used this matlab function to convert it automatically one by one, its more easy to get the job done. You can download it from GitHub, and here is the matlab function as the following.

function [bibitem] = bibtex2bibitemtv(bibtexpath)
path = bibtexpath;
cells = importdata(path);
A = string(cells);
n = length(cells);
K_name = strings(n,1);
citecode = extractBetween(A(1),'{',',');
SVacI = zeros(9,1); % Substyle Vacation Indicator. 

for i = 2:1:n-1
        rawleft = extractBefore(A(i),'=');
        K_name(i) = strtrim(rawleft);
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        if strcmp(K_name(i),"author")
            SVacI(1) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                author = strtrim(extractBetween(A(i),'{','}'));
            else 
                author = strtrim(extractBetween(A(i),'=',','));
            end 
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           
        elseif strcmp(K_name(i),"title")
            SVacI(2) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                title = strtrim(extractBetween(A(i),'{','}'));
            else 
                title = strtrim(extractBetween(A(i),'=',','));
            end 
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
        elseif strcmp(K_name(i),"journal")
            SVacI(3) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                journal = strtrim(extractBetween(A(i),'{','}'));
            else 
                journal = strtrim(extractBetween(A(i),'=',','));
            end 
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
        elseif strcmp(K_name(i),"volume")
            SVacI(4) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                volume = strtrim(extractBetween(A(i),'{','}'));
            else 
                volume = strtrim(extractBetween(A(i),'=',','));
            end
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
        elseif (strcmp(K_name(i),"number"))||(strcmp(K_name(i),"issue"))
            SVacI(5) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                number = strtrim(extractBetween(A(i),'{','}'));
            else 
                number = strtrim(extractBetween(A(i),'=',','));
            end
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
        elseif strcmp(K_name(i),"pages")
            SVacI(6) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                pages = strtrim(extractBetween(A(i),'{','}'));
            else 
                pages = strtrim(extractBetween(A(i),'=',','));
            end
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
        elseif strcmp(K_name(i),"month")
            SVacI(7) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                month = strtrim(extractBetween(A(i),'{','}'));
            else 
                month = strtrim(extractBetween(A(i),'=',','));
            end
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
        elseif strcmp(K_name(i),"year")
            SVacI(8) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                year = strtrim(extractBetween(A(i),'{','}'));
            else 
                year = strtrim(extractBetween(A(i),'=',','));
            end
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
        elseif strcmp(K_name(i),"publisher")
            SVacI(9) = 1;
            rawright = extractAfter(A(i),'=');
            ccb = regexp(rawright,'}');     % ccb = [check curly bracket]
            if (ccb~=0)
                publisher = strtrim(extractBetween(A(i),'{','}'));
            else 
                publisher = strtrim(extractBetween(A(i),'=',','));
            end           
%       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% For adding new references sections.
        else
            msg = join(["Sorry!","[", K_name(i), "]","is not being handled by the program."]);
            disp(msg)
        end
end
% @-> Style reference term code varifications. 
%                  msg = join(["author","=",SVacI(1);...
%                              "title","=",SVacI(2);...
%                              "journal","=",SVacI(3);...
%                              "volume","=",SVacI(4);...
%                              "number","=",SVacI(5);...
%                              "pages","=",SVacI(6);...
%                              "month","=",SVacI(7);...
%                              "year","=",SVacI(8);...
%                              "publisher","=",SVacI(9)]);
%                              disp(SVacI)

              if (SVacI(1)==0)||(SVacI(2)==0)||(SVacI(3)==0)||(SVacI(8)==0)
                  bibitem = sprintf('THE ARTICLE IS NOT CORRECT. THE AUTHOR, TITLE, JOURNAL AND YEAR SECTIONS ARE COMPULSORY! PLEASE INCLUDE THEM AND RE-RUN THE SOFTWARE:)');
              elseif all(SVacI == [1    1   1   0   0   0   0   1   0])
                  bibitem = sprintf(' n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s},(%s). n',citecode,author,title,journal,year);
              elseif all(SVacI == [1    1   1   0   0   0   0   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s},(%s). %s.n',citecode,author,title,journal,year,publisher);
              elseif all(SVacI == [1    1   1   0   0   0   1   1   0])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s},(%s %s). n',citecode,author,title,journal,month,year);
              elseif all(SVacI == [1    1   1   0   0   0   1   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s},(%s %s). %s.n',citecode,author,title,journal,month,year,publisher);
              elseif all(SVacI == [1    1   1   0   0   1   0   1   0])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, pp. %s,(%s).n',citecode,author,title,journal,pages,year);
              elseif all(SVacI == [1    1   1   0   0   1   0   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, pp. %s,(%s). %s.n',citecode,author,title,journal,pages,year,publisher);
              elseif all(SVacI == [1    1   1   0   0   1   1   1   0])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, pp. %s,(%s %s).n',citecode,author,title,journal,pages,month,year);
              elseif all(SVacI == [1    1   1   0   0   1   1   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, pp. %s,(%s %s). %s.n',citecode,author,title,journal,pages,month,year,publisher);
              elseif all(SVacI == [1    1   1   0   1   0   0   1   0])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s,(%s).n',citecode,author,title,journal,number,year);
              elseif all(SVacI == [1    1   1   0   1   0   0   1   1])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s,(%s). %s.n',citecode,author,title,journal,number,year,publisher);
              elseif all(SVacI == [1    1   1   0   1   0   1   1   0])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s,(%s %s).n',citecode,author,title,journal,number,month,year);
              elseif all(SVacI == [1    1   1   0   1   0   1   1   1])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s,(%s %s). %s.n',citecode,author,title,journal,number,month,year,publisher);
              elseif all(SVacI == [1    1   1   0   1   1   0   1   0])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s, pp. %s,(%s).n',citecode,author,title,journal,number,pages,year);
              elseif all(SVacI == [1    1   1   0   1   1   0   1   1])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s, pp. %s,(%s). %s.n',citecode,author,title,journal,number,pages,year,publisher);
              elseif all(SVacI == [1    1   1   0   1   1   1   1   0])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s, pp. %s,(%s %s). %s.n',citecode,author,title,journal,number,pages,month,year);
              elseif all(SVacI == [1    1   1   0   1   1   1   1   1])
                  disp("Attention! THE VOLUME HAS TO BE ASSOCIATED WITH NUMBER, PLEASE CHECK!") 
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s, pp. %s,(%s %s). %s.n',citecode,author,title,journal,number,pages,month,year,publisher);
              elseif all(SVacI == [1    1   1   1   1   0   0   1   0])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s),(%s).n',citecode,author,title,journal,volume,number,year);
              elseif all(SVacI == [1    1   1   1   1   0   0   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s),(%s). %s.n',citecode,author,title,journal,volume,number,year,publisher);
              elseif all(SVacI == [1    1   1   1   1   0   1   1   0])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s),(%s %s).n',citecode,author,title,journal,volume,number,month,year);
              elseif all(SVacI == [1    1   1   1   1   0   1   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s),(%s %s). %s.n',citecode,author,title,journal,volume,number,month,year,publisher);
              elseif all(SVacI == [1    1   1   1   1   1   0   1   0])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s), pp. %s,(%s).n',citecode,author,title,journal,volume,number,pages,year);
              elseif all(SVacI == [1    1   1   1   1   1   0   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s), pp. %s,(%s). %s.n',citecode,author,title,journal,volume,number,pages,year,publisher);
              elseif all(SVacI == [1    1   1   1   1   1   1   1   0])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s), pp. %s,(%s %s). %s.n',citecode,author,title,journal,volume,number,pages,month,year);
              elseif all(SVacI == [1    1   1   1   1   1   1   1   1])
                  bibitem = sprintf('n bibitem{%s}n %s.n newblock {%s.} n newblock {emph %s}, %s(%s), pp. %s,(%s %s). %s.n',citecode,author,title,journal,volume,number,pages,month,year,publisher);
              end       

end Sample input:

@article{thompson1990home,
  title={In-home pasteurization of raw goat's milk by microwave treatment},
  year={1990},  
  author={Thompson, J Stephen and Thompson, Annemarie},
  journal={International journal of food microbiology},
  volume={10},
  number={1},
  pages={59--64},
  publisher={Elsevier}
}

Sample Output:

  bibitem{lewis2009heat}
  Lewis, MJ and Deeth, HC.
  newblock {Heat treatment of milk.} 
  newblock {emph Milk processing and quality management}, 3(193), pp. 168--204,(Jan. 2009). Wiley Online Library.

Answered by Suhail Abdullah on April 11, 2021

Modified for python3

# filename: bibtex2item.py
# python2 bibtex2item.py 
import sys

# bibtex = sys.stdin.read()
try:
    bibtex=open("refs.bib")
except IOError:
    print("File not found or path is incorrect")
finally:
    print("exit")
bibtex = bibtex.readlines()
# print(bibtex)
# r = bibtex.split('n')
r = bibtex
i = 0
while i < len(r):
  line = r[i].strip()
  if not line: i += 1
  if '@' == line[0]:
    code = line.split('{')[-1][:-1]
    title = venue = volume = number = pages = year = publisher = authors = None
    output_authors = []
    i += 1
    while i < len(r) and '@' not in r[i]:
      line = r[i].strip()
      #print(line)
      if line.startswith("title"):
        title = line.split('{')[-1][:-2]
      elif line.startswith("journal"):
        venue = line.split('{')[-1][:-2]
      elif line.startswith("volume"):
        volume = line.split('{')[-1][:-2]
      elif line.startswith("number"):
        number = line.split('{')[-1][:-2]
      elif line.startswith("pages"):
        pages = line.split('{')[-1][:-2]
      elif line.startswith("year"):
        year = line.split('{')[-1][:-2]
      elif line.startswith("publisher"):
        publisher = line.split('{')[-1][:-2]
      elif line.startswith("author"):
        authors = line[line.find("{")+1:line.rfind("}")]
        for LastFirst in authors.split('and'):
          lf = LastFirst.replace(' ', '').split(',')
          if len(lf) != 2: continue
          last, first = lf[0], lf[1]
          output_authors.append("{}, {}.".format(last.capitalize(), first.capitalize()[0]))
      i += 1

    print("bibitem{%s}" % code)
    if len(output_authors) == 1:
      print(output_authors[0] + " {}. ".format(title),)
    else:
      print(", ".join(_ for _ in output_authors[:-1]) + " & " + output_authors[-1] + " {}. ".format(title),)
    if venue:
      print ("{{em {}}}.".format(" ".join([_.capitalize() for _ in venue.split(' ')])),)
      if volume:
        sys.stdout.write(" textbf{{{}}}".format(volume))
      if pages:
        sys.stdout.write(", {}".format(pages) if number else " pp. {}".format(pages))
      if year:
        sys.stdout.write(" ({})".format(year))
    if publisher and not venue:
      print("({},{})".format(publisher, year))
    print
    print

Answered by vinsent paramanantham on April 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP