Debian Codegraph @

texmf-dist/source/latex/ltb2bib/ltb2bib.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2017 by Laurent Bartholdi <[email protected]>
4 % ---------------------------------------------------------------------------
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.3
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3 or later is part of all distributions of LaTeX
11 % version 2005/12/01 or later.
12 %
13 % This work has the LPPL maintenance status `maintained'.
14 %
15 % The Current Maintainer of this work is Laurent Bartholdi.
16 %
17 % This work consists of the files ltb2bib.dtx and ltb2bib.ins
18 % and the derived filebase ltb2bib.sty.
19 %
20 % \fi
21 %
22 % \iffalse
23 %<*driver>
24 \ProvidesFile{ltb2bib.dtx}
25 %</driver>
26 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
27 %<package>\ProvidesPackage{ltb2bib}
28 %<*package>
29 [2017/04/03 0.01 First release in the wild]
30 %</package>
31 %
32 %<*driver>
33 \documentclass{ltxdoc}
34 \EnableCrossrefs
35 \CodelineIndex
36 \RecordChanges
37 \begin{document}
38 \DocInput{ltb2bib.dtx}
39 \PrintChanges
40 \PrintIndex
41 \end{document}
42 %</driver>
43 % \fi
44 %
45 % \CheckSum{452}
46 %
47 % \CharacterTable
48 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
49 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
50 % Digits \0\1\2\3\4\5\6\7\8\9
51 % Exclamation \! Double quote \" Hash (number) \#
52 % Dollar \$ Percent \% Ampersand \&
53 % Acute accent \' Left paren \( Right paren \)
54 % Asterisk \* Plus \+ Comma \,
55 % Minus \- Point \. Solidus \/
56 % Colon \: Semicolon \; Less than \<
57 % Equals \= Greater than \> Question mark \?
58 % Commercial at \@ Left bracket \[ Backslash \\
59 % Right bracket \] Circumflex \^ Underscore \_
60 % Grave accent \` Left brace \{ Vertical bar \|
61 % Right brace \} Tilde \~}
62 %
63 %
64 % \changes{0.01}{2017/04/03}{Converted to DTX file}
65 %
66 % \DoNotIndex{\def,\newwrite,\newskip,\newenvironment,\gdef,\immediate,\let}
67 %
68 % \providecommand*{\url}{\texttt}
69 % \GetFileInfo{ltb2bib.dtx}
70 % \title{The \textsf{ltb2bib} package}
71 % \author{Laurent Bartholdi \\ \url{[email protected]}}
72 % \date{\fileversion~from \filedate}
73 %
74 % \maketitle
75 %
76 % \section{Introduction}
77 %
78 % The \textsf{amsrefs} package is a wonderful, pure-\LaTeX\
79 % implementation of bibliographical databases. In a format close to
80 % Bib\TeX's, one writes entries in database files (say
81 % \verb+ltbsource.ltb+); \LaTeX\ extracts from these files the entries
82 % required for the document (say \verb+myarticle.tex+), writes them to
83 % myarticle.bbl and incorporates them in the document's bibliography
84 % section. The file \verb+myarticle.bbl+ can be sent to the publisher
85 % along with \verb+myarticle.tex+, or can be directly incorporated
86 % into it.
87 %
88 % It happens, sometimes, that one needs the bibliographical entries in
89 % Bib\TeX\ format:
90 % \begin{itemize}
91 % \item Some publishers (Taylor \& Francis, for example) refuse
92 % to process \textsf{amsrefs} files, and require the authors to manually
93 % retype them;
94 % \item \textsf{amsrefs} cannot sort entries, so it may be useful to
95 % convert a \textsf{amsrefs} database to Bib\TeX\ format to feed them to
96 % bibliography management software;
97 % \item \textsf{emacs}'s Ref\TeX\ mode, for example, requires the database
98 % in Bib\TeX\ format.
99 % \end{itemize}
100 %
101 % For these purposes, I have written this short \LaTeX\ package that
102 % converts an \textsf{amsrefs} bibliography file to Bib\TeX.
103 %
104 % Note that the inverse operation, namely producing a \textsf{amsrefs}
105 % bibliographical database out of a Bib\TeX\ one, is implemented by
106 % the \verb+amsxport+ option of \textsf{amsrefs}.
107 %
108 % \section{Usage}
109 %
110 % The only command supplied by this package is \verb+\writebib+. It is
111 % typically used in a driver-document \verb+doltb2bib.tex+ such as
112 % the following:
113 % \begin{verbatim}
114 % \documentclass{article}
115 % \usepackage{ltb2bib}
116 % \begin{document}
117 % \writebib{ltbsource}{bibdest}
118 % \end{document}
119 % \end{verbatim}
120 % which, when invoked with `\verb+latex doltb2bib+', produces from
121 % \verb+ltbsource.ltb+ the file \verb+bibdest.bib+.
122 %
123 % \DescribeMacro{\writebib}
124 %
125 % This command takes two arguments: an input and an output
126 % filename. If the input filename is given without extension, then the
127 % actual filename may have the extension \verb+.ltb+; and similarly
128 % the output will be written to a file with the \verb+.bib+ extension
129 % added.
130 %
131 % This command reads all \verb+\bib+ entries from the input file, and
132 % writes corresponding Bib\TeX\ entries to the output file. It produces no
133 % output.
134 %
135 % \StopEventually{}
136 %
137 % \section{Implementation}
138 %
139 % \iffalse
140 %<*package>
141 % \fi
142 %
143 % \begin{macrocode}
144 \ProvidesPackage{ltb2bib}[2017/04/03 v0.01]
145 \@ifundefined{thebibliography}{\newenvironment{thebibliography}{}{}}{}
146 \@ifundefined{footnotesize}{\let\footnotesize\relax}{}
147 \RequirePackage{amsrefs}
148 \RequirePackage{xpatch}
149 \newwrite\@bibfile
150 % \end{macrocode}
151 % make a copy of protected@iwrite, and patch it to add \verb+\immediate+
152 % \begin{macrocode}
153 \let\protected@iwrite\protected@write
154 \xpatchcmd{\protected@iwrite}{\write}{\immediate\write}{}{}
155 \def\bib@write#1{\protected@iwrite\@bibfile{\let~\relax\def\ndash{--}}{#1}}
156 \gdef\space{ }
157 \gdef\spsp{\space\space}
158 % \end{macrocode}
159 %
160 % treat the multiple entries to be coalesced, with \verb+\name+ prepended to names
161 % \begin{macro}{\@author}
162 % \begin{macrocode}
163 \def\@author#1{\toks0={}\toks1={}%
164 \def\name##1{\toks0=\expandafter{\the\toks0\the\toks1{##1}}\toks1={\spsp and }}%
165 #1
166 \bib@write{\spsp author={\the\toks0},}%
167 }
168 % \end{macrocode}
169 % \end{macro}
170 %
171 %
172 % \begin{macro}{\@editor}
173 % \begin{macrocode}
174 \def\@editor#1{\toks0={}\toks1={}%
175 \def\name##1{\toks0=\expandafter{\the\toks0\the\toks1{##1}}\toks1={\spsp and }}%
176 #1
177 \bib@write{\spsp editor={\the\toks0},}%
178 }
179 % \end{macrocode}
180 % \end{macro}
181 %
182 %
183 % \begin{macro}{\@translator}
184 % \begin{macrocode}
185 \def\@translator#1{\toks0={}\toks1={}%
186 \def\name##1{\toks0=\expandafter{\the\toks0\the\toks1{##1}}\toks1={\spsp and }}%
187 #1
188 \bib@write{\spsp translator={\the\toks0},}%
189 }
190 % \end{macrocode}
191 % \end{macro}
192 %
193 %
194 % Keys which may appear multiply, but which we don't coalesce
195 % \begin{macrocode}
196 \def\@contribution#1{\bib@write{\spsp contribution=\@charlb#1\@charrb,}}
197 \def\@isbn#1{\def\fld@elt##1{\bib@write{\spsp isbn=\@charlb##1\@charrb,}}#1}
198 \def\@issn#1{\def\fld@elt##1{\bib@write{\spsp issn=\@charlb##1\@charrb,}}#1}
199 \def\@review#1{\def\MR##1{\let\MR\relax\bib@write{\spsp review={\MR{##1}},}}#1}
200 \def\@partial#1{\bib@write{\spsp partial=\@charlb#1\@charrb,}}
201 % \end{macrocode}
202 %
203 % Keys which translate directly to simple keys
204 % \begin{macrocode}
205 \def\@accessdate#1{\bib@write{\spsp accessdate=\@charlb#1\@charrb,}}
206 \def\@address#1{\bib@write{\spsp address=\@charlb#1\@charrb,}}
207 \def\@book#1{\bib@write{\spsp book=\@charlb#1\@charrb,}}
208 \def\@booktitle#1{\bib@write{\spsp booktitle=\@charlb#1\@charrb,}}
209 \def\@conference#1{\bib@write{\spsp conference=\@charlb#1\@charrb,}}
210 \def\@copula#1{\bib@write{\spsp copula=\@charlb#1\@charrb,}}
211 \def\@date#1{\bib@write{\spsp year=\@charlb#1\@charrb,}}
212 \def\@doi#1{\bib@write{\spsp doi=\@charlb#1\@charrb,}}
213 \def\@edition#1{\bib@write{\spsp edition=\@charlb#1\@charrb,}}
214 \def\@eprint#1{\bib@write{\spsp eprint=\@charlb#1\@charrb,}}
215 \def\@fulljournal#1{\bib@write{\spsp fulljournal=\@charlb#1\@charrb,}}
216 \def\@hyphenation#1{\bib@write{\spsp hyphenation=\@charlb#1\@charrb,}}
217 \def\@institution#1{\bib@write{\spsp institution=\@charlb#1\@charrb,}}
218 \def\@journal#1{\bib@write{\spsp journal=\@charlb#1\@charrb,}}
219 \def\@label#1{\bib@write{\spsp label=\@charlb#1\@charrb,}}
220 \def\@language#1{\bib@write{\spsp language=\@charlb#1\@charrb,}}
221 \def\@name#1{\bib@write{\spsp name=\@charlb#1\@charrb,}}
222 \def\@note#1{\bib@write{\spsp note=\@charlb#1\@charrb,}}
223 \def\@number#1{\bib@write{\spsp number=\@charlb#1\@charrb,}}
224 \def\@organization#1{\bib@write{\spsp organization=\@charlb#1\@charrb,}}
225 \def\@pages#1{\bib@write{\spsp pages=\@charlb#1\@charrb,}}
226 \def\@part#1{\bib@write{\spsp part=\@charlb#1\@charrb,}}
227 \def\@place#1{\bib@write{\spsp place=\@charlb#1\@charrb,}}
228 \def\@publisher#1{\bib@write{\spsp publisher=\@charlb#1\@charrb,}}
229 \def\@reprint#1{\bib@write{\spsp reprint=\@charlb#1\@charrb,}}
230 \def\@school#1{\bib@write{\spsp school=\@charlb#1\@charrb,}}
231 \def\@series#1{\bib@write{\spsp series=\@charlb#1\@charrb,}}
232 \def\@setup#1{\bib@write{\spsp setup=\@charlb#1\@charrb,}}
233 \def\@status#1{\bib@write{\spsp status=\@charlb#1\@charrb,}}
234 \def\@subtitle#1{\bib@write{\spsp subtitle=\@charlb#1\@charrb,}}
235 \def\@title#1{\bib@write{\spsp title=\@charlb#1\@charrb,}}
236 \def\@translation#1{\bib@write{\spsp translation=\@charlb#1\@charrb,}}
237 \def\@type#1{\bib@write{\spsp type=\@charlb#1\@charrb,}}
238 \def\@url#1{\bib@write{\spsp url=\@charlb#1\@charrb,}}
239 \def\@volume#1{\bib@write{\spsp volume=\@charlb#1\@charrb,}}
240 \def\@xref#1{\bib@write{\spsp xref=\@charlb#1\@charrb,}}
241 \def\@year#1{\bib@write{\spsp year=\@charlb#1\@charrb,}}
242 % \end{macrocode}
243 %
244 % We create a bib specification to output the data in Bib\TeX\ format,
245 % rather than as a formatted bibliography.
246 % \begin{macro}{\make@bibspec}
247 % \begin{macrocode}
248 \def\make@bibspec#1#2{\BibSpec{#1}{%
249 +{}{\bib@write{@#2\@charlb\current@citekey,}}{transition}
250 +{}{\@author} {author}
251 +{}{\@editor} {editor}
252 +{}{\@translator} {translator}
253 +{}{\@contribution} {contribution}
254 +{}{\@isbn} {isbn}
255 +{}{\@issn} {issn}
256 +{}{\@review} {review}
257 +{}{\@partial} {partial}
258 +{}{\@accessdate} {accessdate}
259 +{}{\@address} {address}
260 +{}{\@book} {book}
261 +{}{\@booktitle} {booktitle}
262 +{}{\@conference} {conference}
263 +{}{\@copula} {copula}
264 +{}{\@date} {date}
265 +{}{\@doi} {doi}
266 +{}{\@edition} {edition}
267 +{}{\@eprint} {eprint}
268 +{}{\@fulljournal} {fulljournal}
269 +{}{\@hyphenation} {hyphenation}
270 +{}{\@institution} {institution}
271 +{}{\@journal} {journal}
272 +{}{\@label} {label}
273 +{}{\@language} {language}
274 +{}{\@name} {name}
275 +{}{\@note} {note}
276 +{}{\@number} {number}
277 +{}{\@organization} {organization}
278 +{}{\@pages} {pages}
279 +{}{\@part} {part}
280 +{}{\@place} {place}
281 +{}{\@publisher} {publisher}
282 +{}{\@reprint} {reprint}
283 +{}{\@school} {school}
284 +{}{\@series} {series}
285 +{}{\@setup} {setup}
286 +{}{\@status} {status}
287 +{}{\@subtitle} {subtitle}
288 +{}{\@title} {title}
289 +{}{\@translation} {translation}
290 +{}{\@type} {type}
291 +{}{\@url} {url}
292 +{}{\@volume} {volume}
293 +{}{\@xref} {xref}
294 +{}{\@year} {year}
295 +{}{\bib@write{\@charrb^^J}}{transition}
296 }}
297 % \end{macrocode}
298 % \end{macro}
299 %
300 % amsrefs treats bib entries as items in an enumeration. We'll just make
301 % sure nothing is produced in the output, and set the paragraph skips between
302 % items to be 0.
303 % \begin{macrocode}
304 \newskip\ltb@baselineskip
305 \newskip\ltb@parskip
306 % \end{macrocode}
307 %
308 % The main command: just run \verb+bibselect+ on the input file, after
309 % having set up the desired bibspec.
310 % \begin{macro}{\writebib}
311 % \begin{macrocode}
312 \newcommand\writebib[2]{%
313 \immediate\openout\@bibfile=#2.bib
314 \bib@write{\@percentchar\space Converted bibliography from #1[.ltb] to #2.bib}%
315 \typeout{Writing bibliography to #2.bib...}%
316 \make@bibspec{article}{Article}%
317 \make@bibspec{partial}{InCollection}%
318 \make@bibspec{contribution}{InCollection}%
319 \make@bibspec{book}{Book}%
320 \make@bibspec{collection.article}{InCollection}%
321 \make@bibspec{conference}{Conference}%
322 \make@bibspec{innerbook}{InBook}%
323 \make@bibspec{report}{TechReport}%
324 \make@bibspec{thesis}{PhdThesis}%
325 \make@bibspec{webpage}{Misc}%
326 % \end{macrocode}
327 % Set paragraph separation to 0, so that all \verb+\item+s produced by
328 % \textsf{amsrefs} appear superimposed in the output:
329 % \begin{macrocode}
330 \ltb@parskip=\parskip
331 \ltb@baselineskip=\baselineskip
332 \let\ltb@item=\item
333 \parskip=0pt
334 \baselineskip=0pt
335 \let\item=\relax
336 % \end{macrocode}
337 % \verb+\fld@elt+ is defined to be ``?'' by
338 % \textsf{amsrefs}. Sometimes this character gets shipped out; we
339 % simply remove it by redefining \verb+\fld@elt+.
340 % \begin{macrocode}
341 \let\fld@elt=\relax
342 \bibselect*{#1}%
343 \bib@write{\@percentchar\space End of #2.bib}%
344 \immediate\closeout\@bibfile
345 \parskip=\ltb@parskip
346 \baselineskip=\ltb@baselineskip
347 \let\item=\ltb@item
348 }
349 % \end{macrocode}
350 % \end{macro}
351 %
352
353 %
354 % \iffalse
355 %</package>
356 % \fi
357 %
358 % \Finale
359 \endinput