| 1 |
#! /usr/bin/env perl |
| 2 |
|
| 3 |
################################################### |
| 4 |
# Combine a bunch of ancillary files into a LaTeX # |
| 5 |
# document using the filecontents* environment. # |
| 6 |
# # |
| 7 |
# By Scott Pakin <[email protected]> # |
| 8 |
################################################### |
| 9 |
|
| 10 |
######################################################################## |
| 11 |
# arlatex # |
| 12 |
# Copyright (C) 2018-2025 Scott Pakin # |
| 13 |
# # |
| 14 |
# This program may be distributed and/or modified under the conditions # |
| 15 |
# of the LaTeX Project Public License, either version 1.3c of this # |
| 16 |
# license or (at your option) any later version. # |
| 17 |
# # |
| 18 |
# The latest version of this license is in: # |
| 19 |
# # |
| 20 |
# http://www.latex-project.org/lppl.txt # |
| 21 |
# # |
| 22 |
# and version 1.3c or later is part of all distributions of LaTeX # |
| 23 |
# version 2008/05/04 or later. # |
| 24 |
# # |
| 25 |
# This program consists of the file bundledoc and all the files listed # |
| 26 |
# in the Files section of the associated README file. # |
| 27 |
######################################################################## |
| 28 |
|
| 29 |
use 5.006; # Fail gracefully if we're not using Perl v5.6.0. |
| 30 |
our $VERSION = "1.1"; # Specify the version of arlatex. |
| 31 |
use Getopt::Long; |
| 32 |
use Pod::Usage; |
| 33 |
use File::Basename; |
| 34 |
use File::Find; |
| 35 |
require File::Spec; |
| 36 |
use strict; |
| 37 |
|
| 38 |
my $progname = basename($0); # Name of this program |
| 39 |
my $latexfile; # Main LaTeX document |
| 40 |
my $outfile = "-"; # Output file (default is standard output) |
| 41 |
my $baseoutfile = "-"; # Base name of output file |
| 42 |
|
| 43 |
###################################################################### |
| 44 |
|
| 45 |
# Include a file using the filecontents* environment. |
| 46 |
sub includefile ($) |
| 47 |
{ |
| 48 |
my $filename = $_[0]; |
| 49 |
my $basefilename = basename $filename; |
| 50 |
next if $basefilename eq $baseoutfile; # Don't let a file overwrite itself. |
| 51 |
$basefilename = "\"$basefilename\"" if $basefilename =~ /\s/ && substr($basefilename, 0, 1) ne '"'; |
| 52 |
open (SOMEFILE, "<$filename") || die "${progname}: $! ($filename)\n"; |
| 53 |
print OUTFILE "\\begin{filecontents*}[overwrite]{$basefilename}\n"; |
| 54 |
print OUTFILE <SOMEFILE>; |
| 55 |
print OUTFILE "\\end{filecontents*}\n"; |
| 56 |
close SOMEFILE; |
| 57 |
} |
| 58 |
|
| 59 |
###################################################################### |
| 60 |
|
| 61 |
# Parse the command line. |
| 62 |
GetOptions ('version' => sub { print "arlatex $VERSION\n"; exit -1 }, |
| 63 |
'help'=> sub {pod2usage (-exitval => 0, -verbose => 1)}, |
| 64 |
'document=s' => \$latexfile, |
| 65 |
'outfile=s' => \$outfile) || |
| 66 |
pod2usage (-exitval => -1, -verbose => 0); |
| 67 |
pod2usage (-exitval => -1, -verbose => 0) if !defined $latexfile; |
| 68 |
if (!-e $latexfile) { |
| 69 |
my $newlatexfile = "$latexfile.tex"; |
| 70 |
if (!-e $newlatexfile) { |
| 71 |
die "${progname}: File $latexfile does not exist\n"; |
| 72 |
} |
| 73 |
else { |
| 74 |
$latexfile = $newlatexfile; |
| 75 |
} |
| 76 |
} |
| 77 |
$baseoutfile = basename $outfile if $outfile ne "-"; |
| 78 |
|
| 79 |
# Read the entire input file. |
| 80 |
open (LATEXFILE, "<$latexfile") || die "${progname}: $! ($latexfile)\n"; |
| 81 |
my @entirefile = <LATEXFILE>; |
| 82 |
close LATEXFILE; |
| 83 |
|
| 84 |
# Open the output file, write any initial comments, and then include |
| 85 |
# all of the files specified on the command line using the filecontents* |
| 86 |
# environment. Finally, include the rest of the input file. |
| 87 |
open (OUTFILE, ">$outfile") || die "${progname}: $! ($outfile)\n"; |
| 88 |
while ($entirefile[0] =~ /^\s*(\%.*)?$/) { |
| 89 |
print OUTFILE (shift @entirefile); |
| 90 |
} |
| 91 |
my @expandedARGV; |
| 92 |
foreach my $filename (@ARGV) { |
| 93 |
if (-f $filename) { |
| 94 |
push @expandedARGV, $filename; |
| 95 |
} |
| 96 |
else { |
| 97 |
find ({wanted => sub {push @expandedARGV, File::Spec->rel2abs($_) if -f $_}, |
| 98 |
follow => 1}, |
| 99 |
$filename); |
| 100 |
} |
| 101 |
} |
| 102 |
foreach my $filename (@expandedARGV) { |
| 103 |
includefile ($filename); |
| 104 |
print OUTFILE "\n"; |
| 105 |
} |
| 106 |
print OUTFILE @entirefile; |
| 107 |
close OUTFILE; |
| 108 |
exit 0; |
| 109 |
|
| 110 |
###################################################################### |
| 111 |
|
| 112 |
=head1 NAME |
| 113 |
|
| 114 |
arlatex - archive a number of ancillary LaTeX files into a master |
| 115 |
F<.tex> file |
| 116 |
|
| 117 |
|
| 118 |
=head1 SYNOPSIS |
| 119 |
|
| 120 |
arlatex |
| 121 |
[B<--outfile>=I<filename>F<.tex>] |
| 122 |
B<--document>=I<filename>F<.tex> |
| 123 |
I<filename> |
| 124 |
... |
| 125 |
|
| 126 |
arlatex |
| 127 |
B<--version> |
| 128 |
|
| 129 |
arlatex |
| 130 |
B<--help> |
| 131 |
|
| 132 |
|
| 133 |
=head1 DESCRIPTION |
| 134 |
|
| 135 |
B<arlatex> is an archiving program like B<shar>, B<tar>, B<zip>, etc. |
| 136 |
Unlike those other archivers, however, B<arlatex> is designed |
| 137 |
specifically for use with LaTeX. B<arlatex> takes the name of a |
| 138 |
master F<.tex> file and a number of ancillary files used by that |
| 139 |
master file (e.g., F<.tex>, F<.sty>, F<.cls>, and F<.eps> files). |
| 140 |
From these, B<arlatex> outputs a single file that, when it's run |
| 141 |
through B<latex>, both regenerates the ancillary files and compiles |
| 142 |
the document into a F<.dvi> file. |
| 143 |
|
| 144 |
B<arlatex> has a few advantages over other archiving programs: |
| 145 |
|
| 146 |
=over 4 |
| 147 |
|
| 148 |
=item * |
| 149 |
|
| 150 |
The F<.tex> files produced by B<arlatex> are in a plain-text format. |
| 151 |
They are therefore perfectly portable and trivial to e-mail to |
| 152 |
colleagues. |
| 153 |
|
| 154 |
=item * |
| 155 |
|
| 156 |
B<arlatex> needs only LaTeX to run. There is no dependence on any |
| 157 |
external tools. |
| 158 |
|
| 159 |
=item * |
| 160 |
|
| 161 |
There is no explicit extraction step. As the generated document is |
| 162 |
run through B<latex>, it extracts the ancillary files and builds the |
| 163 |
document in the same step. The user running B<latex> may not even |
| 164 |
notice that additional files are being produced. |
| 165 |
|
| 166 |
=back |
| 167 |
|
| 168 |
B<arlatex> works by writing a number of S<C<\begin{filecontents*}> ...> |
| 169 |
C<\end{filecontents*}> blocks to the output file, followed by the |
| 170 |
contents of the master file. (In fact, any LaTeX comments at the |
| 171 |
beginning of the master file are hoisted to the top of the generated |
| 172 |
file. This enables the author to draw attention, if so desired, to |
| 173 |
the fact that ancillary files will be generated.) The |
| 174 |
C<filecontents*> environment, part of standard LaTeX2e, writes its |
| 175 |
contents verbatim to a specified file. |
| 176 |
|
| 177 |
|
| 178 |
=head1 OPTIONS |
| 179 |
|
| 180 |
=over 4 |
| 181 |
|
| 182 |
=item B<--version> |
| 183 |
|
| 184 |
Output the B<arlatex> script's version number. |
| 185 |
|
| 186 |
=item B<--help> |
| 187 |
|
| 188 |
Output brief B<arlatex> usage information. |
| 189 |
|
| 190 |
=item B<--document>=I<.tex file> |
| 191 |
|
| 192 |
Specify the master document. The output from B<arlatex> is this file |
| 193 |
with all of the other files named on the command line prepended to it. |
| 194 |
Note that C<--document> is a I<mandatory> parameter; B<arlatex> will |
| 195 |
abort with an error message if C<--document> is not specified. |
| 196 |
|
| 197 |
=item B<--outfile>=I<.tex file> |
| 198 |
|
| 199 |
Specify the output file. The output file looks just like the master |
| 200 |
document, but with a number of C<filecontents*> environments preceding |
| 201 |
the C<\documentclass> line. If C<--outfile> is not specified, output |
| 202 |
will be written to the standard output device. |
| 203 |
|
| 204 |
=back |
| 205 |
|
| 206 |
|
| 207 |
=head1 EXAMPLES |
| 208 |
|
| 209 |
Suppose you have a paper called F<paper.tex> that loads a custom |
| 210 |
package with C<\usepackage{mypackage}>. You want to submit the paper |
| 211 |
to a conference, but you want to be absolutely certain that |
| 212 |
F<mypackage.sty> doesn't get lost as your paper is shuttled from |
| 213 |
person to person. Here's how B<arlatex> can be of use: |
| 214 |
|
| 215 |
arlatex --document=paper.tex mypackage.sty --outfile=paper-submit.tex |
| 216 |
|
| 217 |
When F<paper-submit.tex> is processed with B<latex>, it builds just |
| 218 |
like the original F<paper.tex>, except that it additionally creates a |
| 219 |
F<mypackage.sty> in the current directory: |
| 220 |
|
| 221 |
This is TeX, Version 3.14159 (Web2C 7.3.1) |
| 222 |
(paper-submit.tex |
| 223 |
LaTeX2e <1999/12/01> patch level 1 |
| 224 |
Babel <v3.6Z> and hyphenation patterns for american, french, german, |
| 225 |
ngerman, italian, nohyphenation, loaded. |
| 226 |
|
| 227 |
LaTeX Warning: Writing file `./mypackage.sty'. |
| 228 |
|
| 229 |
(/usr/share/texmf/tex/latex/base/article.cls |
| 230 |
Document Class: article 1999/09/10 v1.4a Standard LaTeX document class |
| 231 |
(/usr/share/texmf/tex/latex/base/size12.clo)) (mypackage.sty) |
| 232 |
. |
| 233 |
. |
| 234 |
. |
| 235 |
|
| 236 |
As another example, here's how you could bundle together all of the |
| 237 |
files needed to build a large document for longevity: |
| 238 |
|
| 239 |
arlatex --document=thesis.tex abstract.tex introduction.tex |
| 240 |
background.tex approach.tex experiments.tex relatedwork.tex |
| 241 |
conclusions.tex before.eps after.eps fast.eps slow.eps |
| 242 |
podunkUthesis.cls --outfile=thesis-all.tex |
| 243 |
|
| 244 |
As the number of files to archive together increases it becomes more |
| 245 |
cumbersome to run B<arlatex> manually. Fortunately, using B<arlatex> |
| 246 |
with B<bundledoc> is straightforward. B<bundledoc> finds all of the |
| 247 |
files needed to build the document, and B<arlatex> combines them into |
| 248 |
a single file. The following are examples of the C<bundle:> line you |
| 249 |
might use in a B<bundledoc> configuration file: |
| 250 |
|
| 251 |
=over 1 |
| 252 |
|
| 253 |
=item Unix: |
| 254 |
|
| 255 |
bundle: (arlatex --document=$BDBASE.tex $BDINPUTS \ |
| 256 |
--outfile=$BDBASE-all.tex) |
| 257 |
|
| 258 |
=item Windows: |
| 259 |
|
| 260 |
bundle: arlatex --document=%BDBASE%.tex %BDINPUTS% \ |
| 261 |
--outfile=%BDBASE%-all.tex |
| 262 |
|
| 263 |
=back |
| 264 |
|
| 265 |
See the B<bundledoc> documentation for more information. |
| 266 |
|
| 267 |
|
| 268 |
=head1 CAVEATS |
| 269 |
|
| 270 |
B<arlatex> makes use of LaTeX's C<filecontents*> environment. It |
| 271 |
passes C<filecontents*> the C<overwrite> option to indicate that |
| 272 |
existing files with the same name should be overwritten. Be |
| 273 |
forewarned that building the generated file will overwrite the files |
| 274 |
used to create it. |
| 275 |
|
| 276 |
It is best to avoid bundling binary files (e.g., included graphics) |
| 277 |
with B<arlatex>. These typically do not extract cleanly. |
| 278 |
|
| 279 |
|
| 280 |
=head1 SEE ALSO |
| 281 |
|
| 282 |
bundledoc(1), latex(1), shar(1), tar(1), zip(1) |
| 283 |
|
| 284 |
|
| 285 |
=head1 AUTHOR |
| 286 |
|
| 287 |
Scott Pakin, I<[email protected]> |