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-
Following are the problems (and expected improvements) in the code above-
- You can see that I have manually updated the caption for each
includegraphicscommand. 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. - The filename to each
filesizecommand is assigned manually. Instead, I want thefilesizecommand to automatically grab the filename from correspondingincludegraphicscommand. - Expecting to define a flag globally which enables filesize printing when set and shows real caption when unset.
