有条件地覆盖 includegraphics 命令以在相应的标题中打印文件大小

有条件地覆盖 includegraphics 命令以在相应的标题中打印文件大小

我想分析报告中使用的每个图形文件的文件大小。因此,我试图通过有条件地覆盖命令来实现一个新命令,includegraphics以在相应的标题中打印文件大小。像往常一样,在我的报告中,每个includegraphics命令都有一个分配了一些文本的标题。conditionally我说的意思是,如果我设置一个标志,命令includegraphics应该在相应的标题中打印文件大小。

首先,请参阅下面的示例报告-

\documentclass[paper=A4, fontsize=12pt, DIV=calc, BCOR=0mm, Ipagesize=auto, draft=false]{scrreprt}
\usepackage{graphicx}
\DeclareGraphicsExtensions{.pdf,.jpeg,.jpg,.png}
\usepackage{subfigure}

\begin{document}
\chapter{Chapter 1}
Sentences are omitted to keep the code short.
\begin{figure}[!htbp]
  \centering
  \includegraphics[width=0.45\linewidth]{tiger}
  \caption{A real caption on a figure}
  \label{img:ch1}
\end{figure}

\chapter{Chapter 2}
Sentences are omitted to keep the code short.
\begin{figure}[!htbp]
 \centering
 \subfigure[A real caption on a subfigure]{
  \includegraphics[width=0.45\linewidth]{tiger}
 }
 \subfigure[Another real caption on a subfigure]{
  \includegraphics[width=0.45\linewidth]{tiger}
 }
 \caption{A real caption}
 \label{img:ch2}
\end{figure}
\end{document}

现在,请参阅下面的代码以了解我想要实现的目标-

\documentclass[paper=A4, fontsize=12pt, DIV=calc, BCOR=0mm, Ipagesize=auto, draft=false]{scrreprt}
\usepackage{graphicx}
\DeclareGraphicsExtensions{.pdf,.jpeg,.jpg,.png}
\usepackage{subfigure}

% ---------------------------------------
% src: https://tex.stackexchange.com/a/461742/49520
\usepackage{xfp}
\ExplSyntaxOn
\NewDocumentCommand{\filesize}{O{B}m}
 {
  \fpeval{ round( \pdffilesize { #2 } / \fp_use:c { c_brian_#1_fp } , 2) }
  \,#1
 }

\fp_const:Nn \c_brian_B_fp { 1 }
\fp_const:Nn \c_brian_KiB_fp { 1024 }
\fp_const:Nn \c_brian_MiB_fp { 1024*1024 }
\ExplSyntaxOff
% ---------------------------------------

\begin{document}
\chapter{Chapter 1}
Sentences are omitted to keep the code short.
\begin{figure}[!htbp]
  \centering
  \includegraphics[width=0.45\linewidth]{tiger}
  \caption{\filesize[KiB]{tiger.pdf}}
  \label{img:ch1}
\end{figure}


\chapter{Chapter 2}
Sentences are omitted to keep the code short.
\begin{figure}[!htbp]
 \centering
 \subfigure[{\filesize[KiB]{example-image-a}}]{
  \includegraphics[width=0.45\linewidth]{example-image-a}
 }
 \subfigure[{\filesize[KiB]{example-image-b}}]{
  \includegraphics[width=0.45\linewidth]{example-image-b}
 }
 \caption{A real caption}
 \label{img:ch2}
\end{figure}
\end{document}

生成的 PDF 如下所示:

在此处输入图片描述

以下是上述代码中的问题(以及预期的改进)-

  1. 您可以看到,我已手动更新了每个命令的标题includegraphics。相反,我只希望在设置标志时才能看到文件大小。其余时间,我希望看到分配的实际标题。
  2. 每个命令的文件名filesize都是手动分配的。相反,我希望命令filesize自动从相应的includegraphics命令中获取文件名。
  3. 希望全局定义一个标志,当设置时启用文件大小打印,当未设置时显示真实标题。

答案1

与其更改 的定义\includegraphics,不如使用另一个宏来调用\includegrahpics并插入 ,这样更简洁\caption。为了使它在子图中也能工作,我建议从 切换到subfiguresubcaption它提供了环境。我发现这比(包的)subfigure更直观,也大大简化了你的问题。subfigure\subfigure

通过对您的方法进行这些调整,这应该就是您想要的。请注意,为了确保\includegraphics\filesize都找到相同的文件,在中指定扩展名可能更安全\includegraphicswithfilesizecaption。此外,如果您计划使用\graphicspath或任何其他graphicx影响文件搜索方式的功能,则必须将这些功能添加到的实现中\includegraphicswithfilesizecaption

最后要说的是:确实不能使用[!htbp]。不过,这又是另一回事了。

\documentclass{scrreprt}

\usepackage{xparse}
\usepackage{graphicx}
\usepackage{subcaption}

\usepackage{mwe} % Just for example images

% ---------------------------------------
% src: https://tex.stackexchange.com/a/461742/49520
\usepackage{xfp}
\ExplSyntaxOn
\NewDocumentCommand{\filesize}{O{B}m}
 {
  \fpeval{ round( \pdffilesize { #2 } / \fp_use:c { c_brian_#1_fp } , 2) }
  \,#1
 }

\fp_const:Nn \c_brian_B_fp { 1 }
\fp_const:Nn \c_brian_KiB_fp { 1024 }
\fp_const:Nn \c_brian_MiB_fp { 1024*1024 }
\ExplSyntaxOff
% ---------------------------------------

\makeatletter
  \newif\ifshowgraphicssize
  \NewDocumentCommand\includegraphicswithfilesizecaption{ O{} O{} m }{%
    \includegraphics[#1]{#3}%
    \ifshowgraphicssize
      \caption{\filesize[#2]{#3}}%
      \let\caption\@gobbleom
    \fi
  }
  \NewDocumentCommand\@gobbleom{ o m }{}
\makeatother

\showgraphicssizetrue % Remove this to get the "normal" captions.

\begin{document}

\chapter{Chapter 1}
Sentences are omitted to keep the code short.
\begin{figure}[!htbp]
  \centering
  \includegraphicswithfilesizecaption[width=0.45\linewidth][KiB]{example-grid-100x100pt}
  \caption{Hello}
  \label{img:ch1}
\end{figure}


\chapter{Chapter 2}
Sentences are omitted to keep the code short.
\begin{figure}[!htbp]
  \begin{subfigure}{.5\textwidth}
    \centering
    \includegraphicswithfilesizecaption[width=.9\linewidth][KiB]{example-image-a}
  \end{subfigure}%
  \begin{subfigure}{.5\textwidth}
    \centering
    \includegraphicswithfilesizecaption[width=.9\linewidth][KiB]{example-image-b}
  \end{subfigure}
  \caption{A real caption}
  \label{img:ch2}
\end{figure}

\end{document}

相关内容