Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The following example works in so far as it shows the image in the source latex file and disregards everything else. Is there some way of performing the same task but also list information about each image such as its the dimensions, file size and filename below each image? Only the preview should have that.

I am hoping that not only can I accomplish that but that I don't have to manually type in the file size and other info myself -- although if there is no way to grab the info automatically then the next best would be to add the info manually. Either way, I would like to know how to accomplish this.

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage[active,tightpage,pdflatex,graphics,showlabels]{preview}

\begin{document}

\lipsum[1]

\begin{center}
\includegraphics{elephant}
\end{center}

\lipsum[1]

\end{document}

Result (the grey part around the image was not part of the output but is an artifact of my capturing process so that part can be disregarded):

screenshot

(Creative Commons info on elephant image available here .)

share|improve this question
add comment

1 Answer

up vote 15 down vote accepted

The following solution redefines \includegraphics (in the form with one optional argument) and prints extra data below the image if preview is loaded and active. Since version 1.30 pdfTeX has some nice commands that extracts file modification date, file size or the MD5 sum of the file.

The image sizes are get by graphics \Gin@nat@width and \Gin@nat@height. However they get modified by option trim, for example. Perhaps more clear is the "final image size". It is just the size that the image occupies on the paper. It is measured by putting the \includegraphics in a box.

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}

% Store the old meaning of \includegraphics
\usepackage{letltxmacro}% safer variant
\LetLtxMacro\OldIncludegraphics\includegraphics

% \FileName{<file>} prints the file name
\usepackage{url}
\DeclareUrlCommand\FileName{\urlstyle{sf}}

% Packages for extracting and printing file meta data
% (for pdfTeX and LuaTeX in both modes)
\usepackage{pdftexcmds}
\usepackage{filemod}

\makeatletter
\newsavebox{\graphics@box}
\newcommand*{\NewIncludegraphics}[2][]{%
  \begingroup
    % catch the right file name by hooking into \@addtofilelist
    % that is called by \includegraphics
    \gdef\graphics@filename{#2}% default
    \gdef\graphics@natsize{\textit{unknown}}%
    \let\org@addtofilelist\@addtofilelist
    \renewcommand*{\@addtofilelist}[1]{%
      \org@addtofilelist{##1}%
      \xdef\graphics@filename{##1}%
      \xdef\graphics@natsize{%
        \the\Gin@nat@width\,$\times$\,\the\Gin@nat@height
      }%
    }%
    \sbox{\graphics@box}{\OldIncludegraphics[{#1}]{#2}}%
    \begin{tabular}{@{}l@{ }l@{}}%
      \multicolumn{2}{@{}l@{}}{\usebox{\graphics@box}}\tabularnewline
      \LaTeX\ name: & \FileName{#2}\tabularnewline
      File name: &
      \expandafter\FileName\expandafter{\graphics@filename}\tabularnewline
      File date: &
      \filemodprint{\graphics@filename}\tabularnewline
      File size: &
      \pdf@filesize{\graphics@filename}\tabularnewline
      File MD5 sum: &
      \pdf@filemdfivesum{\graphics@filename}\tabularnewline
      Natural image size: & \graphics@natsize\tabularnewline
      Final image size: &
      \the\wd\graphics@box\,$\times$\,%
      \the\dimexpr\ht\graphics@box + \dp\graphics@box\relax
      \tabularnewline
    \end{tabular}%
  \endgroup
}
\renewcommand*{\includegraphics}{\NewIncludegraphics}
\makeatother

\usepackage[active,tightpage,pdflatex,graphics,showlabels]{preview}

% if package preview is loaded and active,
% \ifPreview is defined and true.
\ifx\ifPreview\iftrue
\else
  % otherwise switch to unchanged behaviour of \includegraphics
  \let\includegraphics\OldIncludegraphics
\fi

\begin{document}

\lipsum[1]

\begin{center}
\includegraphics[scale=.5]{elephant}
\end{center}

\lipsum[1]

\end{document}

Result:

enter image description here

share|improve this answer
    
I'm speechless. Absolutely brilliant. :) I wrote a humble code trying to answer this question - My idea was to call the external identify command from ImageMagick via \write18 and then gather the output. My code is not even close to your majestic answer. :) Thanks a million for joining us, it's really an honour to have you here. :) –  Paulo Cereda Jul 29 '12 at 14:33
    
@Paulo, Even though Heiko's answer is amazing I would still be interested at looking at other approaches too even if they are not as good. –  user1189687 Jul 31 '12 at 20:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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