summaryrefslogtreecommitdiff
path: root/support/bundledoc/arlatex
blob: f0031654e224a383b33cb9792cabd996d76f9b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#! /usr/bin/env perl

###################################################
# Combine a bunch of ancillary files into a LaTeX #
# document using the filecontents* environment.   #
#                                                 #
# By Scott Pakin <scott+bdoc@pakin.org>           #
###################################################

########################################################################
# arlatex                                                              #
# Copyright (C) 2018-2022 Scott Pakin                                  #
#                                                                      #
# This program may be distributed and/or modified under the conditions #
# of the LaTeX Project Public License, either version 1.3c of this     #
# license or (at your option) any later version.                       #
#                                                                      #
# The latest version of this license is in:                            #
#                                                                      #
#    http://www.latex-project.org/lppl.txt                             #
#                                                                      #
# and version 1.3c or later is part of all distributions of LaTeX      #
# version 2008/05/04 or later.                                         #
#                                                                      #
# This program consists of the file bundledoc and all the files listed #
# in the Files section of the associated README file.                  #
########################################################################

use 5.006;                    # Fail gracefully if we're not using Perl v5.6.0.
our $VERSION = "1.1";         # Specify the version of arlatex.
use Getopt::Long;
use Pod::Usage;
use File::Basename;
use File::Find;
require File::Spec;
use strict;

my $progname = basename($0);  # Name of this program
my $latexfile;                # Main LaTeX document
my $outfile = "-";            # Output file (default is standard output)
my $baseoutfile = "-";        # Base name of output file

######################################################################

# Include a file using the filecontents* environment.
sub includefile ($)
{
    my $filename = $_[0];
    my $basefilename = basename $filename;
    next if $basefilename eq $baseoutfile;    # Don't let a file overwrite itself.
    $basefilename = "\"$basefilename\"" if $basefilename =~ /\s/ && substr($basefilename, 0, 1) ne '"';
    open (SOMEFILE, "<$filename") || die "${progname}: $! ($filename)\n";
    print OUTFILE "\\begin{filecontents*}[overwrite]{$basefilename}\n";
    print OUTFILE <SOMEFILE>;
    print OUTFILE "\\end{filecontents*}\n";
    close SOMEFILE;
}

######################################################################

# Parse the command line.
GetOptions ('version' => sub { print "arlatex $VERSION\n"; exit -1 },
            'help'=> sub {pod2usage (-exitval => 0, -verbose => 1)},
            'document=s' => \$latexfile,
            'outfile=s' => \$outfile) ||
    pod2usage (-exitval => -1, -verbose => 0);
pod2usage (-exitval => -1, -verbose => 0) if !defined $latexfile;
if (!-e $latexfile) {
    my $newlatexfile = "$latexfile.tex";
    if (!-e $newlatexfile) {
        die "${progname}: File $latexfile does not exist\n";
    }
    else {
        $latexfile = $newlatexfile;
    }
}
$baseoutfile = basename $outfile if $outfile ne "-";

# Read the entire input file.
open (LATEXFILE, "<$latexfile") || die "${progname}: $! ($latexfile)\n";
my @entirefile = <LATEXFILE>;
close LATEXFILE;

# Open the output file, write any initial comments, and then include
# all of the files specified on the command line using the filecontents*
# environment.  Finally, include the rest of the input file.
open (OUTFILE, ">$outfile") || die "${progname}: $! ($outfile)\n";
while ($entirefile[0] =~ /^\s*(\%.*)?$/) {
    print OUTFILE (shift @entirefile);
}
my @expandedARGV;
foreach my $filename (@ARGV) {
    if (-f $filename) {
        push @expandedARGV, $filename;
    }
    else {
        find ({wanted => sub {push @expandedARGV, File::Spec->rel2abs($_) if -f $_},
               follow => 1},
              $filename);
    }
}
foreach my $filename (@expandedARGV) {
    includefile ($filename);
    print OUTFILE "\n";
}
print OUTFILE @entirefile;
close OUTFILE;
exit 0;

######################################################################

=head1 NAME

arlatex - archive a number of ancillary LaTeX files into a master
F<.tex> file


=head1 SYNOPSIS

arlatex
[B<--outfile>=I<filename>F<.tex>]
B<--document>=I<filename>F<.tex>
I<filename>
...

arlatex
B<--version>

arlatex
B<--help>


=head1 DESCRIPTION

B<arlatex> is an archiving program like B<shar>, B<tar>, B<zip>, etc.
Unlike those other archivers, however, B<arlatex> is designed
specifically for use with LaTeX.  B<arlatex> takes the name of a
master F<.tex> file and a number of ancillary files used by that
master file (e.g., F<.tex>, F<.sty>, F<.cls>, and F<.eps> files).
From these, B<arlatex> outputs a single file that, when it's run
through B<latex>, both regenerates the ancillary files and compiles
the document into a F<.dvi> file.

