2

From a previous question (\includegraphics: get the "scale" value of a figure whose size is expressed by "width") I learned how to get the scale factor of a figure inserted by the \includegraphics command.

Now I'd like to print, as a watermark, the scaling value on each figure in my document. I started from this:

\documentclass{article}
\pdfoutput=1
\usepackage{graphicx}
\usepackage{color}

\makeatletter
\let\ORG@Gscale@box\Gscale@box
\long\def\Gscale@box#1{%
  \xdef\thelastscalefactor{#1}%
  \ORG@Gscale@box{#1}}
\makeatother

\begin{document}

\includegraphics[width=0.4\textwidth]{example-image}
\colorbox{red}{\the\thelastscalefactor}

\end{document}

enter image description here

The best solution would be to redefine the \includegraphics command to do this but it would be ok also to define a \myincludegraphics command like in another issue of mine https://tex.stackexchange.com/a/455293/33634.

Finally I also have an issue with the string \the\thelastscalefactor:

! You can't use `the character 0' after \the.
\thelastscalefactor ->0
                       .3437
3

Here is one solution:

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}

\makeatletter
\let\ORG@Gscale@box\Gscale@box
\long\def\Gscale@box#1{%
  \xdef\thelastscalefactor{#1}%
  \ORG@Gscale@box{#1}}
\makeatother

\newsavebox{\mybox}

% Make the top left corner of the annotation box start at 10% (1-0.9)
% from the left edge of the image and at 10% (0.1) from the top edge (this
% is why it lies exactly on the diagonal in the screenshot).
\newcommand*{\putimagescale}[1]{%
  \sbox{\mybox}{#1}%
  \usebox{\mybox}%
  \hbox to 0pt{\kern-0.9\wd\mybox
               \vbox to \ht\mybox{%
                 \kern0.1\ht\mybox
                 \hbox{\colorbox{red}{\thelastscalefactor}}%
                 \vss
               }%
               \hss
  }}

% Alternative method: the placement is done differently. In particular, the
% vertical offset is counted from the baseline of the image to the baseline of
% the annotation box.
% \newcommand*{\putimagescale}[1]{%
%   \sbox{\mybox}{#1}%
%   \usebox{\mybox}%
%   \makebox[0pt][r]{%
%     \raisebox{0.8\ht\mybox}[0pt][0pt]{%
%       \rlap{\colorbox{red}{\thelastscalefactor}}%
%     }%
%     \hspace*{0.9\wd\mybox}%
%   }%
% }

% You can uncomment the following if you want \includegraphics to be redefined
% so as to always print the image scale (obviating the need to manually call
% \putimagescale).
%
% \let\includegraphicsORI\includegraphics
% \renewcommand*{\includegraphics}[2][]{%
%   \putimagescale{\includegraphicsORI[#1]{#2}}}

\begin{document}

\putimagescale{%
  \includegraphics[width=0.4\textwidth]{example-image}}

\end{document}

TeXish technique

The alternative method gives (the vertical placement is not the same):

Screenshot of alternative method

  • Nice. I suggest this editing: \let\oldincludegraphics\includegraphics and \renewcommand\includegraphics[2][]{\putimagescale{\oldincludegraphics[#1]{#2}}}. Thank you. – Gabriele Nicolardi May 23 at 21:47
  • Feel free to do it in your document, but I find it maybe a bit excessive for general use, as it will then apply to all \includegraphics commands. Well, I can put it in the form of a comment. – frougon May 23 at 21:51
  • I've put your suggestion in the form of a comment, ready to be used, and added a different implementation for \putimagescale (most notably, vertical placement of the annotation is different). Look at the comments for details. – frougon May 23 at 23:08
3

TikZ might be a bit overkill, but at least I know how to do it ...

\documentclass{article}
\pdfoutput=1
\usepackage{tikz} % loads graphicx and xcolor

\makeatletter
\let\ORG@Gscale@box\Gscale@box
\long\def\Gscale@box#1{%
  \xdef\thelastscalefactor{#1}%
  \ORG@Gscale@box{#1}}
\makeatother

\newcommand\myincludegraphics[2][]{%
\begin{tikzpicture}
\node [inner sep=0] (img) {\includegraphics[#1]{#2}};
\node [below right,fill=red,draw=black] at (img.north west) {\thelastscalefactor}; % no \the needed
\end{tikzpicture}%
}

\begin{document}

\myincludegraphics[scale=1]{example-image}

\myincludegraphics[width=0.6\textwidth]{example-image}

\myincludegraphics[scale=0.5]{example-image}

\end{document}

enter image description here

  • Ok. I would modify your code adding \let\oldincludegraphics\includegraphics and using \renewcommand\includegraphics[2][]{... instead so you can keep the \includegraphics commands in the rest of the file. – Gabriele Nicolardi May 23 at 21:37
  • One advantage of tikz is that you can adjust the opacity. – John Kormylo May 24 at 16:00

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.