11

Is it possible to print the size of a given file in latex? For instance, suppose I have a file ./foo.jpg whose size is 381.3 KiB. I want a command \printfilesize so that \printfilesize{./foo.jpg} writes 381.3 KiB.

15

pdftex has primitive for querying the file system:

\documentclass{article}
\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 }
\fp_const:Nn \c_brian_GiB_fp { 1024*1024*1024 }
\ExplSyntaxOff

\begin{document}

\filesize{../duck.jpg}

\filesize[KiB]{../duck.jpg}

\end{document}

enter image description here

16
\documentclass{book}


\begin{document}
\pdffilesize {example-image-duck.pdf}


\end{document}

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.