📄 tex2pdf
字号:
my $base; my $suffix; my $kpse_result; my $working_dir = cwd."/"; ($base,$path,$suffix) = fileparse($tag_contents, $regexp_suffixes); # if a suffix is specified in the tag_contents handle it as requested # # 1. $suffix: TRUE if $suffix is defined and not of zero length # means: a valid suffix has been found in the filename # 2. defined($ignore_suffix): TRUE if $ignore_suffix is defined # means: a regexp for suffixes to be ignored has been specified as # parameter4 # 3. $suffix =~ /$ignore_suffix/: TRUE if $suffix matches the regexp # means: the suffix in the filename is wanted to be ignored # # The IF statement will be executed when: # a valid suffix has been found in the filename (1) # AND regexp for suffixes to be ignored has NOT been specified (not 2) # OR # a valid suffix has been found in the filename (1) # AND regexp for suffixes to be ignored has been specified (2) # AND the suffix in the filename is NOT wanted to be ignored (not 3) # # The stuff that is executed if the entire IF statement is TRUE does the # following: accept the found suffix and consider it as the only possible # file name. if($suffix and not (defined($ignore_suffix) and $suffix =~ /$ignore_suffix/)){ $kpse_result=`kpsewhich $tag_contents`; # print warning and skip this tag if kpsewhich could not find it if (!$kpse_result) { &report(4, "WARNING - Could not identify referenced file:\n", " Ignoring '$tag_contents'."); next; } } else { # if there is a '.' in the basename assume that this is a reference # to a file of another type and skip it if( $base =~ /\./ ) { &report(9, "Found an unknown extension. Ignoring '$tag_contents'."); next; } # search for all possible files with allowed suffixes foreach my $allowed_suffix (@suffixes) { if (not $allowed_suffix =~ /[\]\)\(\|\[\\]/ ) { # suffix is not a regexp, but a real extension my $possible_file= $path.$base.'.'.$allowed_suffix; $kpse_result=`kpsewhich $possible_file`; if ($kpse_result) { last; } } } } # if kpsewhich could not find any file with an allowed suffix # assume that this reference is of a different type and skip it # quietly if (!$kpse_result) { &report(9, "No suitable file found. Ignoring '$tag_contents'."); next; } # expand '.' in kpsewhich output to the current path $kpse_result =~ s#^\./#$working_dir#; # remove trailing newline chomp($kpse_result); # add file to the found file list if it is not already on it if( &array_index($kpse_result, @found_files) < 0 ) { push(@found_files, $kpse_result); } } return @found_files;}### Build a list of all files which are included from the root file.# This function recurses, and is maybe smart enough to detect cycles.# Be sure to set REF_DOCS to the empty string prior to calling this.# parameter 1: tex file to start with# no return value# result is appended to global variable @REF_DOCSsub get_file_list { my $source = $_[0]; my @imports = (); # This is the cycle avoidance logic. if ( &array_index($source, @REF_DOCS) < 0 ) { # Make sure the file can be accessed &check_file($source, "Included TeX file seems not to be available. Path problem?"); # Save the argument in the list of files. push(@REF_DOCS, $source); # Get the list of files included by the argument. @imports=&identify_files($source, 'include|input', ['tex']); # Recurse. foreach my $file (@imports) { if( ! ($file =~ /\.tex$/) ) { $file .= '.tex'; } &get_file_list($file); } }}### do the required modifications in the LaTeX preamble # parameter 1: original preamble from the source file# lines before \begin{document} tag (without this tag)# parameter 2: reference to hyperref parameter list# return value: adjusted preamblesub adjust_preamble { my $preamble = $_[0]; my $hyperref_params_ref = $_[1]; my $extra_code; my $result; $_ = $preamble; # protect pdflatex execution mode s/^(\\batchmode)$/% $1/m; # insert a4paper in the documentclass when a4wide is used # fixes problem that hyperref defaults to letter otherwise if ( /^[^%]*\\usepackage(\[widemargins\])?\{(a4|a4wide)\}/m ) { # check if package parameters with [] brackets are present if ( not s/^(\\documentclass\[.*?)\]/$1,a4paper]/m ) { s/^\\documentclass/$&\[a4paper\]/m; } } ### collect additional LaTeX code $extra_code = "\n" . '\usepackage{pslatex}' . "\n"; if ( ¶m_value('thumbpdf') eq $YES ) { $extra_code .= '\usepackage{thumbpdf}' . "\n"; } else { $extra_code .= "% no thumbpdf support\n"; } # if ( ¶m_value('ppower') eq $YES ) {# $extra_code .= '\usepackage{mpmulti}' . "\n";# } else {# $extra_code .= "% no ppower support\n";# } if ( ¶m_value('authorindex') eq $YES ) { $extra_code .= '\usepackage[pages]{authorindex}' . "\n"; $extra_code .= '\let\cite=\aicite' . "\n"; } else { $extra_code .= "% no authorindex support\n"; } $extra_code .= '\makeatletter' . "\n"; $extra_code .= '\usepackage[' . join(',', @$hyperref_params_ref) . ']{hyperref}' . "\n"; $extra_code .= '\makeatother' . "\n"; ### insert the extra LaTeX code directly after documentclass m/^(\\documentclass)(\[[^]]*\])?(\{.*\})/m; return $` . $& . $extra_code . $';}### adjust all filenames in the LaTeX code to the tmp files# parameter 1: original LaTeX code from the source file# return value: adjusted codesub adjust_filenames { my $code = $_[0]; my $tmp_suffix = ¶m_value('tmp_base_suffix'); my $result; $_ = $code; # cut off the suffix of eps, ps, *.gz and pstex graphics s/((\\includegraphics)(\[[^]]*?\])?(\{[^}]+?))\.(e?ps|pstex|e?ps\.gz)\n?\}/$1}/sg; # replace the suffix 'pstex_t' with 'pdf_t' s/(\\input\{[^}]+?\.)pstex_t\n?\}/$1pdf_t}/sg; if ( ¶m_value('mtp_preamble') eq $NO ) { # cut off the suffix of mmp graphics s/(\\multiinclude(\[[^]]*?\])?\{[^}]+?)\.mmp\n?\}/$1}/sg; } else { # replace the suffix '.#' with '-mp.#' s/(\\includegraphics(\[[^]]*?\])?\{[^}]+?)\.(\d+?)\n?\}/$1$MTP_TMP_BASESUFFIX\.$3}/sg; # replace the suffix '.#' with '-mp.#' s/(\\convertMPtoPDF(\[[^]]*?\])?\{[^}]+?)\.(\d+?)\n?\}/$1$MTP_TMP_BASESUFFIX\.$3}/sg; # cut off optional suffix '.mmp' and append '-mp' in any case s/(\\multiinclude(\[[^]]*?\])?\{[^}]+?)(\.mmp)?\n?\}/$1$MTP_TMP_BASESUFFIX}/sg; } # insert the tmp_suffix in tex filenames # I assume that files with no extension are TeX files as well; correct? s#(\\(input|include)\{([^}]*?/)?[^}/.]+?)((\.tex)?\n?\})#$1$tmp_suffix$4#sg; return $_;}### Convert given tex file to the temp tex file we need for pdftex### major task is to change the reference in the tex files to the### corresponding tmp files# parameter 1: tex source file# parameter 2: tex tmp file# parameter 3: reference to hyperref parameter list or# 'undef' if preamble should not be changedsub convert_tex2tmp { my $source = $_[0]; my $target = $_[1]; my $hyperref_params_ref = $_[2]; my $contents; my $preamble; my $body; my $adjust_preamble = defined($hyperref_params_ref) ? $YES : $NO; my $read_err_msg = "Could not read original TeX document to generate temporary document"; ### open source and target file &check_file($source, $read_err_msg . '.'); open(SOURCE_FILE, "<$source") or &abort($read_err_msg . " ($source)."); ### read in the LaTeX source file $contents = ""; while(<SOURCE_FILE>) { $contents .= $_; } close SOURCE_FILE; ### prepare the LaTeX code for PDF generation if ( $adjust_preamble eq $YES ) { $contents =~ m/^ *\\begin\{document\} *$/m; $preamble = $`; $body = $&.$'; $preamble = &adjust_preamble($preamble, $hyperref_params_ref); $preamble = &adjust_filenames($preamble); } else { $preamble = ""; $body = $contents; } $body = &adjust_filenames($body); ### write the new LaTeX target file open(TARGET_FILE, ">$target") or &abort("Could not open file to write temporary TeX document ($target)."); print TARGET_FILE $preamble.$body; close TARGET_FILE;}### Convert the given EPS image to PDF# parameters $1: EPS image filename with absolute path# return value: nonesub convert_eps2pdf { my $image = $_[0]; my $image_path; my $image_base; my $image_name; my $suffix; my $image_target; my $zipped = 0; my $dummy; ($image_base,$image_path,$suffix) = fileparse($image, '\.eps', '\.ps', '\.pstex', '\.gz'); if ($suffix eq "\.gz") { $zipped = 1; ($image_base,$dummy,$suffix) = fileparse($image_base, '\.eps', '\.ps', '\.pstex'); } $image_name = $image_base . $suffix; $image_target = $image_path . $image_base . '.pdf'; #### check if image file really exists #&check_file($image, "Could not convert referenced image."); ### return if image directory is not writeable if (! -w $image_path) { &report(4, "WARNING - Image directory not writable: $image_path\n", " Skipping '$image_name', assume you have converted it manually."); return; } if ( ! -f $image_target or -M $image_target > -M $image ) { &report(7, "Converting image $image_name to $image_target ...\n"); if ($zipped > 0) { &system_command("gunzip -c $image | epstopdf -f -outfile=$image_target", $TRUE, 8, "epstopdf failed on $image_name"); } else { &system_command("epstopdf -outfile=$image_target $image", $TRUE, 8, "epstopdf failed on $image_name"); } if (¶m_value('delete_pdf_images') eq $YES) { push(@TMPFILES, $image_target); } } else { &report(7, "$image_base.pdf newer than $image_name, conversion skipped..."); }}### Convert the given PSTEX_T file to PDF_T# parameters 1: PSTEX_T filename with absolute path# return value: nonesub convert_pstex2pdf { my $pstex_file = $_[0]; my $pstex_path; my $pstex_base; my $pstex_name; my $suffix; my $pstex_target; my @eps_images; ($pstex_base,$pstex_path,$suffix) = fileparse($pstex_file, ('\.pstex_t')); $pstex_name = $pstex_base . $suffix; $pstex_target = $pstex_path . $pstex_base . '.pdf_t'; #### check if image file really exists #&check_file($pstex_file, "Could not convert referenced file."); ### return if directory is not writeable if (! -w $pstex_path) { &report(4, "WARNING - Directory not writable: $pstex_path\n", " Skipping '$pstex_name', assume you have converted it manually."); return; } # descend into file &report(7, "Converting file $pstex_name ...\n"); # find included EPS image(s) @eps_images=&identify_files($pstex_file, 'includegraphics', ['pstex', 'pstex\.gz']); # create .pdf_t file &convert_tex2tmp($pstex_file, $pstex_target, undef); # put tmp file in the tmp file list push(@TMPFILES, $pstex_target); # convert image(s) to pdf foreach my $image (@eps_images) { &convert_eps2pdf($image); }}### Convert the given MP image to PDF# parameters $1: MP image filename with absolute path# return value: nonesub convert_mp2pdf { my $image = $_[0]; my $image_path; my $image_base; my $image_name; my $suffix; my $image_target; my $image_src; my @mps_fig=(); my $mp_fig; ($image_base,$image_path,$suffix) = fileparse($image, '\.mp|\.mmp'); $image_name = $image_base . $suffix; $image_src=$image_path . $image_base . $suffix; @mps_fig= &grep_file($image_path.$image_name,'beginfig',$TRUE); $_=$mps_fig[0]; /(\d)/; $mp_fig=$1; if (¶m_value('mtp_preamble') eq $YES) { $image_target = $image_path . $image_base . $MTP_TMP_BASESUFFIX . '.' . $mp_fig; $image_name=$image_base.$MTP_TMP_BASESUFFIX.$suffix; } else { $image_target = $image_path . $image_base . '.' . $mp_fig; } #### check if image file really exists #&check_file($image, "Could not convert referenced image."); ### return if image directory is not writeable if (! -w $image_path) { &report(4, "$MYNAME: WARNING - Image directory not writable: $image_path\n", " Skipping '$image_name', assume you have converted it manually."); return; } if ( ! -f $image_target or (-M $image_target > -M $image_src) or ( ( ¶m_value('mtp_preamble') eq $YES) and (-M $image_target > -M $image_path.$MTP_PREAMBLE_FILENAME) ) ) { &report(7, "Converting image $image_name ...\n"); my $working_dir = cwd."/"; chdir("$image_path") or &abort("cannot cd to $image_path($!)"); if ( ¶m_value('mtp_preamble') eq $YES ) { &modify_mp_file($image_base,$suffix); } &system_command("mpost $image_name", $TRUE, 8, "mpost fa
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -