从 \includegraphics 生成图形编号和文件名列表

从 \includegraphics 生成图形编号和文件名列表

对于包含大量图片的图书项目,我需要生成包含的所有图片的文件名列表,方法是includegraphics{} 及其图号。 就像是:

1.1 ch01-intro/fig/lascaux.jpg
1.2 ch01-intro/fig/plot-weather-new.png
1.3 ch01-intro/fig/huygens1669.png
1.4 ch01-intro/fig/priestley-chart-biography-edit.png

CTAN 上有一段perl脚本,泰克森,它将找到文件名,但它不一定按顺序列出它们,也不会理解图号。(它旨在生成Makefile或为 LaTeX 项目准备 ZIP 文件。)

> texdepend -format=1 -print=f TOGS.tex |head
# C:\batchfiles\texdepend.pl, v0.96 (Michael Friendly ([email protected]))
# commandline: C:\batchfiles\texdepend.pl -format=1 -print=f TOGS.tex
# FIGS =
ch05-playfair/fig/playfair1805-inquiry-crop.jpg
ch01-intro/fig/lascaux.jpg
ch01-intro/fig/plot-weather-new.png
ch01-intro/fig/huygens1669.png
ch01-intro/fig/priestley-chart-biography-edit.png
ch01-intro/fig/marey-trains.png
ch01-intro/fig/Minard-cotton3.png

也有人问过类似的问题这里那里但没有一个要求包含图号。

通缉:一种修改includegraphics自身的方法,以便它也将行写入外部文件,形式如下

fignum filename

一个小的复杂因素是给定的begin{figure}环境可以包含两个或多个\includegraphics命令,例如:

\begin{figure}[htb]
    \centering
    \includegraphics[width=.49\textwidth]{\theChapter/fig/galton-interp2}
    \includegraphics[width=.49\textwidth]{\theChapter/fig/galton-interp3}
    \caption{A reconstruction of Galton's method for finding contours of
        approximately equal frequency in the relationship between heights of parents
        and their children. 
    }%
    \label{fig:galton-interp2}
\end{figure}

如果生成图 6.16,结果应该如下所示

6.16 ch06-scat/fig/galton-interp2.png
6.16 ch06-scat/fig/galton-interp3.png

答案1

\includegraphics通过在末尾添加更多代码来重新定义,人为地增加figure数量(在一组内)并写入ToC类似文件,以获得文件句柄机制\addtocontents的好处。ToC\tf@...

该文件将被命名,并且只有在使用\jobname.lfn时才会写入。\listoffigurenumbernames

删除demo实际用例的选项,我把它放在那里是因为我的磁盘上没有各种图形文件。

\documentclass{book}


\usepackage{xparse}
\usepackage[demo]{graphicx}
\usepackage{letltxmacro}


\LetLtxMacro\davidsincludegraphics\includegraphics

