summaryrefslogtreecommitdiff
path: root/support/latexpand/latexpand
blob: e7de1965f1c2991edd18ca66b330f7c942d6e8ca (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#!/usr/bin/perl
# Inspired by latexpand by D. Musliner, University of Michigan
# 2012-2019: Matthieu Moy <git@matthieu-moy.fr>
# BSD License

use strict;
use Cwd;
use Getopt::Long;
use IO::Handle;
use File::Spec;

my $TEXINPUTS = $ENV{'TEXINPUTS'};
# By default, search in current directory. We use '.' and not getcwd()
# to avoid issues if the working directory contains a ':' character.
if (!$TEXINPUTS) { $TEXINPUTS = '.'; }

my $verbose;
my $keep_comments;
my $keep_includes;
my $empty_comments;
my $help;
my $long_help;
my $output;
my $explain;
my $show_graphics;
my $graphics_extensions = ":.pdf:.png:.jpg:.eps";
my $expand_usepackage;
my $expand_bbl;
my $biber;
my $fatal;
my $version;
my $makeatletter;
my $inside_import;
my $in_enc = "bytes";
my $out_enc = "bytes";

GetOptions (
	'h' => \$help,
	'help' => \$long_help,
	'verbose|v' => \$verbose,
	'keep-comments' => \$keep_comments,
	'keep-includes' => \$keep_includes,
	'empty-comments' => \$empty_comments,
	'output|o=s' => \$output,
	'explain' => \$explain,
	'show-graphics' => \$show_graphics,
	'graphics-extensions' => \$graphics_extensions,
	'expand-usepackage' => \$expand_usepackage,
	'expand-bbl=s' => \$expand_bbl,
	'biber=s' => \$biber,
	'fatal' => \$fatal,
	'version' => \$version,
        'makeatletter' => \$makeatletter,
	'in-encoding=s' => \$in_enc,
	'out-encoding=s' => \$out_enc,
) or pod2usage_wrapper(2);
version() if $version;
pod2usage_wrapper(0) if $help;
pod2usage_wrapper(-exitstatus => 0, -output => \*STDOUT, -verbose => 2) if $long_help;

sub pod2usage_wrapper
{
	# Like pod2usage, but fall back to a simpler implem in case
	# pod2usage can't be found.
	if  (eval {require Pod::Usage;1;} ne 1) {
		print "Please install perldoc and Pod::Usage to get proper help.\n";
		my $started = 0;
		open (my $in, '<', "$0") or die $!;
		while (<$in>) {
			if ($started) {
				print;
			}
			if (/^__END__$/) {
				$started = 1;
			}
		}
	} else {
		Pod::Usage->import();
		pod2usage(@_);
	}
}

sub get_version
{
	# $VERSION's value will be substituted by 'make dist', but the
	# next line won't (the string has to be broken to avoid it).
	my $VERSION = 'v1.6';
	if ($VERSION eq '@LATEXPAND' . '_VERSION@') {
		my($vol,$dir,$file) = File::Spec->splitpath($0);
		chdir($dir);
		$VERSION = `git describe --tags HEAD 2>/dev/null`;
	}
	if ($VERSION eq '') {
		$VERSION = '<unknown version>';
	}
	$VERSION =~ s/^\s+|\s+$//g;
	return $VERSION;
}

sub version
{
	print "latexpand version ". get_version() .".\n";
	exit(0);
}

my $nl = "";
if ($empty_comments) {
	$nl = "%\n";
}

if ($output && $output ne "-") {
	open (my $OUTPUT, '>', "$output") or die $!;
	STDOUT->fdopen(\*$OUTPUT, 'w') or die $!;
}

sub say
{
	if ($verbose) {
		print STDERR "$_[0]";
	}
}

my $makeatletter_found;
my $in_preamble;

use open IN  => ":$in_enc", OUT => ":$out_enc";

foreach my $file (@ARGV)
{
	say "processing $file\n";
	$makeatletter_found = 0;
	$in_preamble = 1;
        $inside_import = "";
	process_file($file, "  ");
}

sub cat_file
{
	my $file = shift;
	open (my $INFILE, "<", $file) || die "could not open input file '$file'\n";
	while (<$INFILE>) {
		print;
	}
	close ($INFILE);
}

sub process_file
{
	my $file = shift;
	my $prefix = (shift || "");
	my $in_comment = 0;
	open(my $FILE, "<", $file) or die "could not open input file '$file'\n";
	my $commented_newline = 0;
	while (my $line = <$FILE>) {
		if ($line =~ /^[ \t]*\\endinput/) {
			$line =~ s/(\\endinput.*)\n/% $1/;
			$in_comment = 1;
			process_line($line, $prefix, \$commented_newline);
			last;
		}
		process_line($line, $prefix, \$commented_newline, $file);
		if ($line =~ /^%.*[^\n]\z/ || $line =~ /[^\\]%.*[^\n]\z/) {
			# file ends with a comment not ending with a newline
			print "\n";
		}
		# Garbage at end of line after \end{document} is
		# ignored by LaTeX, but we don't allow anything before
		# to avoid e.g. \verb|\end{document}| from terminating
		# the file.
		if (!$keep_comments && $line =~ /^[ \t]*\\end\{document\}/) {
			last;
		}
	}
	close($FILE);
	return $in_comment;
}

sub process_line
{
	my ($line, $prefix, $commented_newline, $file) = @_;
	$_ = $line;
	if ($$commented_newline) {
		# Leading whitespaces after a comment is ignored.
		# There's no space in:
		# Line 1%
		#    Line 2.
		# Match just space and tabs (\s would match \n)
		s/^[ \t]*//;
		if (/^$/) {
			# Deal with:
			#
			# Line 1 % comment
			#
			# Line 2
			#
			# The newline after Line 1 is commented, but we still
			# want a new paragraph. We strip the comment together
			# with its newline, but re-add a newline to chnge
			# paragraph here if needed:
			print "\n";
		}
	}
	$$commented_newline = 0;
	# Consider \makeatletter only in preamble, because we do want
	# to warn on \someCommand{\makeatletter\command@with@arobase}.
	if ($in_preamble && /^[^%]*\\makeatletter/) {
		$makeatletter_found = 1;
	}
	if ($in_preamble && /^[^%]*\\makeatother/) {
		$makeatletter_found = 0;
	}
	my $command;
	if (!$makeatletter && !$makeatletter_found
	    && (($command) = /^[^%]*(\\[[:alpha:]]*@[[:alpha:]]*)/)
	    && ($command ne '\@')) {
		print STDERR "Warning: command $command containing @ found in\n";
		print STDERR "Warning: $file.\n";
		print STDERR "Warning: consider using --makeatletter if the result is not compilable.\n";
	}

        # non-comment is a sequence of:
        # - escaped character (\\.), including \% and \\
        # - neither '%' nor '\'.
        my $NON_COMMENT = '([^\\\\%]|\\\\.)*';

	unless ($keep_comments) {
		if (!$empty_comments) {
			# Include \n in pattern to avoid matching
			# comments at end of files

			# remove comments + whitespace-only lines completely
			if (s/^\s*%.*\n//) {
				$$commented_newline = 1;
			}

			# Special-case commands at end of line. We
			# don't want "\\foo%\nbar" to become
			# "\\foobar"
			if (s/^($NON_COMMENT\\[[:alpha:]@]+)%.*\n/$1 /) {
				$$commented_newline = 1;
			} elsif (s/^($NON_COMMENT)%.*\n/$1/) {
				# remove only the comment if the line has actual content
				$$commented_newline = 1;
			}
		}
		# Apply the "empty comments" treatment unconditionally
		# for comments not matched above (it doesn't harm to
		# keep an empty comment sometimes, but it may harm to
		# leave a real comment if the goal was to strip them).
		s/^(([^\\%]|\\.)*)%.*$/$1%/;
	}

	unless ($keep_includes) {
		# \input{foo.tex}
		my $ARGBRACES = '\{\\s*([^"}\\s][^}]*)(\\s*)\}';
		# \input{"foo bar.tex"}
		my $ARGQUOTED = '\{\\s*"([^"]*)"(\\s*)\}';
		# \input foo.tex
		my $ARGSPACES = '\\s([^\{\\s][^\\s]+?)\\s()';
		my $ARGUMENT = "\\s*?(?|$ARGBRACES|$ARGQUOTED|$ARGSPACES)";

		if (my ($before, $ignored, $full_filename, $trailing, $after)
		    = /^($NON_COMMENT)\\include$ARGUMENT(.*)$/) {
			$full_filename = find_tex_file($full_filename . ".tex");
			if ($full_filename) {
				say $prefix . "Found include for file: $full_filename\n";
				print $before . $nl;
				print '\clearpage{}' . $nl;
				print "% start include $full_filename\n" if ($explain);
				my $in_comment = process_file($full_filename, $prefix . "  ");
				if ($explain) {
				    print " % end include $full_filename\n";
				} elsif ($in_comment) {
				    print "\n";
				}
				print '\clearpage{}' . $nl;
				print $nl . $after . "\n";
				$_ = "";
			}
		} elsif (my ($before, $ignored, $full_filename, $trailing,  $after)
			 = /^($NON_COMMENT)\\input$ARGUMENT(.*)$/) {
                        if ($inside_import) {
                              $full_filename = $inside_import . $full_filename;
                        }
			$full_filename = find_tex_file($full_filename, ":.tex");
			if ($full_filename) {
				say $prefix . "Found input for file: $full_filename\n";
				# Surprisingly, space after filename
				# in \input{foo.tex } is inserted
				# _before_ the inclusion. Apply this
				# rule in latexpand.
				print $before . $trailing . $nl;
				print "% start input $full_filename\n" if ($explain);
				my $in_comment = process_file($full_filename, $prefix . "  ");
				if ($explain) {
				    print " % end input $full_filename\n";
				} elsif ($in_comment) {
				    print "\n";
				}
				if ($after =~ /[^\s]/) {
				    # LaTeX produces this space, so let's do it also
				    print " " . $nl . $after . "\n";
				} else {
				    print " ";
				}
				$_ = "";
			}
		} elsif (my ($before, $ignored, $dir, $ignored, $full_filename, $ignored,  $after)
			 = /^($NON_COMMENT)\\(?:sub)?import$ARGUMENT$ARGUMENT(.*)$/) {
                        if ($explain) {
                              print "% dir " . $dir ."\n";
                              print "% full_filename " . $full_filename ."\n";
                              print "% after " . $after ."\n";
                              print "% inside_import $inside_import\n";
                        }
                        $full_filename = $dir . $full_filename;
                        if ($inside_import) {
                              $full_filename = $inside_import . $full_filename;
                        }
                        print "% cat(inside_import,dir,full_filename) " . $full_filename ."\n" if ($explain);
			$full_filename = find_tex_file($full_filename, ":.tex");
			if ($full_filename) {
				say $prefix . "Found input for file: $full_filename\n";
				print $before . $nl;
				print "% start input $full_filename\n" if ($explain);
                                my $previous_import_dir = $inside_import;
                                $inside_import = $inside_import . $dir;
				my $in_comment = process_file($full_filename, $prefix . "  ");
                                $inside_import = $previous_import_dir;
				if ($explain) {
				    print " % end input $full_filename\n";
				} elsif ($in_comment) {
				    print "\n";
				}
				if ($after =~ /[^\s]/) {
				    # LaTeX produces this space, so let's do it also
				    print " " . $nl . $after . "\n";
				} else {
				    print " ";
				}
				$_ = "";
			}
		} elsif (my ($before, $ignored, $args, $full_filename, $ignored, $after)
			 = /^($NON_COMMENT)\\includegraphics[\[\s]+(.*?)[\s\]]$ARGUMENT(.*)$/) {
                        if ($explain) {
                                print "% inside_import " . $inside_import ."\n";
                                print "% before " . $before ."\n";
                                print "% ignored " . $ignored ."\n";
                                print "% args " . $args ."\n";
                                print "% full_filename " . $full_filename ."\n";
                                print "% after " . $after ."\n";
                        }
                        if ($inside_import) {
                                $full_filename = $inside_import . $full_filename;
                                print "$before\\includegraphics[$args]{$full_filename}$after\n";
                                $_ = "";
                        }
		} elsif (my ($before, $ignored, $args, $full_filename, $ignored, $after)
			 = /^($NON_COMMENT)\\lstinputlisting[\[\s]+(.*?)[\s\]]$ARGUMENT(.*)$/) {
                        if ($explain) {
                                print "% inside_import " . $inside_import ."\n";
                                print "% before " . $before ."\n";
                                print "% ignored " . $ignored ."\n";
                                print "% args " . $args ."\n";
                                print "% full_filename " . $full_filename ."\n";
                                print "% after " . $after ."\n";
                        }
                        if ($inside_import) {
                                $full_filename = $inside_import . $full_filename;
                                print "$before\\lstinputlisting[$args]{$full_filename}$after\n";
                                $_ = "";
                        }
		}
	}
	if ($expand_usepackage) {
		# Don't bother with before and after text, we just require the
		# usepackage to be alone on its line.
		if (my ($package_name) = /^\s*\\usepackage\{([^\}]*)\}\s*(%.*)?$/) {
			my $full = find_file($package_name . ".sty", $TEXINPUTS);
			if ($full) {
				say $prefix . "Found package file: $full\n";
				process_file($full, $prefix . "  ");
				$_ = "";
				# Forget about any commented newline
				# before the \usepackage:
				$$commented_newline = 0;
			} else {
				say $prefix . "Not including external package $package_name\n";
			}
		}
	}
	if ($expand_bbl) {
		if (my ($before, $bib_name, $after)
			 = /^(.*)\\(?:bibliography|bibselect)\{([^\}]*)\}(.*)$/) {
			# The BBL file is not necessarily $bib_name.
			# Take it from the command-line.
			print $before . $nl;
			say $prefix . "Expanding BBL file: $expand_bbl\n";
			process_file($expand_bbl, $prefix . "  ");
			print " " . $nl . $after . "\n";
			$_ = "";
		}
	}
	if ($biber) {
		if (my ($before, $after)
		    = /^(.*)\\(?:addbibresource)\{[^\}]*\}(.*)$/) {
			# See https://tex.stackexchange.com/questions/166518/biblatex-include-bbl-problem-with-verb-field/166526#166526
			my $biber_noext = $biber;
			$biber_noext =~ s/.bbl//;
			print $before . $nl;
			say $prefix . "Expanding Biber BBL file: $biber\n";
			print '\begin{filecontents*}{' . $biber . '}' . "\n";
			cat_file($biber);
			print "\n";
			print '\end{filecontents*}

\usepackage{xpatch}

%Patch the biblatex input command.
%replace "testinput-bbl" if you change the name above.
%disable if you want to run biblatex/biber normally
\makeatletter
\patchcmd\blx@bblinput{\blx@blxinit}
                      {\blx@blxinit
                       \def\jobname{' . $biber_noext . '}%new jobname
                      }{}{\fail}
\makeatother
			    ';
			say $prefix . "End expansion of Biber BBL file: $biber\n";
			print " " . $nl . $after . "\n";
			$_ = "";
		}
	}
	if ($show_graphics) {
		if (/\\includegraphics(\[[^\]]*\])?{([^}]*)}/) {
                        my $full_filename = $2;
                        if ($inside_import) {
                                $full_filename = $inside_import . $full_filename;
                        }
			my $full = find_tex_file($full_filename, $graphics_extensions);
			say $prefix . "needs graphics file: ";
			print STDERR "$full\n";
		}
	}
	if (/^[ \t]*\\begin\{document\}/) {
		$in_preamble = 0;
		if ($makeatletter) {
			print '\makeatletter' . $nl;
		}
	}
	print;
}

