[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
branch master updated: * tp/Texinfo/Convert/LaTeX.pm (convert_math_to_im
From: |
Patrice Dumas |
Subject: |
branch master updated: * tp/Texinfo/Convert/LaTeX.pm (convert_math_to_images): render math @-commands Texinfo tree elements as images using LaTeX with the preview package and dvipng. |
Date: |
Tue, 24 Dec 2024 07:48:18 -0500 |
This is an automated email from the git hooks/post-receive script.
pertusus pushed a commit to branch master
in repository texinfo.
The following commit(s) were added to refs/heads/master by this push:
new 5815c6a0ac * tp/Texinfo/Convert/LaTeX.pm (convert_math_to_images):
render math @-commands Texinfo tree elements as images using LaTeX with the
preview package and dvipng.
5815c6a0ac is described below
commit 5815c6a0acee7c825cf1338937f7c8ad752a7ed9
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Thu Oct 31 01:12:34 2024 +0100
* tp/Texinfo/Convert/LaTeX.pm (convert_math_to_images): render math
@-commands Texinfo tree elements as images using LaTeX with the
preview package and dvipng.
* tp/Texinfo/Convert/Info.pm (output): preliminary code for the call
to convert_math_to_images.
---
ChangeLog | 9 ++
tp/Texinfo/Convert/Info.pm | 24 +++++
tp/Texinfo/Convert/LaTeX.pm | 243 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 276 insertions(+)
diff --git a/ChangeLog b/ChangeLog
index 6233ef32d9..81f44122ed 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-09-01 Patrice Dumas <pertusus@free.fr>
+
+ * tp/Texinfo/Convert/LaTeX.pm (convert_math_to_images): render math
+ @-commands Texinfo tree elements as images using LaTeX with the
+ preview package and dvipng.
+
+ * tp/Texinfo/Convert/Info.pm (output): preliminary code for the call
+ to convert_math_to_images.
+
2024-12-24 Patrice Dumas <pertusus@free.fr>
Remove letter column in printindex HTML formatting, improve formatting
diff --git a/tp/Texinfo/Convert/Info.pm b/tp/Texinfo/Convert/Info.pm
index 2d644754c2..bd1beceb17 100644
--- a/tp/Texinfo/Convert/Info.pm
+++ b/tp/Texinfo/Convert/Info.pm
@@ -30,6 +30,8 @@ use strict;
use Carp qw(cluck);
+use File::Spec;
+
use Texinfo::Common;
use Texinfo::OutputUnits;
use Texinfo::Convert::Text;
@@ -121,6 +123,28 @@ sub output($$)
my $complete_header_bytes = $header_bytes;
my $output_units = Texinfo::OutputUnits::split_by_node($document);
+ my $elements_images;
+ if (0) {
+ require Texinfo::Convert::LaTeX;
+ Texinfo::Convert::LaTeX->import;
+ my $math_images_dir;
+ if (!defined($destination_directory) or $destination_directory =~ /^\s*$/
+ or $destination_directory eq File::Spec->curdir()) {
+ $math_images_dir = $document_name.'_info_images';
+ } else {
+ $math_images_dir = $destination_directory;
+ }
+ $elements_images
+ = Texinfo::Convert::LaTeX::convert_math_to_images($self, $document,
+ $document_name.'_info',
+ $math_images_dir);
+ #if (defined($elements_images)) {
+ # foreach my $element (%$elements_images) {
+ # print STDERR "$element $elements_images->{$element}\n";
+ # }
+ #}
+ }
+
print STDERR "DOCUMENT\n" if ($self->get_conf('DEBUG'));
my $out_file_nr = 0;
diff --git a/tp/Texinfo/Convert/LaTeX.pm b/tp/Texinfo/Convert/LaTeX.pm
index ae947d1e5e..7d24d82bb2 100644
--- a/tp/Texinfo/Convert/LaTeX.pm
+++ b/tp/Texinfo/Convert/LaTeX.pm
@@ -4492,4 +4492,247 @@ sub _convert($$)
return $result;
}
+
+
+# math commands to images
+#
+# render math @-commands Texinfo tree elements as images.
+# Requires LaTeX with some math modules and the preview package.
+# Use dvipng to generate the images.
+
+sub convert_math_to_images($$$;$)
+{
+ my $self = shift;
+ my $document = shift;
+ my $prefix = shift;
+ my $destination_directory = shift;
+
+ my $document_root = $document->tree();
+
+ my @math_at_commands = ('math', 'displaymath');
+
+ my $math2img_out_dir = $destination_directory;
+ if (!defined($math2img_out_dir) or $math2img_out_dir =~ /^\s*$/) {
+ $math2img_out_dir = File::Spec->curdir();
+ } else {
+ my ($encoded_math2img_out_dir, $math2img_out_dir_encoding)
+ = $self->encoded_output_file_name($math2img_out_dir);
+ $self->create_destination_directory($encoded_math2img_out_dir,
+ $math2img_out_dir);
+ }
+
+ my $math2img_basename = "${prefix}_math2img";
+
+ my $collected_commands = Texinfo::Common::collect_commands_list_in_tree(
+ $document_root, \@math_at_commands);
+
+ if (scalar(@$collected_commands) == 0) {
+ return undef;
+ }
+
+ my $math2img_latex_basefile = $math2img_basename.'.tex';
+
+ my ($encoded_math2img_latex_basefile, $latex_basefile_name_encoding)
+ = $self->encoded_output_file_name($math2img_latex_basefile);
+
+ my $math2img_latex_file_path_name = File::Spec->catfile($math2img_out_dir,
+ $math2img_latex_basefile);
+
+ my ($encoded_math2img_latex_file_path_name, $math2img_latex_path_encoding)
+ = $self->encoded_output_file_name($math2img_latex_file_path_name);
+
+ my %options_latex_math = copy_options_for_convert_to_latex_math($self);
+
+ my $latex_converter
+ = Texinfo::Convert::LaTeX->converter(\%options_latex_math);
+
+ my $fh;
+ my $counter = 0;
+ my $result = {};
+ foreach my $element (@$collected_commands) {
+ my $cmdname = $element->{'cmdname'};
+ my $tree;
+ if ($cmdname eq 'math') {
+ $tree = $element->{'args'}->[0];
+ } elsif ($element->{'contents'}) {
+ $tree = {'contents' => [@{$element->{'contents'}}]};
+ if (scalar(@{$tree->{'contents'}})
+ and $tree->{'contents'}->[0]->{'type'}
+ and ($tree->{'contents'}->[0]->{'type'} eq 'empty_line_after_command'
+ or $tree->{'contents'}->[0]->{'type'} eq
'elided_brace_command_arg'
+ or $tree->{'contents'}->[0]->{'type'} eq
'elided_rawpreformatted')) {
+ shift @{$tree->{'contents'}};
+ }
+ if ($tree->{'contents'}->[-1]->{'cmdname'}
+ and $tree->{'contents'}->[-1]->{'cmdname'} eq 'end') {
+ pop @{$tree->{'contents'}};
+ }
+ } else {
+ next;
+ }
+ if (scalar(@{$tree->{'contents'}}) == 0) {
+ # should correspond to an ignored block
+ next;
+ }
+
+ $counter++;
+ if ($counter == 1) {
+ local *MATH2IMG_TEXFILE;
+ unless (open(*MATH2IMG_TEXFILE,
+ ">$encoded_math2img_latex_file_path_name")) {
+ $self->converter_document_error(
+ sprintf(__("math to images: could not open %s: %s"),
+ $math2img_latex_file_path_name, $!));
+ return undef;
+ }
+ $fh = *MATH2IMG_TEXFILE;
+
+ print $fh "% Automatically generated by Texinfo math to image code\n";
+
+ # TODO customization of the packages loaded
+ # The following unused list from
+ # https://orgmode.org/worg/org-tutorials/org-latex-preview.html
+ my $unused_more_packages = '
+\usepackage[usenames]{color}
+\usepackage{amsmath}
+\usepackage[mathscr]{eucal}
+\usepackage[utf8]{inputenc}
+\usepackage[T1]{fontenc}
+% Package fixltx2e omitted
+\usepackage{graphicx}
+% Package longtable omitted
+% Package float omitted
+% Package wrapfig omitted
+\usepackage[normalem]{ulem}
+\usepackage{textcomp}
+\usepackage{marvosym}
+\usepackage{wasysym}
+\usepackage{latexsym}
+\usepackage{amssymb}
+% Package amstext omitted
+% Package hyperref omitted
+';
+ print $fh '
+\documentclass{minimal}
+\usepackage{amsmath}
+\usepackage{amsthm}
+\usepackage{amssymb}
+\usepackage{bm}
+\usepackage[active,tightpage]{preview}
+\pagestyle{empty}
+\begin{document}
+'
+ }
+ my $text;
+ $text = convert_to_latex_math($latex_converter, $tree, undef);
+ print $fh "\\begin{preview}\n";
+ if ($cmdname eq 'math') {
+ print $fh "\$$text\$\n";
+ } else { #displaymath
+ print $fh "\n\\[" . $text . "\\]\n";
+ }
+ print $fh "\\end{preview}\n\n";
+
+ my $png_file_name = File::Spec->catfile($math2img_out_dir,
+ $math2img_basename.$counter.'.png');
+ $result->{$element} = $png_file_name;
+ }
+ if ($counter > 0) {
+ print $fh "\\end{document}\n";
+ # FIXME check close error
+ close ($fh);
+ } else {
+ return undef;
+ }
+
+ # run LaTeX
+ my ($encoded_math2img_out_dir, $math2img_out_dir_encoding)
+ = $self->encoded_output_file_name($math2img_out_dir);
+
+ my $math2img_initial_dir = Cwd::abs_path;
+ unless (chdir $encoded_math2img_out_dir) {
+ $self->converter_document_warn(
+ sprintf(__("math to images: chdir %s failed: %s"),
+ $math2img_out_dir, $!));
+ return undef;
+ }
+ print STDERR "cwd($encoded_math2img_out_dir): " . Cwd::cwd() ."\n"
+ if ($self->get_conf('VERBOSE'));
+
+ $self->converter_document_warn(
+ sprintf(__("math to images: output file missing: %s"),
+ $math2img_latex_basefile))
+ unless (-f $encoded_math2img_latex_basefile);
+
+ my $exec = 'latex';
+ my $cmd = "$exec $math2img_latex_basefile";
+ my $encoding = $self->get_conf('MESSAGE_ENCODING');
+
+ my $encoded_exec;
+ if (defined($encoding)) {
+ $encoded_exec = Encode::encode($encoding, $exec);
+ } else {
+ $encoded_exec = $exec;
+ }
+
+ my $encoded_cmd = $encoded_exec . " " . $encoded_math2img_latex_basefile;
+
+ print STDERR "math2img command: $encoded_cmd\n"
+ if ($self->get_conf('VERBOSE'));
+
+ # do not use system in order to be sure that STDIN is not
+ # mixed up with the main script STDIN. It is important because
+ # if latex fails, it will may from STDIN and the input may trigger
+ # diverse actions.
+ if (not(open(MATH2IMG, "|-", $encoded_cmd))) {
+ $self->converter_document_error(sprintf(__(
+ "math to images: command failed: %s"), $cmd));
+ return undef;
+ }
+ if (!close(MATH2IMG)) {
+ $self->converter_document_warn(sprintf(__(
+ "math to images: closing communication failed: %s: %s"),
+ $cmd, $!));
+ return undef;
+ }
+
+ my $math2img_dvi_basefile = $math2img_basename.'.dvi';
+
+ my ($encoded_math2img_dvi_basefile, $dvi_basefile_name_encoding)
+ = $self->encoded_output_file_name($math2img_dvi_basefile);
+
+ my $math2img_dvi_file_path_name = File::Spec->catfile($math2img_out_dir,
+ $math2img_dvi_basefile);
+
+ my ($encoded_math2img_dvi_file_path_name, $math2img_dvi_path_encoding)
+ = $self->encoded_output_file_name($math2img_dvi_file_path_name);
+
+ $self->converter_document_warn(
+ sprintf(__("math to images: dvi file missing: %s"),
+ $math2img_dvi_basefile))
+ unless (-f $encoded_math2img_dvi_basefile);
+
+ my @to_images_options = ('-T', 'tight', '-D', '600');
+ my @to_images_args = (@to_images_options, $encoded_math2img_dvi_basefile);
+
+ my $to_image_exec = 'dvipng';
+ my $status = system $to_image_exec, @to_images_args;
+
+ if ($status != 0) {
+ $self->converter_document_warn(sprintf(__(
+ "math to images: system `%s' failed: %d\n"), $to_image_exec . ' ' .
+ join(' ', @to_images_options). ' '.$math2img_dvi_basefile, $?));
+ return undef;
+ }
+
+ unless (chdir $math2img_initial_dir) {
+ $self->converter_document_warn(sprintf(__(
+ "math to images: unable to return to initial directory: %s"), $!));
+ return undef;
+ }
+
+ return $result;
+}
+
+
1;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- branch master updated: * tp/Texinfo/Convert/LaTeX.pm (convert_math_to_images): render math @-commands Texinfo tree elements as images using LaTeX with the preview package and dvipng.,
Patrice Dumas <=