\makeatletter
\RenewDocumentCommand{\includegraphics}{O{}m}{%
  \davidsincludegraphics[#1]{#2}%
  \begingroup
  \advance\c@figure by \@ne
  \addtocontents{lfn}{\thefigure\space #2}
  \endgroup
}

\def\@starttocbutdonotshowit#1{%
  \begingroup
    \makeatletter
    \if@filesw
      \expandafter\newwrite\csname tf@#1\endcsname
      \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \@nobreakfalse
  \endgroup}


\newcommand{\listoffigurenumbernames}{%
  \@starttocbutdonotshowit{lfn}%
}

\begin{document}
\listoffigurenumbernames

\chapter{First}

\begin{figure}[htb]
    \centering
    \includegraphics[width=.49\textwidth]{\thechapter/fig/galton-interp2}
    \includegraphics[width=.49\textwidth]{\thechapter/fig/galton-interp3}
    \caption{A reconstruction of Galton's method for finding contours of
        approximately equal frequency in the relationship between heights of parents
        and their children. 
    }%
    \label{fig:galton-interp2}
\end{figure}


\begin{figure}[htb]
    \centering
    \includegraphics[width=.49\textwidth]{\thechapter/fig/galton-interp4}
    \includegraphics[width=.49\textwidth]{\thechapter/fig/galton-inter5}
    \caption{A reconstruction of Galton's method for finding contours of
        approximately equal frequency in the relationship between heights of parents
        and their children. 
    }%
    \label{fig:galton-interp4}
\end{figure}

\chapter{Second}

\begin{figure}[htb]
    \centering
    \includegraphics[width=.49\textwidth]{\thechapter/fig/galton-interp6}
    \includegraphics[width=.49\textwidth]{\thechapter/fig/galton-inter7}
    \caption{A reconstruction of Galton's method for finding contours of
        approximately equal frequency in the relationship between heights of parents
        and their children. 
    }%
    \label{fig:galton-interp5}
\end{figure}


\end{document}

这给出

1.1 1/fig/galton-interp2
1.1 1/fig/galton-interp3
1.2 1/fig/galton-interp4
1.2 1/fig/galton-inter5
2.1 2/fig/galton-interp6
2.1 2/fig/galton-inter7

改良版

使用启发式方法确定文件扩展名.jpg.pdf.pdf

\includegraphics宏有第四个可选参数,可用于覆盖文件扩展名。

\documentclass{book}


\usepackage{xparse}
\usepackage{graphicx}
\usepackage{letltxmacro}


\LetLtxMacro\davidsincludegraphics\includegraphics

\makeatletter

\RenewDocumentCommand{\includegraphics}{sO{}mo}{%
  \IfBooleanTF{#1}{%
    \davidsincludegraphics*[#2]{#3}%
  }{%
    \davidsincludegraphics[#2]{#3}%
  }%
  \begingroup
  % Trying to determine the extension
  \def\loc@l@ext{}
  \IfValueTF{#4}{%
    \def\loc@l@ext{#4}%
  }{%
    \IfFileExists{#3}{%
    }{%
      \IfFileExists{#3.pdf}{%
        \edef\loc@l@ext{.pdf}%
      }{%
        \IfFileExists{#3.jpg}{%
          \edef\loc@l@ext{.jpg}%
        }{%
          \edef\loc@l@ext{.png}%
        }%
      }%
    }%
  }%
  \advance\c@figure by \@ne
  \addtocontents{lfn}{\thefigure\space #3\loc@l@ext}
  \endgroup
}

\def\@starttocbutdonotshowit#1{%
  \begingroup
    \makeatletter
    \if@filesw
      \expandafter\newwrite\csname tf@#1\endcsname
      \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \@nobreakfalse
  \endgroup}


\newcommand{\listoffigurenumbernames}{%
  \@starttocbutdonotshowit{lfn}%
}



\begin{document}
\listoffigurenumbernames

\chapter{First}

\begin{figure}[htb]
    \centering
    \includegraphics[width=.49\textwidth]{galton-interp2}[.png]
    \includegraphics[width=.49\textwidth]{galton-interp3}
    \caption{A reconstruction of Galton's method for finding contours of
        approximately equal frequency in the relationship between heights of parents
        and their children. 
    }%
    \label{fig:galton-interp2}
\end{figure}


\begin{figure}[htb]
    \centering
    \includegraphics[width=.49\textwidth]{galton-interp4}
    \includegraphics[width=.49\textwidth]{galton-inter5}
    \caption{A reconstruction of Galton's method for finding contours of
        approximately equal frequency in the relationship between heights of parents
        and their children. 
    }%
    \label{fig:galton-interp4}
\end{figure}

\chapter{Second}

\begin{figure}[htb]
    \centering
    \includegraphics[width=.49\textwidth]{galton-interp6}
    \includegraphics[width=.49\textwidth]{galton-inter7}
    \caption{A reconstruction of Galton's method for finding contours of
        approximately equal frequency in the relationship between heights of parents
        and their children. 
    }%
    \label{fig:galton-interp5}
\end{figure}


\end{document}

相关内容