sub unquote
{
	my $str = shift;
	my $x = substr($str, 0, 1);
	my $y = substr($str, -1, 1);
	if ($x eq $y && ($x eq '"' || $x eq "'")) {
		$str = substr($str, 1, -1);
	}
	# There's a weird LaTeX syntax: \include{"file\space
	# with\space spaces"}, so remove these \space when unquoting.
	$str =~ s/\\space / /g;
	return $str;
}

# search $1 in $TEXINPUTS, with possible extensions in $2
sub find_tex_file
{
	my $file = unquote(shift);
	my $extensions = (shift || ":");
	foreach my $ext (split(':', $extensions, -1)) {
		my $full = find_file_global($file . $ext);
		if ($full) {
			return $full;
		}
	}
	if ($fatal) {
		die "ERROR: Could not find file [$file]\n";
	} else {
		print STDERR "Warning: Could not find file [$file]\n";
		return;
	}
}

sub find_file_global
{
	my $file = shift;
	if (open(my $fh, "-|", "kpsewhich", $file)) {
		my $full = <$fh>;
		$full =~ s/\s+$//;
		close($fh);
		if ($full) {
			return $full;
		}
	}
	# Should be useless, but fall-back in case kpsewhich fails (or is not installed, or ...):
	return find_file($file, $TEXINPUTS);
}

