我正在使用 minipage 环境来水平对齐四个图形:
\usepackage[]{caption}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{compatibility=false}
\begin{figure}
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]
{Figures/fig1.png}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]
{Figures/fig2.png}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]
{Figures/fig3.png}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]
{Figures/fig4.png}
\subcaption{}
\end{minipage}
\end{figure}
我想将这四幅图像向左移动几个像素。我尝试了以下两种方法,但都不起作用:
- 替换
\centering
为\raggedright
。 \bigskip
在小页面内的每个图形后添加一个空格(即)。
知道如何实现这个吗?
编辑:
我很抱歉造成混淆。我真正想做的是调整图形的位置,使它们看起来更“对齐”。以下是我的图样:
顶部的图是带有彩条的摄影师图像,底部的图是一些绘图。它们都居中对齐。但是,由于摄影师图像带有彩条,它们看起来好像与底部的绘图有点偏离。我想水平调整它们的位置以获得如下所示的效果:
答案1
使用tabular
相同大小的列、左对齐和手动偏移是实现您需要的另一种方法,甚至可以达到居中效果;可以在偏移中包含/排除其他图像组件。
由于我没有您的图像文件,代码会稍微长一些,因为我画了额外的条形图来展示偏移是如何工作的。您只需要使用\includegraphics
适当的大小。这是每个变成子标题subcaptiongroup
的环境,您以后可以参考。是一个方便的宏,可以避免不必要的重复。\caption
\setkeys
Using `tabular` with columns of the same size, left alignment, and manual offsets would be another approach to achieve what you need, even an effect of centring; additional image components can be included/excluded in offsets.
As I don't have your image files, the code is slightly longer because I draw extra bars in order to show how offsetting can work. You just need to use `\includegraphics` with proper size. The `subcaptiongroup` is the environment in which each `\caption` is turned into sub caption, you can later refer to. `\setkeys` is a convenient macro to avoid unnecessary repetitions.
```latex
\documentclass{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{compatibility=false}
\newlength\offset
\usepackage{xcolor}
\begin{document}
\begin{figure}[tbh]
\setlength\tabcolsep{0pt}
\begin{subcaptiongroup}
\setkeys{Gin}{width=2.2cm,height=1.5cm}
\setlength\offset{0.5\dimexpr0.25\textwidth-2.2cm}
\begin{tabular*}{\linewidth}{
*4{>{\hspace{\offset}}p{0.25\linewidth}}
}
\includegraphics{example-image}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
& \includegraphics{example-image-a}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
& \includegraphics{example-image-b}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
& \includegraphics{example-image-c}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
\end{tabular*}
\setlength\offset{0.5\dimexpr0.25\textwidth-2cm}
\setkeys{Gin}{width=2cm,height=1.5cm}
\begin{tabular*}{\linewidth}{
*4{>{\hspace{\offset}}p{0.25\linewidth}}
}
\includegraphics{example-image}\caption{}
& \includegraphics{example-image-a}\caption{}
& \includegraphics{example-image-b}\caption{}
& \includegraphics{example-image-c}\caption{}
\end{tabular*}
\end{subcaptiongroup}
\caption{The main caption}
\end{figure}
\end{document}
编辑。
如果您想更改图像、子标题和主标题之间的间距,请使用\captionsetup[]{}
。如果您想减少两行之间的间距,您必须同时更改skip
和belowskip
。如果使用可选参数,则仅影响特定组,例如子图。首先使用一些常规设置,然后使用仅与特定组相关的特定设置
会很方便。例如:\captionsetup{}
\captionsetup[...]{}
\captionsetup{skip=-3pt,position=bottom}
将减少所有(子)标题和图像之间的距离,并将位置设置为底部。另一方面,以下行
\captionsetup[subfigure]{belowskip=-6pt}
只会影响子图,而“其他”设置保持不变。
这是更新的完整代码
\documentclass{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{position=bottom,skip=0pt}
\captionsetup[subfigure]{belowskip=-6pt}
\newlength\offset
\usepackage{xcolor}
\begin{document}
\begin{figure}[tbh]
\setlength\tabcolsep{0pt}
\begin{subcaptiongroup}
\setkeys{Gin}{width=2.2cm,height=1.5cm}
\setlength\offset{0.5\dimexpr0.25\textwidth-2.2cm}
\begin{tabular*}{\linewidth}{
*4{>{\hspace{\offset}}p{0.25\linewidth}}
}
\includegraphics{example-image}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
& \includegraphics{example-image-a}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
& \includegraphics{example-image-b}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
& \includegraphics{example-image-c}%
\textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
\end{tabular*}
\setlength\offset{0.5\dimexpr0.25\textwidth-2cm}%
\setkeys{Gin}{width=2cm,height=1.5cm}%
\begin{tabular*}{\linewidth}{
*4{>{\hspace{\offset}}p{0.25\linewidth}}
}
\includegraphics{example-image}\caption{}
& \includegraphics{example-image-a}\caption{}
& \includegraphics{example-image-b}\caption{}
& \includegraphics{example-image-c}\caption{}
\end{tabular*}
\end{subcaptiongroup}
\caption{The main caption}
\end{figure}
\end{document}
以及截图
答案2
我想将这四幅图像向左移动几个像素。
我假设您真正想要实现的是展开子图,使它们占据整个文本块的宽度。这可以通过在或环境\hfill
之间插入来实现。请注意,环境只不过是知道在遇到(或)指令时该做什么的。minipage
subfigure
subfigure
minipage
\caption
\subcaption
\documentclass{article} % or some other suitable document class
\usepackage{showframe} % draw frame around text block
\usepackage[demo]{graphicx} % remove 'demo' option in real document
\usepackage[skip=0.333\baselineskip]{caption}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{compatibility=false}
\begin{document}
\null
\begin{figure}[h]
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]{Figures/fig1.png}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]{Figures/fig2.png}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]{Figures/fig3.png}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.24\linewidth}
\centering
\includegraphics[width=0.95\linewidth]{Figures/fig4.png}
\subcaption{}
\end{minipage}
\caption{Before}
\end{figure}
\begin{figure}[h]
\begin{subfigure}[t]{0.24\linewidth}
\includegraphics[width=1\linewidth]{Figures/fig1.png}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.24\linewidth}
\includegraphics[width=1\linewidth]{Figures/fig2.png}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.24\linewidth}
\includegraphics[width=1\linewidth]{Figures/fig3.png}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.24\linewidth}
\includegraphics[width=1\linewidth]{Figures/fig4.png}
\caption{}
\end{subfigure}
\caption{After}
\end{figure}
\end{document}
答案3
更新问题编辑后。
我想实现两个目标:(1)将文本区域中的每一“行”居中,(2)对齐四个“列”。
在第一个图中,“列”对齐,但行未在页面上水平居中。这通常是通过\hfill
在小页面之间插入来实现的。
但是如第二张图所示,当图像的尺寸不同时,列就不对齐了。当四个小页面的宽度小于行宽的 1/4 时,这种影响最为明显。
最后一张图的行居中,列对齐。这是通过使用第一张图中的代码并分别将每行居中来实现的
\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{showframe} % show margins
\begin{document}
\begin{figure}[hbt!]
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]{example-image-a}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
\smallskip
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]
{example-image-b}
\subcaption{}
\end{minipage}
\caption{Align, row not centered}
\end{figure}
\begin{figure}[hbt!]
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]{example-image-a}
\subcaption{}
\end{minipage}
\hfill % added <<<<<
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
\hfill % added <<<<<
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
\hfill % added <<<<<
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
\smallskip
\begin{minipage}[t]{0.22\linewidth} % changed <<<<<<<
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
\hfill % added <<<<<
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
\hfill % added <<<<<
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
\hfill % added <<<<<
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]
{example-image-b}
\subcaption{}
\end{minipage} % changed <<<<<<<
\caption{Not aligned but row centered using hfill}
\end{figure}
\begin{figure}[hbt!]
\hfill\mbox{\begin{minipage}[t]{0.22\linewidth} % changed <<<<<<<
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]{example-image-a}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth,height=0.6\linewidth]
{example-image-a}
\subcaption{}
\end{minipage}}\hfill % changed <<<<<<<
\smallskip
\hfill\mbox{\begin{minipage}[t]{0.22\linewidth} % changed <<<<<<<
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\subcaption{}
\end{minipage}
%
\begin{minipage}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]
{example-image-b}
\subcaption{}
\end{minipage}}\hfill % changed <<<<<<<
\smallskip
\caption{Aligned and centered}
\end{figure}
\end{document}
最后,使用Mico的指示(使用子图环境)简化了代码。
\begin{figure}[h]
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth]{example-image-a}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth]{example-image-a}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth]{example-image-a}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.6\linewidth]{example-image-a}
\caption{}
\end{subfigure}
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\caption{}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.22\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{example-image-b}
\caption{}
\end{subfigure}
\caption{Using subfigure}
\end{figure}