4

I used amsrefs package for references as follows:

\begin{bibdiv}
\begin{biblist}
\bib{BW}{article}{
author={Bertram, A.},
author={Wentworth, R.},
title={Gromov invariants for holomorphic
maps on Riemann surfaces},
date={1996},
journal={jams},
volume={9},
number={2},
pages={529\ndash 571},
}
\end{biblist}
\end{bibdiv}

Is there a solution to convert this type of reference list to a format that can be processed by bibtex?

Kadir
  • 1,537
  • 1
  • 11
  • 27
  • sadly, there's no automatic method. one was originally intended, but with the death of the developer, someone else took over and the rules changed. on the other hand, this looks like an article that should be indexed by mathscinet, and from mathscinet you can get references in bibtex format. – barbara beeton Feb 06 '17 at 20:33

2 Answers2

9

Possibly something like this:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\processamsrefs}{m}
 {
  \iow_open:Nn \g_kadir_amsrefs_stream { #1.bib }
 }
\NewDocumentCommand{\bib}{mmm}
 {
  \kadir_amsrefs_bib:nnn { #1 } { #2 } { #3 }
 }

\iow_new:N \g_kadir_amsrefs_stream
\seq_new:N \l__kadir_amsrefs_output_seq
\seq_new:N \l__kadir_amsrefs_author_seq
\tl_new:N \l__kadir_amsrefs_pages_tl

\cs_new_protected:Nn \kadir_amsrefs_bib:nnn
 {
  \seq_clear:N \l__kadir_amsrefs_output_seq
  \seq_clear:N \l__kadir_amsrefs_author_seq
  \keys_set:nn { kadir/amsrefs } { #3 }
  \seq_put_left:Nx \l__kadir_amsrefs_output_seq
   {
    author = { \seq_use:Nn \l__kadir_amsrefs_author_seq { ~and~ } }
   }
  \seq_put_left:Nx \l__kadir_amsrefs_output_seq { @#2\c_left_brace_str #1 }
  \seq_put_right:Nx \l__kadir_amsrefs_output_seq { \c_right_brace_str }
  \iow_now:Nx \g_kadir_amsrefs_stream
   {
    \seq_use:Nn \l__kadir_amsrefs_output_seq { ,^^J }
   }
 }

\keys_define:nn { kadir/amsrefs }
 {
  author  .code:n = \seq_put_right:Nn \l__kadir_amsrefs_author_seq { #1 },
  title   .code:n = \seq_put_right:Nn \l__kadir_amsrefs_output_seq { title = {{ #1 }} },
  pages   .code:n = \__kadir_amsrefs_pages:n { #1 },
  date    .code:n = \seq_put_right:Nn \l__kadir_amsrefs_output_seq { year = { #1 } },
  unknown .code:n = 
   \seq_put_right:Nx \l__kadir_amsrefs_output_seq
    { \l_keys_key_tl = { \exp_not:n { #1 } } },
 }

\cs_new_protected:Nn \__kadir_amsrefs_pages:n
 {
  \tl_set:Nn \l__kadir_amsrefs_pages_tl { #1 }
  \tl_replace_all:Nnn \l__kadir_amsrefs_pages_tl { \ndash } { - }
  \seq_put_right:Nx \l__kadir_amsrefs_output_seq
   { pages = { \exp_not:V \l__kadir_amsrefs_pages_tl } }
 }

\ExplSyntaxOff

\processamsrefs{\jobname}

\bib{BW}{article}{
author={Bertram, A.},
author={Wentworth, R.},
title={Gromov invariants for holomorphic
maps on Riemann surfaces},
date={1996},
journal={jams},
volume={9},
number={2},
pages={529\ndash 571},
}

\bib{Bourbaki70}{book}{
        title={Th\'eorie des ensembles},
        author={Bourbaki, Nicolas},
        date={1970},
        publisher={Hermann},
        address={Paris}
}

\stop

If you run this through LaTeX, you will get a .bib file with the contents

@article{BW,
author={Bertram, A. and Wentworth, R.},
title={{Gromov invariants for holomorphic maps on Riemann surfaces}},
year={1996},
journal={jams},
volume={9},
number={2},
pages={529-571},
}
@book{Bourbaki70,
author={Bourbaki, Nicolas},
title={{Th\'eorie des ensembles}},
year={1970},
publisher={Hermann},
address={Paris},
}

Use your preferred file name instead of \jobname.


Expanded version for coping with other complex keys.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\processamsrefs}{m}
 {
  \iow_open:Nn \g_kadir_amsrefs_stream { #1.bib }
 }
\NewDocumentCommand{\bib}{mmm}
 {
  \kadir_amsrefs_bib:nnn { #1 } { #2 } { #3 }
 }

\iow_new:N \g_kadir_amsrefs_stream
\seq_new:N \l__kadir_amsrefs_output_seq
\seq_new:N \l__kadir_amsrefs_author_seq
\tl_new:N \l__kadir_amsrefs_pages_tl

\cs_new_protected:Nn \kadir_amsrefs_bib:nnn
 {
  \seq_clear:N \l__kadir_amsrefs_output_seq
  \seq_clear:N \l__kadir_amsrefs_author_seq
  \keys_set:nn { kadir/amsrefs } { #3 }
  \seq_put_left:Nx \l__kadir_amsrefs_output_seq
   {
    author = { \seq_use:Nn \l__kadir_amsrefs_author_seq { ~and~ } }
   }
  \seq_put_left:Nx \l__kadir_amsrefs_output_seq { @#2\c_left_brace_str #1 }
  \seq_put_right:Nx \l__kadir_amsrefs_output_seq { \c_right_brace_str }
  \iow_now:Nx \g_kadir_amsrefs_stream
   {
    \seq_use:Nn \l__kadir_amsrefs_output_seq { ,^^J }
   }
 }

\keys_define:nn { kadir/amsrefs }
 {
  author     .code:n = \seq_put_right:Nn \l__kadir_amsrefs_author_seq { #1 },
  pages      .code:n = \__kadir_amsrefs_pages:n { #1 },
  title      .code:n = \__kadir_amsrefs_entry:nn { title } { #1 },
  date       .code:n = \__kadir_amsrefs_entry:nn { year } { #1 },
  address    .code:n = \__kadir_amsrefs_entry:nn { address} { #1 },
  place      .code:n = \__kadir_amsrefs_entry:nn { address } { #1 },
  conference .code:n = \keys_set:nn { kadir/amsrefs/conference } { #1 },
  book       .code:n = \keys_set:nn { kadir/amsrefs } { #1 },
  % subkeys
  conference/title .code:n = \__kadir_amsrefs_entry:nn { booktitle } { #1 },
  % other keys
  unknown .code:n = 
   \seq_put_right:Nx \l__kadir_amsrefs_output_seq
    { \l_keys_key_tl = { \exp_not:n { #1 } } },
 }

\cs_new_protected:Nn \__kadir_amsrefs_entry:nn
 {
  \seq_put_right:Nn \l__kadir_amsrefs_output_seq { #1 = { #2 } }
 }

\cs_new_protected:Nn \__kadir_amsrefs_pages:n
 {
  \tl_set:Nn \l__kadir_amsrefs_pages_tl { #1 }
  \tl_replace_all:Nnn \l__kadir_amsrefs_pages_tl { \ndash } { - }
  \seq_put_right:Nx \l__kadir_amsrefs_output_seq
   { pages = { \exp_not:V \l__kadir_amsrefs_pages_tl } }
 }

\ExplSyntaxOff

\processamsrefs{\jobname}

\bib{BW}{article}{
  author={Bertram, A.},
  author={Wentworth, R.},
  title={Gromov invariants for holomorphic
  maps on Riemann surfaces},
  date={1996},
  journal={jams},
  volume={9},
  number={2},
  pages={529\ndash 571},
}

\bib{Bourbaki70}{book}{
  title={Th\'eorie des ensembles},
  author={Bourbaki, Nicolas},
  date={1970},
  publisher={Hermann},
  address={Paris}
}
\bib{MR919828}{article}{
  author={Gersten, Steve M.},
  title={Reducible diagrams and equations over groups},
  conference={
   title={Essays in group theory},
  },
  book={
   series={Math. Sci. Res. Inst. Publ.},
   volume={8},
   publisher={Springer},
   place={New York},
  },
  date={1987},
  pages={15--73},
}
\stop

The .bib output is

@article{BW,
author={Bertram, A. and Wentworth, R.},
title={Gromov invariants for holomorphic maps on Riemann surfaces},
year={1996},
journal={jams},
volume={9},
number={2},
pages={529-571},
}
@book{Bourbaki70,
author={Bourbaki, Nicolas},
title={Th\'eorie des ensembles},
year={1970},
publisher={Hermann},
address={Paris},
}
@article{MR919828,
author={Gersten, Steve M.},
title={Reducible diagrams and equations over groups},
booktitle={Essays in group theory},
series={Math. Sci. Res. Inst. Publ.},
volume={8},
publisher={Springer},
address={New York},
year={1987},
pages={15--73},
}
egreg
  • 1,121,712
  • Add \usepackage{l3str} if this error occurs: Undefined control sequence. <argument> \c_right_brace_str l.69 } – Kadir Feb 07 '17 at 11:41
  • @Kadir Better, update your TeX distribution. ;-) – egreg Feb 07 '17 at 13:51
  • @egreg, Is it true that the program requires every "author" entry to end with a comma? That is not a requirement of the amsrefs format -- only the last author of a block of authors needs a comma. – Paulo Ney Feb 22 '18 at 06:06
  • Also the program does not seem to allow an empty line inside entries -- which is also allowed by AMSRefs. I was wondering which other assumptions is it making. – Paulo Ney Feb 22 '18 at 06:17
  • @PauloNey If I prepare a document using amsrefs with the two shown entries and I remove the comma between the two author fields, I get Package rkeyval Warning: Missing comma. So I won't change the code, because I don't think it's worth supporting coding that's not recommended. About the blank lines, just add \endlinechar=-1 after \processamsrefs{\jobname}. – egreg Feb 22 '18 at 09:58
  • @egreg It seems that it mangles up records of the type: \bib{MR919828}{article}{ author={Gersten, Steve M.}, title={Reducible diagrams and equations over groups}, conference={ title={Essays in group theory}, }, book={ series={Math. Sci. Res. Inst. Publ.}, volume={8}, publisher={Springer}, place={New York}, }, date={1987}, pages={15--73}, } You can see the record better if you edit it. Is there a fix for that? – Paulo Ney Feb 25 '18 at 03:56
  • @PauloNey I added the necessary keys for the example, but I feel that other problems may show up and long research should be done for dealing with all keys. – egreg Feb 25 '18 at 18:55
  • @egreg I ran it on a large amount of AMSRefs files I have and uncovered a few things: 1) the fields have all to be terminated by comma (not a requirement in AMSRefs. 2) The title of the fields have to be in lower-case (author and not AUTHOR) and 3) The recurrence mentioned above. 4) It does not translate all keys,just some. The idea is really nice and we can probably take it further. I am going to give it a try, since I need BibTeX DB's after converting to BibLaTeX. – Paulo Ney Feb 25 '18 at 20:52
  • @PauloNey This could be a project to be discussed in Rio. – egreg Feb 25 '18 at 21:14
  • Definitely! Can you give us a nice talk on the TUG Conference as well? – Paulo Ney Feb 25 '18 at 23:26
  • @PauloNey That's my intention. – egreg Feb 25 '18 at 23:30
4

The latex package https://www.ctan.org/pkg/ltb2bib does exactly this.

grok
  • 198