# Find files, not searching for global files (to allow not expanding global .sty packages)
sub find_file
{
	my ($file, $path) = @_;
	if (File::Spec->file_name_is_absolute($file)) {
		if (-e "$file" && ! -d "$file") {
			return $file;
		} else {
			return;
		}
	}

	# TEXINPUTS=...: (trailing :) means "append default search
	# directories". We don't want global directories here, but
	# still add . that may be needed.
	if (substr($path, -1) eq ':') {
		$path .= '.';
	}
	foreach my $dir (split(':', $path)) {
		if (-e "$dir/$file" && ! -d "$dir/$file") {
			return("$dir/$file");
		}
	}
	return;
}


__END__

=head1 NAME

latexpand - Flatten LaTeX file by expanding \include and \input, ... and  remove comments

=head1 SYNOPSIS

latexpand [options] FILE...

=head2 Options:

	--verbose        show what's going on
	--keep-comments  don't strip comments (comments are lines
                         starting with %, and anything below
                         \end{document})
	--empty-comments keep empty comments (i.e. % at end of lines) for clarity
	--keep-includes  don't expand \input and \include directives
	--expand-usepackage
	                 Expand \usepackage{...} directives if the
	                 corresponding .sty file is found in
	                 $TEXINPUTS (or the current directory if
	                 $TEXINPUTS is not set)
	--expand-bbl FILE
	                 Expand the bibliography by inlining FILE
	                 (should be a *.bbl file)
	--biber FILE	 Include \bibliography{} with FILE's content,
	                 as needed by biblatex with the biber backend.
	                 (similar to --expand-bbl FILE, but for
	                 biber+biblatex).
	--help           this help message
	--output <file>, -o <file>
	                 generate output in <file>
	--explain        generate explanatory comments in output
	--show-graphics  show included graphics
	--graphics_extensions
	                 colon-separated list of possible graphics extensions
	                 (used by --show-graphics to find the actual graphics files)
	--fatal          Die in case a file can't be found.
	--makeatletter   Insert a \makeatletter in the preamble. In some
	                 rare cases it may break your document, but it
	                 may help fixing bad interactions between
	                 @-commands and inclusion (see BUGS section).
	--in-encoding FMT, --out-encoding FMT
			 File encoding used by input and output files.
			 This uses the same syntax as PerlIO's layers.
			 Example:
			 --in-encoding 'encoding(UTF-8)'
			 The default is 'bytes' and should always work.

