5

I would like to pull all figures including captions out of multiple .tex files and write them into a single new .tex file. This means I have to parse for

\begin{figure}
   . . . 
\end{figure}

and output that whole block. I have no clue about regular expressions, but I think grepor awkare best suited for this task. Can anyone help me out with the parsing-part? Writing a bash script to do the rest is no problem for me.

10
  • 7
    \usepackage{endfloat} will do it for you, writing a filename.fff file containing exactly what you need.
    – egreg
    Commented Jan 10, 2013 at 14:19
  • Related/Possible answer: tex.stackexchange.com/a/89972/11002
    – yo'
    Commented Jan 10, 2013 at 14:20
  • @egreg, amazing! I tried it now and the file fff contains \begin{figure*} even if I use non stared environment.
    – Sigur
    Commented Jan 10, 2013 at 14:23
  • @Sigur I would suppose that it is a "feature": you don't want one- and two- column floats mixed in the end ;)
    – yo'
    Commented Jan 10, 2013 at 14:24
  • 1
    @Sigur The package requires that \end{figure} is on a line by itself and starting at column 1, which isn't so bad a requirement. You don't indent all your document environment, do you?
    – egreg
    Commented Jan 10, 2013 at 14:33

2 Answers 2

5

Edit: @deimi 's answer at Compile only the theorems using the xcomment package might make this awk script unnecessary.

Here's an awk solution that bypasses TeX parsing.

#!/usr/bin/awk
# Extract figure environments
#

BEGIN{
    printing = 0;
}

/begin\{figure/{
    printing = 1;
}

printing == 1 {
    print;
}

/end\{figure/{
    printing = 0;
}
3
  • I was writing exactly the same answer! A note, your (and my) solution assumes that there is no figure environments inside, for example, verbatim blocks which should not be output, and that no extraneous chars are at the beginning of the line with \begin{figure} or at the end of the line with \end{figure}, which should be stripped
    – JLDiaz
    Commented Jan 10, 2013 at 16:07
  • Please, post also your solution! :)
    – Eekhoorn
    Commented Jan 10, 2013 at 16:17
  • @JLDiaz My solution does work even when characters precede or follow the begin{figure or end{figure patterns. It just prints the whole line in each case. The OP can strip that extra stuff if he wants to. Yes, I output figures inside verbatim - probably rare, and fixable if necessary. Commented Jan 10, 2013 at 16:20
5

My original answer was to be the following script, totally equivalent to the answer by Ethan Bolker, which posted when I was writing it.

/.*begin{figure}/{
    output=1
}
output==1{
    print
}
/.*end{figure}/{
    output=0
    print "% ---------------------------------- "
}

I cancelled my answer, but the OP asked me to post it nevertheless, so here it is.

In order to provide some added value, I'll post also the following variation, which deals with some rare cases (not really needed, I implemented it only for fun), in particular:

  • It strips extraneous chars which could appear at the beginning of \begin{figure} or the end of \end{figure}
  • It does not output figure environments inside verbatim, Verbatim or lstlisting.
BEGIN{
    in_verbatim=0
}
/.*begin.(verbatim|Verbatim|lstlisting)/{
    in_verbatim=1
}
/.*end.(verbatim|Verbatim|lstlisting)/{
    in_verbatim=0
}
/.*begin.figure./{
    if (! in_verbatim) {
        output=1
        split($0, aux, "begin{figure}")
        print "\\begin{figure}" aux[2]
    }
    next
}
/.*end.figure./{
    output=0
    if (! in_verbatim) {
       split($0, aux, "end{figure}")
       print aux[1] "end{figure}"
       print "% ---------------------------------- "
   }
}
output==1{
    print
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .