mitch
mitch

Reputation: 47

Cannot call pdflatex from perl script (due to encoding?)

When I call pdflatex manually from the windows command line, it generates the desired pdf. When I call pdflatex from a perl script instead, it does not:

system("pdflatex $fileName");

.. results in

Sorry, but pdflatex did not succeed.

You may want to visit the MiKTeX project page, if you need help.
utf8 "\x80" does not map to Unicode at C:/strawberry-perl/perl/site/lib/Encode.pm line 200.

The script was running on unix before and working fine. Now, after having it migrated to a windows system it doesn't. The content of the tex-input-file is generated by the script as well. the "file"-command on my Mac tells me that this file is encoded as "us-ascii". So I tried to make perl encode it as "utf-8", but it did not work:

open(FH, "> :encoding(utf-8)", $fileName);

or

binmode(FH, ":utf8");

Files are still being generated with us-ascii encoding. How can I change that?

So far, the encoding is my only clue. What else could be the problem?

Upvotes: 0

Views: 229

Answers (1)

NzFrancis
NzFrancis

Reputation: 13

If this works fine when manually typed into the command line the this could be due to the way perl interpolates the quotation marks before passing the command to the system. Have you tried printing the call you making to test whether it provides the exact same imput as when to enter it manually? Otherwise, for passing arguments to a program via the system command in perl I always separate them out as follows to avoid any interpolation errors:

#...
my $prog = "Z.*";
my $arg1 = "X";
my $arg2 = "Y";
#...
my $file = "W.*";
system("$prog", ("$arg1", "$arg2", ..., "$file"));
#...

If this doesn't work, another, albeit rather clunky solution, might be to import the file contents into a variable and try the following to 'manually' encode it in perl as follows:

use Encode;
use utf8;
use charnames qw( :full :short );

my $encodedfile = encode("utf8", $filecontents);

If you happen to have any active caracters in the file which could influence the way pdflatex handles the final output (for example in perl \\ gives \ to pdflatex, which ends up finally being ) you can append the following to the encoding:

my $str = $encodedfile;
my $find = "\\N{U+005C}";
my $replace = "\\textbackslash ";
$str =~ s/$find/$replace/g;

my %special_characters;
$special_characters{"\\N{U+0025}"} = "\\pourcent ";
$special_characters{"\\\$"} = "\\\$";
$special_characters{"\\N{U+007B}"} = "\\{";
$special_characters{"\N{U+007D}"} = "\\}";
$special_characters{"\N{U+0026}"} = "\\&";
$special_characters{"\\N{U+005F}"} = "\\textunderscore ";
$special_characters{"\\N{U+002F}"} = "\/";
$special_characters{"\\N{U+005B}"} = "\[";
$special_characters{"\\N{U+005D}"} = "\]";
$special_characters{"\\N{U+005E}"} = "\\textasciicircum ";
$special_characters{"\\N{U+0023}"} = "\\#";
$special_characters{"\\\N{U+007E}"} = "\\textasciitilde ";
$special_characters{"\\\N{U+0021}"} = " \\newline ";

my $string = $str;
foreach my $char (keys %special_characters) {
  $string =~ s/$char/$special_characters{$char}/g;
}

Hope this helps.

Upvotes: 1

Related Questions