=head1 USES

The most common use of latexpand is to simplify distribution of source
LaTeX files, typically to satisfy the requirement of editors and
archival sites (springer, arXiv.org, ...) who force the authors to
submit sources. One does not necessarily want to submit sources with
comments, and uploading a document made of several files including
each other is a bit painful. By default, latexpand answers both
problems by outputing a single LaTeX file that contain no comment.

=head1 GETTING LATEXPAND

The latest version of latexpand is available here:

  https://gitlab.com/latexpand/latexpand

Versions are uploaded to ctan.org from time to time:

  http://www.ctan.org/pkg/latexpand

=head1 BUGS

Please, report bugs on the issue tracker on the project site:

  https://gitlab.com/latexpand/latexpand/issues

=head2 Known bugs

=head3 Verbatim

latexpand currently ignores \begin{verbatim} ... \end{verbatim}, and
will therefore process any \include, \input, ... directives that
appear within verbatim environments (while it shouldn't).

LaTeX comments inside verbatim environments are also incorrectly
stripped. You can use --keep-comments as a workaround to avoid this.

=head3 Comment environment

It would be nice to remove code between \begin{comment} and
\end{comment} too if \usepackage{comment} is used.

Code like

	foo%
	\begin{comment}

will produce the incorrect

	foo\begin{comment}

A workaround is to use --empty-comments when such tricky usage of the
comments package is done.

=head3 \makeatletter and use with transfig/xfig with \scalebox{}

If \input{} or \include{} appears as argument to a command, and the
file included contains \makeatletter, then after expansion, the
\makeatletter and the @-command appear as argument to the command,
which is forbidden because the argument is parsed (and the @-command
badly tokenized) before being executed.

This happens with

	\scalebox{ \input{file-generated-by-xfig.pdf_t} }

Workaround: add \makeatletter before the scalebox manually in your
code, like

        \makeatletter{}
	\scalebox{ \input{file-generated-by-xfig.pdf_t} }
        \makeatother{}

In the case of xfig generated files, it is necessary only for the
first occurence.

A more brute-force workaround is to use latexpand --makeatletter.

=head1 SEE ALSO

Instructions to include only the relevant .bib items (french):

https://lacl.fr/~caubert/notes/portabilite-du-tex.html#dependances

=head1 VERSION

This is latexpand version v1.6.