Question

I want to parse a directory tree for figures to include in a latex file. I have multiple directories and each directory should be a figure tag in latex and each picture in that directory should be a subfigure and include that graphic.

I.E.

2 dirs with 3 pictures each shall result in

\begin{figure}[tb]

\subfigure[file1]{
\includegraphics[width=0.48\textwidth]{./fig/dir1/file1}
        \label{image:file1}
        }
        \vspace{-0.4cm}
        \hfill
\subfigure[file2]{
        \includegraphics[width=0.48\textwidth]{./fig/dir1/file2}
        \label{image:file2}
        }
\subfigure[file3]{
        \includegraphics[width=0.48\textwidth]{./fig/dir1/file3}
        \label{image:file3}
        }
        \vspace{-0.4cm}
        \hfill

        \caption{DIR1}
        \label{image:dir1}
\end{figure}

\begin{figure}[tb]

\subfigure[file1]{
\includegraphics[width=0.48\textwidth]{./fig/dir2/file1}
        \label{image:file1}
        }
        \vspace{-0.4cm}
        \hfill
\subfigure[file2]{
        \includegraphics[width=0.48\textwidth]{./fig/dir2/file2}
        \label{image:file2}
        }
\subfigure[file3]{
        \includegraphics[width=0.48\textwidth]{./fig/dir2/file3}
        \label{image:file3}
        }
        \vspace{-0.4cm}
        \hfill

        \caption{DIR2}
        \label{image:dir2}
\end{figure}

Can anybody help? I'm not very familiar with bash. I already know, how to loop over the directories but exracting file name and directory name and put this into the right place is killing me.

Thanks!

EDIT: Adding some pseudocode from user kbshimmyo

For each directory
  If directory contains a file // which is always the case
    cat "\begin{figure}[tb]\n" to tmpfile
    For each file in directory (also, start counter i)
      cat "\subfigure[$part_of_the_filename]{
\includegraphics[width=0.48\textwidth]{./fig/$dirname/$filename}
\label{image:$part_of_the_filename}\n }\n" to tmpfile
      If (i == first or last)
        cat "\vspace{-0.4cm}\n \hfill\n" to tmpfile

    cat "\caption{$part_of_the_dirname}\n \label{image:$dirname}\n \end{figure}\n\n" to tmpfile
Was it helpful?

Solution

Ok, for the moment here's a sketch of what (I think) you want to attain (playing fast and loose with \n here for human, not computer interpretation):

For each directory
  If directory contains a file
    cat "\begin{figure}[tb]\n" to tmpfile
    For each file in directory (also, start counter i)
      cat "\includegraphics[width=0.48\textwidth]{./fig/$dirname/$filename}\n \label{image:file1}\n }\n" to tmpfile
      If (i == first or last)
        cat "\vspace{-0.4cm}\n \hfill\n" to tmpfile

    cat "\caption{uppercased $dirname}\n \label{image:$dirname}\n \end{figure}\n\n" to tmpfile

Correct me if that's not what you want.

For one piece of this, I'd probably use a tactic from mass-renaming files,

echo $filename | sed 's/find_and/replace_on_filename/g'

and this on bash counters.

I'll update as parts get fleshed out.


Edit Fixed. But this assumes that all images are in ./some_folder.

for dirname in $(ls -d */) 
  do echo $dirname
  MAXFILES=`ls -1 $dirname | wc -l`
  echo $MAXFILES
  if [ "$MAXFILES" -gt "0" ] 
    then echo "we have files"
    echo "\begin{figure}[tb]" >> latex_code

    FCOUNT=0
    for file in $(ls -1 $dirname)
      do echo $file | awk '{printf "\\subfigure[%s]{\n", $1}' >> latex_code
      echo $dirname $file | awk '{printf "     \\includegraphics[width=0.48\\textwidth]{./%s%s}\n", $1, $2}' >> latex_code
      echo $file | awk '{printf "     \\label{image:%s}\n", $1}' >> latex_code
      echo "     }" >> latex_code

      let FCOUNT=FCOUNT+1
      echo $FCOUNT
      if [ "$FCOUNT" -eq "1" -o "$FCOUNT" -eq "$MAXFILES" ]
        then echo "     \vspace{-0.4cm}" >> latex_code
        echo "     \hfill" >> latex_code
      fi
    done
      echo $dirname | sed 's,/,,g' | awk '{printf "     \\caption{%s}\n", toupper($1)}' >> latex_code
      echo $dirname | sed 's,/,,g' | awk '{printf "     \\label{image:%s}\n", $1}' >> latex_code
      echo "\\end{figure}" >> latex_code
      echo "" >> latex_code
    fi
  done

so starting within the overall image directory

$ ls *
latex_code   non_dir_file tmp          tmpfile

blah_images:
bi_01

cool_images:
ci_01 ci_02 ci_03

important_images:
ii_01 ii_02 ii_03 ii_04 ii_05

stunning_images:
si_01 si_02

one gets output

$ touch latex_code
$ ./myscript
$ cat latex_code 
\begin{figure}[tb]
\subfigure[bi_01]{
     \includegraphics[width=0.48\textwidth]{./blah_images/bi_01}
     \label{image:bi_01}
     }
     \vspace{-0.4cm}
     \hfill
     \caption{BLAH_IMAGES}
     \label{image:blah_images}
\end{figure}

\begin{figure}[tb]
\subfigure[ci_01]{
     \includegraphics[width=0.48\textwidth]{./cool_images/ci_01}
     \label{image:ci_01}
     }
     \vspace{-0.4cm}
     \hfill
\subfigure[ci_02]{
     \includegraphics[width=0.48\textwidth]{./cool_images/ci_02}
     \label{image:ci_02}
     }
\subfigure[ci_03]{
     \includegraphics[width=0.48\textwidth]{./cool_images/ci_03}
     \label{image:ci_03}
     }
     \vspace{-0.4cm}
     \hfill
     \caption{COOL_IMAGES}
     \label{image:cool_images}
\end{figure}

\begin{figure}[tb]
\subfigure[ii_01]{
     \includegraphics[width=0.48\textwidth]{./important_images/ii_01}
     \label{image:ii_01}
     }
     \vspace{-0.4cm}
     \hfill
\subfigure[ii_02]{
     \includegraphics[width=0.48\textwidth]{./important_images/ii_02}
     \label{image:ii_02}
     }
\subfigure[ii_03]{
     \includegraphics[width=0.48\textwidth]{./important_images/ii_03}
     \label{image:ii_03}
     }
\subfigure[ii_04]{
     \includegraphics[width=0.48\textwidth]{./important_images/ii_04}
     \label{image:ii_04}
     }
\subfigure[ii_05]{
     \includegraphics[width=0.48\textwidth]{./important_images/ii_05}
     \label{image:ii_05}
     }
     \vspace{-0.4cm}
     \hfill
     \caption{IMPORTANT_IMAGES}
     \label{image:important_images}
\end{figure}

\begin{figure}[tb]
\subfigure[si_01]{
     \includegraphics[width=0.48\textwidth]{./stunning_images/si_01}
     \label{image:si_01}
     }
     \vspace{-0.4cm}
     \hfill
\subfigure[si_02]{
     \includegraphics[width=0.48\textwidth]{./stunning_images/si_02}
     \label{image:si_02}
     }
     \vspace{-0.4cm}
     \hfill
     \caption{STUNNING_IMAGES}
     \label{image:stunning_images}
\end{figure}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top