- 一位朋友写硕士论文并向我寻求帮助。
- 他想使用子图形。
- “没问题”我说...两个小时后我们就到了:)
- 我认为这个
subcaption
包最适合这项任务。 - 我尝试准备一组精美的示例并在计算宽度时遇到了问题。
- 问题:根据我(显然是错误的)的想法,我的总和是 100%,但我得到了一个换行符。
- 我需要少于 100% 才能将所有数字放在一行。
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{graphicx}
% Here: H option for float placement
\usepackage{float}
% caption and subcaption work together
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\section*{Two Figures}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hspace{0.1\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}
\begin{equation}
45\,\% + 10\,\% + 45\,\% = 100\,\%
\end{equation}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.44\textwidth]{example-image-a}}%
\hspace{0.1\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.44\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}
\begin{equation}
44\,\% + 10\,\% + 44\,\% = 98\,\%
\end{equation}
\pagebreak
\section*{Three Figures}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-c}}%
\caption{Caption}
\end{figure}
\begin{equation}
30\,\% + 5\,\% + 30\,\% + 5\,\% + 30\,\% = 100\,\%
\end{equation}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.29\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.29\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.29\textwidth]{example-image-c}}%
\caption{Caption}
\end{figure}
\begin{equation}
29\,\% + 5\,\% + 29\,\% + 5\,\% + 29\,\% = 97\,\%
\end{equation}
\pagebreak
\section*{Four Figures}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.2125\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.2125\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.2125\textwidth]{example-image-c}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.2125\textwidth]{example-image}}%
\caption{Caption}
\end{figure}
\begin{equation}
21.25\,\% + 5\,\% + 21.25\,\% + 5\,\% + 21.25\,\% + 5\,\% +21.25\,\% = 100\,\%
\end{equation}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image-c}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}
\begin{equation}
20\,\% + 5\,\% + 20\,\% + 5\,\% + 20\,\% + 5\,\% +20\,\% = 95\,\%
\end{equation}
\end{document}
问题
计算总宽度时考虑哪些长度?
症状解决方案
用户米科指出 由于长度可变,因此
\hfill
全部替换可以解决问题/症状。\hspace{0.xx\textwidth}
我还是不明白为什么计算结果不准确。
问题的解决方案
用户阿拉什·埃斯巴蒂和用户米科\hspace{0.xx\textwidth}
找到了根本原因。我需要用来掩盖后面%
的换行符\hspace{0.1\textwidth}%
。换行符增加了一个额外的空格,导致总宽度超过 100%。
有关的
最终解决方案和一些附加示例
\documentclass{article}
\usepackage[
showframe, % This option shows the margins and so on
]{geometry}
\usepackage{graphicx}
% Here: H option for float placement
\usepackage{float}
% caption and subcaption work together
\usepackage{subcaption} % loads the caption package
\begin{document}
\section{Two Figures}
\subsection{Horizontal}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}
\subsection{Vertical}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\\ % <-- Line break
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}
\section{Three Figures}
\subsection{Horizontal}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}
\subsection{Vertical}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\\ % <-- Line break
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}
\subsection{Mixed}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}
\section{Four Figures}
\subsection{Horizontal}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}
\subsection{Mixed}
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.45\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}{\includegraphics[width=0.45\textwidth]{example-image}}%
\caption{Caption}
\end{figure}
\section{Symmetric Padding}
See the following example with small picture widths.
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}
You can use the optional argument of the \texttt{\textbackslash subcaptionbox} in order to define the width of the sub element.
\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}
Here I try to illustrate the idea behind the solution -- \textbf{don't use this code}!
\begin{figure}[H]
\centering
\fbox{%
\subcaptionbox{Subcaption A}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-a}}%
}%
\hfill % <-- Seperation
\fbox{%
\subcaptionbox{Subcaption B}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-b}}%
}%
\\ % <-- Line break
\fbox{%
\subcaptionbox{Subcaption C}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-c}}%
}%
\hfill % <-- Seperation
\fbox{%
\subcaptionbox{Subcaption D}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image}}%
}%
\caption{Caption}
\end{figure}
\end{document}
答案1
因为缺少钉子,鞋子丢了,
因为没有马蹄铁,马就丢了,
因为缺少马,骑士失踪了,
...
%
每次出现以下情况后,你都会丢失一个字符
\hspace{<some fraction of \textwidth>}
有了这些人物,一切都会好起来,王国不会消失,每个人都会从此过上幸福的生活。
话虽如此,我认为输入\hfill
(带或不带终止%
字符)一定比进行明确的长度计算来确定参数更容易\hspace
。
完整的 MWE,使用\hfill
:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{graphicx}
\usepackage{subcaption} % load 'caption' automatically
\begin{document}
\begin{figure}[t!]
\subcaptionbox{Subcaption A}{%
\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill
\subcaptionbox{Subcaption B}{%
\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Two subfigures}
\end{figure}
\begin{figure}[h!]
\subcaptionbox{Subcaption A}{%
\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hfill
\subcaptionbox{Subcaption B}{%
\includegraphics[width=0.30\textwidth]{example-image-b}}%
\hfill
\subcaptionbox{Subcaption C}{%
\includegraphics[width=0.30\textwidth]{example-image-c}}%
\caption{Three subfigures}
\end{figure}
\begin{figure}[h!]
\subcaptionbox{Subcaption A}{%
\includegraphics[width=0.225\textwidth]{example-image-a}}%
\hfill
\subcaptionbox{Subcaption B}{%
\includegraphics[width=0.225\textwidth]{example-image-b}}%
\hfill
\subcaptionbox{Subcaption C}{%
\includegraphics[width=0.225\textwidth]{example-image-c}}%
\hfill
\subcaptionbox{Subcaption D}{%
\includegraphics[width=0.225\textwidth]{example-image}}%
\caption{Four subfigures}
\end{figure}
\end{document}