如下面的 MWE 所示,我正在将 graphicsx 与 adjustbox 结合使用。我有一个包含许多图形的文档,并且想自动将超出文本宽度的图形放置在“内部”(而不是“中心”)。我花了很多时间使用 LaTeX,但这超出了我的能力范围,到目前为止,我对解决方案的搜索一直没有结果。感谢您的帮助!
PS 我的 MWE 可能可以更精简,但我已经包含了我在本文档中使用的所有与浮点数或图形有关的包。
\documentclass[
paper=8.5in:11in,
BCOR=0.5in,
twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}
\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}
%% Adjust box adds "inner" and "center" options to includegraphics
\usepackage[export]{adjustbox}
\begin{document}
\mainmatter
I would like to match the behavior of this:
\begin{figure}[h!t]
\includegraphics[scale=1,inner]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\includegraphics[scale=0.5,center]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\newpage
Here:
\newcommand*{\myincludegraphics}[2]{%
\includegraphics[#1,center%% some conditional argument
]{#2}%
}
\begin{figure}[h!t]
\myincludegraphics{scale=1}{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\myincludegraphics{scale=0.5}{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\end{document}
答案1
您可以直接使用\makebox
:
\documentclass[
paper=8.5in:11in,
BCOR=0.5in,
twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}
\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}
\usepackage{adjustbox}
\newsavebox{\igbox}
\newcommand{\igdecide}{%
\ifdim\wd\igbox<\textwidth
c%
\else
\ifoddpage
l%
\else
r%
\fi
\fi
}
\newcommand{\fincludegraphics}[2][]{%
\par
\sbox\igbox{\includegraphics[#1]{#2}}%
\checkoddpage
\makebox[\textwidth][\igdecide]{\usebox{\igbox}}%
}
\begin{document}
\mainmatter
\begin{figure}[!ht]
\fincludegraphics[width=1.1\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\fincludegraphics[width=0.7\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\newpage
\begin{figure}[!ht]
\fincludegraphics[width=1.1\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\fincludegraphics[width=0.7\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\end{document}
答案2
\documentclass[
paper=8.5in:11in,
BCOR=0.5in,
twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}
\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}
%% Adjust box adds "inner" and "center" options to includegraphics
\usepackage[export]{adjustbox}
% customizing LaTeX innards. Still possible, for the time being.
\makeatletter
\def\bm@IC{\hss\edef\width{\dimexpr\the\width\relax}%
\ifdim\width>\linewidth
\ifoddpageoroneside\kern\dimexpr\width-\linewidth\fi
\fi
\unhbox\@tempboxa
\ifdim\width>\linewidth
\unless\ifoddpageoroneside\kern\dimexpr\width-\linewidth\fi
\fi
\hss}
\newcommand*{\myincludegraphics}[2][]{%
{\parindent\z@ \parfillskip\z@skip\checkoddpage
\makebox[\linewidth][IC]{\includegraphics[#1]{#2}}\par}%
}
\makeatother
\begin{document}
\mainmatter
I would like this behavior :
\begin{figure}[h!t]
\myincludegraphics[scale=1]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\myincludegraphics[scale=0.5]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\newpage
\begin{figure}[h!t]
\myincludegraphics[scale=1]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\myincludegraphics[scale=0.5]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\newpage
\begin{figure}[h!t]
\includegraphics[scale=1, inner]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\includegraphics[scale=0.5, center]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\newpage
\begin{figure}[h!t]
\includegraphics[scale=1, inner]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\includegraphics[scale=0.5, center]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\end{document}
答案3
这里有一个基于adjustbox
使用precode
key 的解决方案。遗憾的是,无法通过一次调用来实现这一点,\adjustimage
因为\width
key 保留的不是新的缩放宽度,而是原始宽度。因此需要在里面嵌套\includegraphics
或。\adjustbox
\adjustbox
\documentclass[
paper=8.5in:11in,
BCOR=0.5in,
twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}
\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}
%% Adjust box adds "inner" and "center" options to includegraphics
\usepackage[export]{adjustbox}
\begin{document}
\mainmatter
I would like to match the behavior of this:
\begin{figure}[h!t]
\includegraphics[scale=1.2,inner]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\includegraphics[scale=0.5,center]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\newpage
Here:
\newcommand*{\myincludegraphics}[2]{%
\adjustbox{precode=%
\ifdim\width>\textwidth
\expandafter\innersidebox
\else
\expandafter\centerbox
\fi
}{\includegraphics[#1]{#2}}%
}
\begin{figure}[h!t]
\myincludegraphics{scale=1.2}{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\myincludegraphics{scale=0.5}{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\newpage
And here:
\begin{figure}[h!t]
\myincludegraphics{scale=1.2}{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\myincludegraphics{scale=0.5}{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}
\end{document}