来自上一个问题(\includegraphics:获取图形的“比例”值,其大小由“宽度”表示) 我学习了如何获取scale factor
通过\includegraphics
命令插入的图形的。
现在我想将文档中每个图形上的缩放值打印为水印。我从这里开始:
\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}
最好的解决方案是重新定义\includegraphics
命令来执行此操作,但也可以\myincludegraphics
像我的另一个问题中那样定义命令https://tex.stackexchange.com/a/455293/33634。
最后,我还遇到了一个与字符串有关的问题\the\thelastscalefactor
:
! You can't use `the character 0' after \the.
\thelastscalefactor ->0
.3437
答案1
以下是一种解决方案:
\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}
替代方法给出(垂直放置不一样):
答案2
TikZ 可能有点过头了,但至少我知道怎么做......
\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}