Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
6  
\usepackage{endfloat} will do it for you, writing a filename.fff file containing exactly what you need. –  egreg Jan 10 '13 at 14:19
    
Related/Possible answer: tex.stackexchange.com/a/89972/11002 –  tohecz Jan 10 '13 at 14:20
    
@egreg, amazing! I tried it now and the file fff contains \begin{figure*} even if I use non stared environment. –  Sigur Jan 10 '13 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 ;) –  tohecz Jan 10 '13 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 Jan 10 '13 at 14:33
show 5 more comments

2 Answers

up vote 5 down vote accepted

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;
}
share|improve this answer
    
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 Jan 10 '13 at 16:07
    
Please, post also your solution! :) –  biologue Jan 10 '13 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. –  Ethan Bolker Jan 10 '13 at 16:20
add comment

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
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.