![了解 \resizebox 的缩放比例](https://linux22.com/image/368381/%E4%BA%86%E8%A7%A3%20%5Cresizebox%20%E7%9A%84%E7%BC%A9%E6%94%BE%E6%AF%94%E4%BE%8B.png)
是否有可能知道\resizebox
某物的规模以便在 a 中进一步使用\scalebox
?
为了说明清楚,请看以下例子:
\documentclass{article}
\usepackage[paperwidth=12cm]{geometry}
\usepackage{graphicx}
\begin{document}
\def\figurea{\includegraphics[width=5cm]{example-image-a}}
\def\figureb{\includegraphics[width=2.5cm]{example-image-b}}
This is \texttt{fig-a} with its natural size:
\figurea
\bigskip
This is \texttt{fig-b} with its natural size:
\figureb
\bigskip
This is \texttt{fig-a} scaled to fit the page width:
\resizebox{\textwidth}{!}{\figurea}
\bigskip
How do I rescale \texttt{fig-b} to keep its relative size compared to \texttt{fig-a} without knowing its actual size?
\bigskip
\texttt{Insert a nice piece of code here :)}
\end{document}
我希望具有fig-b
相同的相对大小,但fig-a
实际上并不知道它们任何一个的大小。
另外,如示例中所示,图形之间会有文本,因此将它们放在同一位置\resizebox
不是一个选择。
答案1
\resizebox
只是\scalebox
比例因子就是你要求的数字。所以你可以说服它显示该数字,而不是仅在本地组中使用它然后丢弃它。
\documentclass{article}
\usepackage[paperwidth=12cm]{geometry}
\usepackage{graphicx}
\makeatletter
\let\zz\Gscale@box
\long\def\Gscale@box#1{%
\xdef\thelastscalefactor{#1}%
\zz{#1}}
\makeatother
\begin{document}
\def\figurea{\includegraphics[width=5cm]{example-image-a}}
\def\figureb{\includegraphics[width=2.5cm]{example-image-b}}
This is \texttt{fig-a} with its natural size:
\figurea
\bigskip
This is \texttt{fig-b} with its natural size:
\figureb
\bigskip
This is \texttt{fig-a} scaled to fit the page width:
\noindent%you need this:-)
\resizebox{\textwidth}{!}{\figurea}
\let\savethis\thelastscalefactor
\bigskip
How do I rescale \texttt{fig-b} to keep its relative size compared to \texttt{fig-a} without knowing its actual size?
\bigskip
\noindent
\scalebox{\savethis}{\figureb}
\end{document}
答案2
这里我定义了\Resizebox
,它是的变体,\resizebox
接受一个可选参数;如果不存在,它的作用与相同\resizebox
,如果存在,它应该是一个(可定义的)控制序列,其中存储比例因子,以供后续使用。
\documentclass{article}
\usepackage[paperheight=40cm,paperwidth=21cm]{geometry}
\usepackage{graphicx}
\usepackage{xfp}
\newcommand\figureA{\includegraphics[width=5cm]{example-image-a}}
\newcommand\figureB{\includegraphics[width=2.5cm]{example-image-b}}
\makeatletter
\newcommand{\Resizebox}[4][]{%
\if\relax\detokenize{#1}\relax
\resizebox{#2}{#3}{#4}%
\else
\begingroup
\sbox0{#4}%
\sbox2{\resizebox{#2}{#3}{#4}}%
\@ifdefinable{#1}{\xdef#1{\fpeval{\wd2/\wd0}}}%
\usebox{2}%
\endgroup
\fi
}
\makeatother
\begin{document}
\raggedright
This is \texttt{figure-a} with its natural size:
\figureA
\bigskip
This is \texttt{figure-b} with its natural size:
\figureB
\bigskip
This is \texttt{figure-a} scaled to fit the page width:
\Resizebox[\scaleforfigureA]{\textwidth}{!}{\figureA}
\bigskip
How do I rescale \texttt{figure-b} to keep its relative
size compared to \texttt{figure-a} without knowing its actual size?
\bigskip
\texttt{Insert a nice piece of code here :)}
\scalebox{\scaleforfigureA}{\figureB}
\end{document}
答案3
您可以使用
\resizebox{\fpeval{(2.5cm) / (5cm)}\textwidth}{!}{\figureB}
其中\fpeval
提供xfp
。
\textwidth
本质上,您是根据两个图形的宽度比将图像调整为缩放后的版本。在本例中,这是已知的,因此(2.5cm) / (5cm)
除了使用之外,评估似乎是多余的.5
。但一般来说,这可能是未知的。
下面的示例略有不同,用于2.5pc
较小的图像:
\documentclass{article}
\usepackage[paperwidth=12cm]{geometry}
\usepackage{graphicx,xfp}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\def\figureA{\includegraphics[width=5cm]{example-image-a}}
\def\figureB{\includegraphics[width=2.5pc]{example-image-b}}
This is \texttt{figureA} with its natural size:
\figureA
\bigskip
This is \texttt{figureB} with its natural size:
\figureB
\bigskip
This is \texttt{figureA} scaled to fit the page width:
\resizebox{\textwidth}{!}{\figureA}
\bigskip
How do I rescale \texttt{figureB} to keep its relative size compared to \texttt{figureA} without knowing its actual size?
\bigskip
\resizebox{\fpeval{(2.5pc) / (5cm)}\textwidth}{!}{\figureB}
\end{document}
您还可以使用
\resizebox{\fpeval{\textwidth * (2.5pc) / (5cm)}pt}{!}{\figureB}
\fpeval
因为上维度的结果是用p
oin t
s 来表示的。
如果图形尺寸未知,则可以将图形存储在盒子中,以便可以很容易地从中提取第w
id
个和/或h
eigh 个t
。下面是考虑到这一点而编写的上述示例:
\documentclass{article}
\usepackage[paperwidth=12cm]{geometry}
\usepackage{graphicx,xfp}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\newsavebox{\figureAbox}% Store figure a in a box
\savebox{\figureAbox}{\includegraphics[width=5cm]{example-image-a}}
\newsavebox{\figureBbox}% Store figure b in a box
\savebox{\figureBbox}{\includegraphics[width=2.5pc]{example-image-b}}
This is \texttt{figureA} with its natural size:
\usebox\figureAbox% figure a
\bigskip
This is \texttt{figureB} with its natural size:
\usebox\figureBbox% figure b
\bigskip
This is \texttt{figureA} scaled to fit the page width:
\resizebox{\textwidth}{!}{\usebox\figureAbox}% Scaled figure a
\bigskip
How do I rescale \texttt{figureB} to keep its relative size compared to \texttt{figureA} without knowing its actual size?
\bigskip
\resizebox{\fpeval{\textwidth * (\wd\figureBbox) / (\wd\figureAbox)}pt}{!}{\usebox\figureBbox}% Scaled figure b
\end{document}