获取正在渲染的图形的尺寸

获取正在渲染的图形的尺寸

假设我的 LaTeX 文档中有一个图形:

\begin{figure}
  \includegraphics[scale=0.5]{some_graphic}
\end{figure}

我的问题很简单:有没有办法将some_graphic要渲染的尺寸(以点/英寸/任何单位)放入变量中,以供文档的其余部分使用?以下是来自 beamer 的一个稍微更合理的激励示例:

\begin{frame}
  \begin{block} \begin{block}<2->{Title}
    Text on the first part.
    \only<2>{
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \vspace{2.19in}  % Would be nice not to have to tweak this value manually
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    }
    \only<3>{
    \begin{figure}\centering
      \includegraphics[width=0.60\textwidth,clip,trim=0in 2.1in 0in 0in]{figures/scheme}
    \end{figure}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \vspace{0.775in} % Would be nice not to have to tweak this value manually
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    }   
    \only<4>{
    \begin{figure}\centering
      \includegraphics[width=0.60\textwidth]{figures/scheme_new}
    \end{figure}
    }
  \end{block}
\end{frame}

换句话说,我意识到可以通过调整 vspace 值手动解决此问题,但这不是很合理,而且似乎违背了 TeX 背后让渲染引擎完成工作的整个想法。

答案1

执行此操作的文字方法如下所示:

\usepackage{calc}
...
\def\mygraphic{\includegraphics{...}}

\新长度\图形高度
\setlength\graphicheight{\heightof{\mygraphic}}
% 或:\settoheight\graphicheight{\mygraphic}
...
\mygraphic % 插入图形
...
\vspace{\graphicheight} % 空白与图形大小相同

但是还有一个\phantom命令可以创建一个与其内容大小相同的空框,这可能更适合您的具体情况:

\mygraphic %插入图形
...
\phantom{\mygraphic} % 插入相同大小的空白框

此外,\includegraphics还可以选择draft用占位符替换单个图像,因此

\def\mygraphic\{includegraphics[draft]{...}}

可能比包含实际图像的效果更好。

答案2

这是我最近开发的用于测量图像尺寸的解决方案

\documentclass[11pt]{article} 
\usepackage{graphicx}

\begin{document}
\newlength{\imageh}
\newlength{\imaged}
\newlength{\imagew}

\newcommand{\setimageh}[1]{
 \settoheight{\imageh}{\usebox{#1}}
}

\newcommand{\setimagew}[1]{
 \settowidth{\imagew}{\usebox{#1}}
}

\newcommand{\setimaged}[1]{
 \settodepth{\imaged}{\usebox{#1}}
}


\newcommand{\ImageDimensions}[1]{
   % create and save the box
  \newsavebox{\Image}
  % Set graphics to fixed size for this example
  \savebox{\Image}{\includegraphics[width=80pt, height=120pt]{#1}}
  \centering\usebox{\Image}\
  \setimageh{\Image}
  \setimagew{\Image}
  \setimaged{\Image}
  \footnotesize
     {\vskip7pt
      The height of the image (#1) is : \the\imageh\\
     The width of the  image (#1) is : \the\imagew\\
     The depth of the  image (#1) is : \the\imaged\\}
}

\ImageDimensions{./DFS8U.png}
\end{document}

渲染效果如下所示。显示三个尺寸(宽度、高度和深度)

测量图片尺寸的示例渲染

答案3

如果这是针对 Beamer 的,那么另一种解决方案可能是研究\begin{overlayarea}该命令应允许您用另一个图形替换一个图形,同时保持覆盖区域外的文本不变。请参阅第 79 页等等。beamer 用户指南。

答案4

灵感来自这个答案马丁·沙勒♦我想说你可以使用这个包adjustbox以获得这些尺寸。

关键特征与之相关gstore,您可以使用测量值来解决您的问题。

以下是一个例子:

\documentclass{beamer}

\usepackage{adjustbox}
\newlength\myHeight % creating lenghts
\newlength\myWidth

\begin{document}

\begin{figure}
    \adjustbox{gstore width=\myWidth, gstore height=\myHeight, center}{
        \includegraphics[scale=0.5]{example-image}
    }
\end{figure}

Height: \the\myHeight % print Height

Width: \the\myWidth % print Width

\end{document}

在此处输入图片描述

相关内容