我有一个 LaTeX 文档,它是从一组包含生成图像的源的其他文档生成的。这些图像最终大小不一,其中一些对于页面来说太宽了。我不想让它们缩小以适合页面宽度,而是希望当这些图像(及其标题)的宽高比大于 5×2 时自动旋转。
我无法自定义图形环境如何发射到生成的 LaTeX 文档中,但我可以完全控制文档序言(即生成内容之前的所有内容)。有没有办法可以设置图形环境以根据纵横比自动旋转?我目前正在使用graphicx
图形包,设置如下:
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
图像被发送到生成的 LaTeX 文档中,如下所示:
\begin{figure}
\hypertarget{generated-target-name}{%
\centering
\includegraphics{images/image-filename.png}
\caption{Caption for image}\label{generated-target-name}
}
\end{figure}
答案1
我假设只有当图像太宽时您才想旋转。
请注意,此版本每个图形仅允许一个标题和标签。
\documentclass{scrbook}
\usepackage{graphicx}
\usepackage{varwidth}
\usepackage{caption}
\usepackage{hyperref}
\let\oldfigure=\figure
\let\endoldfigure=\endfigure
\newsavebox{\figurebox}
\newif\ifcentering
\newif\ifcaption
\newif\ifcaptionlabel
\newcommand{\saveshortcaption}{}% reserve global names
\newcommand{\savecaption}{}
\newcommand{\savecaptionlabel}{}
\renewenvironment{figure}[1][tbp]{\oldfigure[#1]%
\centeringfalse
\captionfalse
\captionlabelfalse
\begin{lrbox}{\figurebox}%
\def\centering{\global\centeringtrue}%
\renewcommand{\caption}[2][\empty]{\global\captiontrue
\gdef\saveshortcaption{##1}%
\gdef\savecaption{##2}}%
\renewcommand{\label}[1]{\global\captionlabeltrue
\xdef\savecaptionlabel{##1}}%
\varwidth[b]{2\textheight}}% need some upper limit
{\endvarwidth
\end{lrbox}%
\ifcaption
\savebox{\figurebox}{\begin{minipage}[b]{\wd\figurebox}
\usebox{\figurebox}%
\ifx\empty\saveshortcaption\relax
\caption{\savecaption}%
\else
\caption[\saveshortcaption]{\savecaption}%
\fi
\ifcaptionlabel \label{\savecaptionlabel}\fi
\end{minipage}}%
\fi
\ifdim\wd\figurebox>\columnwidth
\ifdim 2\wd\figurebox > 5\ht\figurebox \savebox\figurebox{\rotatebox{90}{\box\figurebox}}\fi
\fi
\ifdim\wd\figurebox>\columnwidth
\dimen0=\columnwidth
\else
\dimen0=\wd\figurebox
\fi
\ifdim\ht\figurebox>\textheight
\dimen1=\textheight
\else
\dimen1=\ht\figurebox
\fi
\sbox0{\resizebox{\dimen0}{\dimen1}{\box\figurebox}}%
\ifcentering
\makebox[\columnwidth][c]{\box0}%
\else
\box0
\fi
\endoldfigure}
\begin{document}
\begin{figure}[htp]
\centering
\hypertarget{generated-target-name}{%
\rule{20cm}{6cm}}
\caption{Caption for image}\label{generated-target-name}
\end{figure}
\hyperlink{generated-target-name}{Using hyperlink}
Using \verb|\ref| \ref{generated-target-name}
\end{document}
一个更简单、限制更少的解决方案是使用 environ 包。
\documentclass{scrbook}
\usepackage{environ}
\usepackage{graphicx}
\usepackage{varwidth}
\usepackage{caption}
\usepackage{hyperref}
\let\oldfigure=\figure
\let\endoldfigure=\endfigure
\newif\ifcentering
\RenewEnviron{figure}[1][tbp]{\oldfigure[#1]%
\centeringfalse
\sbox0{% get width without caption
\def\centering{\global\centeringtrue}%
\renewcommand{\caption}[2][\empty]{}%
\renewcommand{\label}[1]{}%
\begin{varwidth}[b]{2\textheight}% need some upper limit
\BODY
\end{varwidth}}
\sbox1{% repeat with caption
\begin{minipage}[b]{\wd0}
\BODY
\end{minipage}}%
\ifdim\wd1>\columnwidth
\ifdim 2\wd1 > 5\ht1 \sbox1{\rotatebox{90}{\box1}}\fi
\fi
\ifdim\wd1>\columnwidth
\dimen0=\columnwidth
\else
\dimen0=\wd1
\fi
\ifdim\ht1>\textheight
\dimen1=\textheight
\else
\dimen1=\ht1
\fi
\sbox0{\resizebox{\dimen0}{\dimen1}{\box1}}%
\ifcentering
\makebox[\columnwidth][c]{\box0}%
\else
\box0
\fi
\endoldfigure}
\begin{document}
\begin{figure}[htp]
\centering
\hypertarget{generated-target-name}{%
\rule{20cm}{6cm}}
\caption{Caption for image}\label{generated-target-name}
\end{figure}
\hyperlink{generated-target-name}{Using hyperlink}
Using \verb|\ref| \ref{generated-target-name}
\end{document}