Hi all, I have a "small problem" with a file that I want to change using perl. This is an example of the input file:
Code:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{pst-any}
\begin{document}
Texto
\begin{myexample}{above, algo1,algo2}
\includegraphics[scale=1]{archivo-fig-1}
\end{myexample}
Texto
\begin{myexample}{below,algo1,algo2}
\includegraphics[scale=0.5]{archivo-fig-2}
\end{myexample}
Texto
\begin{myexample}{right,algo1,algo2}
\includegraphics[scale=0.8]{archivo-fig-3}
\end{myexample}
Texto
\begin{myexample}{left,algo1,algo2}
\includegraphics[scale=1.2]{archivo-fig-4}
\end{myexample}
\end{document}
and this is an example of the output I want
Code:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{grfext}
\PrependGraphicsExtensions*{pdf}
\usepackage{graphicx}
\graphicspath{images/}
\begin{document}
Texto
\begin{myexample}{above,algo1,algo2}
\lstinputlisting[linerange=document-document]{images/archivo-fig-1.tex}
\tcblower
\includegraphics[scale=1]{archivo-fig-1}
\end{myexample}
Texto
\begin{myexample}{below,algo1,algo2}
\includegraphics[scale=0.5]{archivo-fig-2}
\tcblower
\lstinputlisting[linerange=document-document]{images/archivo-fig-2.tex}
\end{myexample}
Texto
\begin{myexample}{right,algo1,algo2}
\includegraphics[scale=0.8]{archivo-fig-3}
\tcblower
\lstinputlisting[linerange=document-document]{images/archivo-fig-3.tex}
\end{myexample}
Texto
\begin{myexample}{left,algo1,algo2}
\lstinputlisting[linerange=document-document]{images/archivo-fig-4.tex}
\tcblower
\includegraphics[scale=1.2]{archivo-fig-4}
\end{myexample}
\end{document}
I'm using the following perl script:
Code:
#!/usr/bin/perl
use v5.16;
use autodie;
#use strict; # Activates default in v5.16
my $imageDir = "images"; # where to save the images
my $Iext = ".pdf"; # Graphics Extensions
open my $IPDF,'<', "archivo-pdf.tex";
undef $/; # read all file
my ($uno,$dos) = split(/\\begin\{document\}/,<$IPDF>,2);
close $IPDF;
my @coment = split /\n/, $uno; # split
my @preamb;
foreach my $line (@coment) {
chomp($line);
$line =~ s/\\usepackage(?:\[.+?\])?\{pst.+?}//g; # delete
$line =~ s/(\\usepackage(?:\[.+?\])?)\{graph/\%$1\{graph/g;# coment
next if $line =~ m/^\s*$/; # delete white space
push(@preamb,$line);} # addin changes
my $clean = join("\n", @preamb, "\\usepackage{grfext}\n\\\PrependGraphicsExtensions*{$Iext}\n\\usepackage{graphicx}\n\\graphicspath{{$imageDir/}}\n\\begin{document}". $dos);
open my $OPDF,'>',"archivo-pdf.tex"; # write
print $OPDF $clean;
close $OPDF;
The idea is to modify (insert) text when you can not find the words above, below, left, right.
How I can make these changes using in my script?
thankful in advance
Pablo