我想解析目录树,以便将数字包含在latex文件中。我有多个目录,每个目录应该是latex中的一个figure标签,该目录中的每个图片都应该是一个子图并包含该图形。
即
2个目录,每个3张图片将导致
\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}
有人可以帮忙吗?我对bash不太熟悉。我已经知道了,如何遍历目录,但是提取文件名和目录名,并把它放到正确的地方就是杀了我。
谢谢!
编辑:从用户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
答案 0 :(得分:2)
好的,暂时这里是你想要达到的(我认为)想要达到的速度(用\n
快速和宽松地为人类而不是计算机解释):
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
如果那不是你想要的,请纠正我。
对于其中的一部分,我可能会使用大规模重命名文件的策略,
echo $filename | sed 's/find_and/replace_on_filename/g'
随着零件的充实,我会更新。
修改强>
固定。但这假设所有图像都在./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
从整个图像目录开始
$ 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
一个得到输出
$ 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}