LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2013, 04:33 PM   #1
pablgonz
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Rep: Reputation: Disabled
Insert optional text using perl


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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert text in Thunderbird Skaperen Linux - Software 4 06-29-2012 02:36 PM
[SOLVED] Insert a column of text to a text file docaia Linux - Newbie 5 06-02-2011 10:55 AM
[SOLVED] insert text before the First Pattern Found NetRock Programming 3 04-08-2011 12:22 PM
How to Insert text into multiple files mpetrovic Linux - Software 5 01-15-2010 10:56 PM
vi trick for insert text in between line ufmale Programming 3 05-13-2008 03:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration