0

I want to analyze the file size of each graphics file used in my report. Therefore, I am trying to implement a new command by conditionally overriding the includegraphics command to print file size in the corresponding caption. As usual, in my report, each includegraphics command is having a caption that is assigned with some text. By saying conditionally, I mean that if I set a flag, the includegraphics command should print the file size in the corresponding caption.

First, please see below for a sample report-

\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}

Now, please see the code below to understand what I am trying to achieve-

\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}

This is what the generated PDF looks like-

enter image description here

Following are the problems (and expected improvements) in the code above-

  1. You can see that I have manually updated the caption for each includegraphics command. Instead, I am excepting to see the file size only when a flag is set. Rest of the time, I want to see the real caption assigned.
  2. The filename to each filesize command is assigned manually. Instead, I want the filesize command to automatically grab the filename from corresponding includegraphics command.
  3. Expecting to define a flag globally which enables filesize printing when set and shows real caption when unset.
ravi
  • 1,618

1 Answers1

1

Instead of changing the definition of \includegraphics, it is much cleaner to use a different macro that calls \includegrahpics and inserts the \caption. For this to also work in subfigures, I recommend switching from subfigure to subcaption, which provides the subfigure environment. I find this much more intuitive than subfigure's (the package's) \subfigure and it also very much simplifies your problem.

With these adjustments to your approach, this should pretty much be what you want. Note that in order to make sure that \includegraphics and \filesize both find the same file, it is probably safer to specify the extension in \includegraphicswithfilesizecaption. Also, if you plan on using \graphicspath or any of the other graphicx features that affect how files are searched, you will have to add those to the implementation of \includegraphicswithfilesizecaption.

As a final note: It is really not okay to use [!htbp]. That's a different can of worms, though.

\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}
schtandard
  • 14,892
  • Thank you very much, It works like a charm. The package subcaption is easier. On a side note, can you tell an alternative of [!htbp] for a long report. I don't think [tbp] is better though. – ravi Jun 08 '20 at 02:33
  • 1
    @RaviJoshi Short answer: Just don't use the optional argument at all! When you are finished writing your document, you can go through it and adjust the float placement by moving the environment in the source and adding optional arguments to single floats. If you want to change the default behavior, do that in the preamble (though you probably shouldn't). If you don't want your environment to float, don't use a floating environment. Related reading: This answer (and the others there) and this one. – schtandard Jun 09 '20 at 09:07
  • Thank you again! – ravi Jun 09 '20 at 10:19