stabu has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a hundred images which I stick (via graphicx) into a latex document, with a little commentary on each one. Each image and commentary gets a page. I did it all in vim, half auto, half manual the other day, but it was error prone.

So, I was thinking of doing up some functions in vim, whereby a file with the image names is held, and read into the doc using variables, etc, but I reckoned really perl should be doing that.

Any body done something similar? File is read and for each image name a certain text is generated which is all packaged conveniently in latex markup.

I'm giving it a go, but thought I'd post as well.

Many thanks!

Replies are listed 'Best First'.
Re: repetitive latex doc: in vim or perl?
by CountZero (Bishop) on Jul 12, 2008 at 21:26 UTC
    Perl and LaTex go very well together.

    Your particular problem really calls for for the use of a Templating system, such as Template Toolkit. I just finished an application which takes data out of a database, munges the data, creates some graphs and puts it all in a LaTeX file through the use of a template and then runs pdflatex a few times on it and out comes a beautiful report, complete with table of contents and fully indexed.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: repetitive latex doc: in vim or perl?
by igelkott (Priest) on Jul 12, 2008 at 17:20 UTC

    Not a difficult problem for Perl and probably don't need to do anything special with LaTeX if you have a good template (ie, example file with proper LaTeX included).

    Show what you've got and you could get some help.

      Ooops, sorry, I just haven't used perl in a while ... and I realised that this task is easy as pie in perl.

      I cannot believe I was doing this semi-manually in vim the other day. I wasted hours of repetitive keying. I've been too long away from perl-land.

      I include it for anybody stumbling in (still a bit messy, mind you, only the essentials...)
      #!/usr/bin/perl $fname = shift @ARGV; open(fi,"<$fname"); @text = <fi>; close fi; # First, the header of the latex print(" \\documentclass{article} \\usepackage{graphicx} \\usepackage{fullpage} \\begin{document} "); while (@text) { $imfile = shift @text; chop($imfile); print(" \\section{$imfile} \\begin{figure}[!hbp] \\centerline{\\includegraphics[width=9cm, height=9cm]{ims/$imf +ile"."_gd.png}} \\end{figure} \\begin{figure}[!hbp] \\centerline{\\includegraphics[width=12cm, height=9cm]{$imfile +"."mfm.png}} \\end{figure} \\newpage \n"); } # And now the tail print(" \\end{document} \n");
        I'm glad you found a solution for this problem, especially since it uses perl. Here are just a few suggestions:

        1) Since LaTeX has uses backslashes a lot, you can make it easier on yourself by using a 'here'-document with single quotes:

        print <<'END'; \documentclass{article} \usepackage{graphicx} \usepackage{fullpage} \begin{document} END
        2) Unfortunately, perl's own variable interpolation syntax is disabled when using here-documents within single quotes. On the other hand, writing your own mini-templating system is just a few lines in perl:
        sub substitute_vars { my ($template, $hash) = @_; (my $out = $template) =~ s/\${([^}]+)}/$$hash{$1}/ge; $out; }
        and here's how you could use it (write ${imfile} whenever you want to insert the value of imfile, etc.):
        my $template = <<'TEMPLATE_END'; \section{${imfile}} \begin{figure}[!hbp] \centerline{\includegraphics[width=9cm, height=9cm]{ims/${imfile}_gd.p +ng}} \end{figure} \begin{figure}[!hbp] \centerline{\includegraphics[width=12cm, height=9cm]{${imfile}mfm.png} +} \end{figure} \newpage TEMPLATE_END for (@text) { chomp; print substitute_vars($template, { imfile => $_ } ); }
        Of course, once your needs started to get more demanding, I'd consider using an existing templating system.

        3) Finally, you can define your own macros in LaTeX with \newcommand. You might try something along the lines of this:

        \newcommand{\insert_figures}[1]{ \section{#1} ... \centerline{\includegraphics[width=9cm, height=9cm]{ims/#1_gd.png}} ... \centerline{\includegraphics[width=12cm, height=9cm]{#1mfm.png}} ... \newpage }
        However, if you have to manipulate the macro arguments, you might find using something you are familiar with (like perl) easier.
Re: repetitive latex doc: in vim or perl?
by blazar (Canon) on Jul 17, 2008 at 23:12 UTC

    I personally believe that others already correctly confirmed your impression that Perl is perfectly apt for the task you're up to and the only thing I may want to add is that while most people either suggested some sort of handrolled template system or -as in the case of CountZero- of a "real" one, Perl and LaTeX can "go very well together" in another sense too: precisely by means of PerlTeX which basically lets you implement LaTeX macros in Perl and may be or not of your interest for this task, but is certainly worth mentioning anyway. (Also because IMHO it's less known than it should deserve to.)

    (Apologies for replying so late.)

    --
    If you can't understand the incipit, then please check the IPB Campaign.
Re: repetitive latex doc: in vim or perl?
by repellent (Priest) on Jul 18, 2008 at 00:52 UTC
    Vim is powerful when you need to script positional-editing actions (wrt the Vim cursor/lines/window, etc.) Couple that with the capability to record & repeat keystrokes and you quickly eliminate edit repetitions.

    You can shell out Perl to perform the job of returning the LaTeX markup text, while you have Vim insert the text with a user command or key map. Then, you can figure out where in your document you wish to place the image commentaries and tell Vim the lines you wish to act on, in one swoop.