B<arlatex> has a few advantages over other archiving programs:

=over 4

=item *

The F<.tex> files produced by B<arlatex> are in a plain-text format.
They are therefore perfectly portable and trivial to e-mail to
colleagues.

=item *

B<arlatex> needs only LaTeX to run.  There is no dependence on any
external tools.

=item *

There is no explicit extraction step.  As the generated document is
run through B<latex>, it extracts the ancillary files and builds the
document in the same step.  The user running B<latex> may not even
notice that additional files are being produced.

=back

B<arlatex> works by writing a number of S<C<\begin{filecontents*}> ...>
C<\end{filecontents*}> blocks to the output file, followed by the
contents of the master file.  (In fact, any LaTeX comments at the
beginning of the master file are hoisted to the top of the generated
file.  This enables the author to draw attention, if so desired, to
the fact that ancillary files will be generated.)  The
C<filecontents*> environment, part of standard LaTeX2e, writes its
contents verbatim to a specified file.


=head1 OPTIONS

=over 4

=item B<--version>

Output the B<arlatex> script's version number.

=item B<--help>

Output brief B<arlatex> usage information.

=item B<--document>=I<.tex file>

Specify the master document.  The output from B<arlatex> is this file
with all of the other files named on the command line prepended to it.
Note that C<--document> is a I<mandatory> parameter; B<arlatex> will
abort with an error message if C<--document> is not specified.

=item B<--outfile>=I<.tex file>

Specify the output file.  The output file looks just like the master
document, but with a number of C<filecontents*> environments preceding
the C<\documentclass> line.  If C<--outfile> is not specified, output
will be written to the standard output device.

=back


=head1 EXAMPLES

Suppose you have a paper called F<paper.tex> that loads a custom
package with C<\usepackage{mypackage}>.  You want to submit the paper
to a conference, but you want to be absolutely certain that
F<mypackage.sty> doesn't get lost as your paper is shuttled from
person to person.  Here's how B<arlatex> can be of use:

    arlatex --document=paper.tex mypackage.sty --outfile=paper-submit.tex

When F<paper-submit.tex> is processed with B<latex>, it builds just
like the original F<paper.tex>, except that it additionally creates a
F<mypackage.sty> in the current directory:

    This is TeX, Version 3.14159 (Web2C 7.3.1)
    (paper-submit.tex
    LaTeX2e <1999/12/01> patch level 1
    Babel <v3.6Z> and hyphenation patterns for american, french, german,
    ngerman, italian, nohyphenation, loaded.

    LaTeX Warning: Writing file `./mypackage.sty'.

    (/usr/share/texmf/tex/latex/base/article.cls
    Document Class: article 1999/09/10 v1.4a Standard LaTeX document class
    (/usr/share/texmf/tex/latex/base/size12.clo)) (mypackage.sty)
                                  .
                                  .
                                  .

As another example, here's how you could bundle together all of the
files needed to build a large document for longevity:

    arlatex --document=thesis.tex abstract.tex introduction.tex
      background.tex approach.tex experiments.tex relatedwork.tex
      conclusions.tex before.eps after.eps fast.eps slow.eps
      podunkUthesis.cls --outfile=thesis-all.tex

As the number of files to archive together increases it becomes more
cumbersome to run B<arlatex> manually.  Fortunately, using B<arlatex>
with B<bundledoc> is straightforward.  B<bundledoc> finds all of the
files needed to build the document, and B<arlatex> combines them into
a single file.  The following are examples of the C<bundle:> line you
might use in a B<bundledoc> configuration file:

=over 1

=item Unix:

   bundle: (arlatex --document=$BDBASE.tex $BDINPUTS \
     --outfile=$BDBASE-all.tex)

=item Windows:

   bundle: arlatex --document=%BDBASE%.tex %BDINPUTS% \
     --outfile=%BDBASE%-all.tex

=back

See the B<bundledoc> documentation for more information.


=head1 CAVEATS

B<arlatex> makes use of LaTeX's C<filecontents*> environment.  It
passes C<filecontents*> the C<overwrite> option to indicate that
existing files with the same name should be overwritten.  Be
forewarned that building the generated file will overwrite the files
used to create it.

It is best to avoid bundling binary files (e.g., included graphics)
with B<arlatex>.  These typically do not extract cleanly.


=head1 SEE ALSO

bundledoc(1), latex(1), shar(1), tar(1), zip(1)


=head1 AUTHOR

Scott Pakin, I<scott+bdoc@pakin.org>