![自动生成图形标题](https://linux22.com/image/440356/%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%9B%BE%E5%BD%A2%E6%A0%87%E9%A2%98.png)
我的 latex 文件中包含数百张图片。有什么方法可以自动为每张图片创建标题吗?例如,文件 picture1 会生成一个标题 picture1。
\begin{document}
\begin{figure*}[h]
\section{Title}
\subsection{SubTitle}
\begin{subfigure}{0.5\linewidth}
\includegraphics[width=\linewidth, height=0.5\linewidth]{Figure1.png}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}{0.5\linewidth}
\includegraphics[width=\linewidth, height=0.5\linewidth]{Figure2.png}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}{0.5\linewidth}
\includegraphics[width=\linewidth, height=0.5\linewidth]Figure3.png}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}{0.5\linewidth}
\includegraphics[width=\linewidth, height=0.5\linewidth]{Figure4.png}
\caption{}
\end{subfigure}
\end{figure*}
\end{document}
答案1
ShowSubFigure
有两个参数,两个要并排显示为子图的图形的文件名。
ShowFigure
同样作为两个参数:页面居中的单个图形的文件名及其宽度。
更新后续跟进
\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{caption}
\captionsetup[figure]{labelformat=parens,labelsep = space, labelfont=bf}
\renewcommand{\figurename}{}% without the word "Figure"
\usepackage{kantlipsum} % dummy text (to be removed)
\usepackage{showframe} % show margins (to be removed)
\newenvironment{ShowSubFigure}[2]
{ \begin{minipage}[b]{0.4\linewidth}
\includegraphics[width=\linewidth, keepaspectratio]{#1}
\caption{\texttt{#1}}
\end{minipage}\hfill
\begin{minipage}[b]{0.4\linewidth}
\includegraphics[width=\linewidth, keepaspectratio]{#2}
\caption{\texttt{#2}}
\end{minipage}
}
\newenvironment{ShowFigure}[2]
{\begin{figure}[h]
\centering
\includegraphics[width=#2, keepaspectratio]{#1}
\caption{\texttt{#1}}
\end{figure}
}
\makeatletter
\setlength{\@fptop}{0pt} % figure on top of the page
\setlength{\@fpsep}{15pt} % float separation
\makeatother
\begin{document}
\kant[1]
\begin{figure*}[th!]
\begin{ShowSubFigure}{example-image-a}{example-image-c}\end{ShowSubFigure}
\end{figure*}
\begin{figure*}[ht!]
\begin{ShowSubFigure}{example-image-b}{example-image-b}\end{ShowSubFigure}
\end{figure*}
\kant[2]
\begin{figure*}[th!]
\begin{ShowSubFigure}{example-image-c}{example-image-a}\end{ShowSubFigure}
\end{figure*}
\kant[9]
\begin{ShowFigure}{example-grid-100x100pt}{0.3\textwidth}\end{ShowFigure}
\end{document}
自动化替代方案
如果从操作系统生成了图形列表(如dir / b / s * .jpg > ListFig.txt
),则可以自动执行该过程以生成包含所有缩略图的文档。
.csv
然后构建一个(逗号分隔值)文件,每行有两个文件名,以逗号分隔。
将数据加载到数据库中(使用包datatool
),然后逐行遍历,同时将文件名插入到环境中ShowSubFigure
。
这个非常基本的示例生成了 7 页,每页 6 幅图像,按顺序编号。
\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{datatool} % added <<<<<<<<<<
\usepackage{calculator}% added <<
\newcounter{nrow}
\setcounter{nrow}{1}
\begin{filecontents*}[overwrite]{ImagesList.csv}
example-image-a, example-image-c
example-image-b, example-image-b
example-image-c, example-image-a
example-image-a, example-image-c
example-image-b, example-image-b
example-image-c, example-image-a
example-image-a, example-image-c
example-image-b, example-image-b
example-image-c, example-image-a
example-image-a, example-image-c
example-image-b, example-image-b
example-image-c, example-image-a
example-image-a, example-image-c
example-image-b, example-image-b
example-image-c, example-image-a
example-image-a, example-image-c
example-image-b, example-image-b
example-image-c, example-image-a
example-image-a, example-image-c
example-image-b, example-image-b
example-image-c, example-image-a
\end{filecontents*}
\newenvironment{ShowSubFigure}[2]
{\begin{minipage}[b]{0.3\linewidth}
\includegraphics[width=\linewidth, keepaspectratio]{#1}
\subcaption{\texttt{#1}}
\end{minipage}\hfill
\begin{minipage}[b]{0.3\linewidth}
\addtocounter{figure}{1}
\includegraphics[width=\linewidth, keepaspectratio]{#2}
\subcaption{\texttt{#2}}
\end{minipage}
}
\makeatletter
\renewcommand{\thesubfigure}{\@arabic\c@figure}
\makeatother
\begin{document}
\DTLloaddb[noheader]{index}{ImagesList.csv} % load the database
\DTLforeach{index}{\first=Column1, \second=Column2}{%
\INTEGERDIVISION{\value{nrow}}{3}{\sola}{\solb}
\ifthenelse{\solb =1}{\clearpage}{}%
\begin{figure*}% display two figure side by side
\begin{ShowSubFigure}{\first}{\second}\end{ShowSubFigure}
\end{figure*}\stepcounter{nrow}
}
\end{document}