如何正确对齐 7 个子图?

如何正确对齐 7 个子图?

正如标题所示,我在对齐figure环境中的一些子图时遇到了一些麻烦。到目前为止,我一直在修改值hspace{}以根据需要对齐子图。这是我的 MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{subfig}
\usepackage[dvipsnames]{xcolor}
\usepackage{geometry}
    \geometry{a4paper}

\begin{document}
    \begin{figure}[htbp]
        \hspace{-7em}
        \begin{minipage}{0.35\textwidth}
        \centering
        \subfloat[Pregunta 1]{
            \label{q1}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \end{minipage}
        \hspace{2em}
        \begin{minipage}{0.35\textwidth}
        \centering
        \subfloat[Pregunta 2]{
            \label{q2}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \end{minipage}
        \hspace{2em}
        \begin{minipage}{0.35\textwidth}
        \subfloat[Pregunta 3]{
            \label{q3}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \end{minipage}\par\medskip
        \hspace{-7em}
        \begin{minipage}{0.35\textwidth}
        \centering
        \subfloat[Pregunta 4]{
            \label{q4}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \end{minipage}
        \hspace{2em}
        \begin{minipage}{0.35\textwidth}
        \centering
        \subfloat[Pregunta 5]{
            \label{q5}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \end{minipage}
        \hspace{2em}
        \begin{minipage}{0.35\textwidth}
        \subfloat[Pregunta 6]{
            \label{q6}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \end{minipage}\par\medskip
        \centering
        \subfloat[Pregunta 7]{
            \label{q7}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \caption{Diagramas de barras del test post-experimento}
    \end{figure}
\end{document}

接下来是我得到的图像。如你所见b)e)不符合G)。这就是我想要修复的问题,但我不知道如何修复,因此如果能得到任何帮助我将不胜感激。

MWE 的输出

答案1

我认为没有必要超出边距。不要使用scale,而是设置宽度。并且不要忘记避免不必要的空格。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{subfig}
\usepackage[dvipsnames]{xcolor}
\usepackage{geometry}

\geometry{a4paper}

\begin{document}

\begin{figure}[htbp]
\centering

\subfloat[Pregunta 1]{% <--- important
  \label{q1}% <--- important
  \includegraphics[width=0.32\textwidth]{example-image-a}% <--- important
}\hfill
\subfloat[Pregunta 2]{%
  \label{q2}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}\hfill
\subfloat[Pregunta 3]{%
  \label{q3}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}

\medskip

\subfloat[Pregunta 4]{%
  \label{q4}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}\hfill
\subfloat[Pregunta 5]{%
  \label{q5}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}\hfill
\subfloat[Pregunta 6]{%
  \label{q6}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}

\medskip

\subfloat[Pregunta 7]{%
  \label{q7}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}

\caption{Diagramas de barras del test post-experimento}

\end{figure}

\end{document}

在此处输入图片描述

如果有八个子浮点数,最后一对可以输入为

\subfloat[Pregunta 7]{%
  \label{q7}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}\hspace{0.02\textwidth}%
\subfloat[Pregunta 8]{%
  \label{q7}%
  \includegraphics[width=0.32\textwidth]{example-image-a}%
}

这是因为前两行图像之间的空间是0.02\textwidth

在此处输入图片描述

如果您确实想超越边距,您可以通过使用\leftskip\rightskip设置为负值并设置一个等于文本宽度加上“超大尺寸”的参数以自动方式完成。

例如,我添加了showframe选项\geometry以便查看文本块边界。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{subfig}
\usepackage[dvipsnames]{xcolor}
\usepackage{geometry}

\geometry{a4paper,showframe}

\newlength{\oversized}

\begin{document}

\begin{figure}[htbp]
\centering
\addtolength{\leftskip}{-3em}
\addtolength{\rightskip}{-3em}
\setlength{\oversized}{\textwidth}
\addtolength{\oversized}{6em}

\subfloat[Pregunta 1]{%
  \label{q1}%
  \includegraphics[width=0.32\oversized]{example-image-a}%
}\hfill
\subfloat[Pregunta 2]{%
  \label{q2}%
  \includegraphics[width=0.32\oversized]{example-image-a}%
}\hfill
\subfloat[Pregunta 3]{%
  \label{q3}%
  \includegraphics[width=0.32\oversized]{example-image-a}%
}

\medskip

\subfloat[Pregunta 4]{%
  \label{q4}%
  \includegraphics[width=0.32\oversized]{example-image-a}%
}\hfill
\subfloat[Pregunta 5]{%
  \label{q5}%
  \includegraphics[width=0.32\oversized]{example-image-a}%
}\hfill
\subfloat[Pregunta 6]{%
  \label{q6}%
  \includegraphics[width=0.32\oversized]{example-image-a}%
}

\medskip

\subfloat[Pregunta 7]{%
  \label{q7}%
  \includegraphics[width=0.32\oversized]{example-image-a}%
}

\caption{Diagramas de barras del test post-experimento}

\end{figure}

\end{document}

在此处输入图片描述

答案2

  • minipage不需要
  • 文本宽度可以通过使用包\adjustwidth中定义的宏来局部放大changepage,但是,首先你需要问自己,这是否真的有必要
  • width使用键确定图像大小Gin可以简单地缩小所有适合的图像\textwidth(参见第二个示例)
\documentclass{article}
\usepackage[showframe,  % in real document remove this option
            a4paper]{geometry}

%\usepackage[utf8]{inputenc} this is default encoding
\usepackage[spanish]{babel}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage[strict]{changepage}

\begin{document}
    \begin{figure}[htbp]
    \setkeys{Gin}{width=0.32\linewidth} % <---
\begin{adjustwidth}{}{-\dimexpr\marginparsep+\marginparwidth}   % <--- locally enlarge text width 
  \centering
  \subfloat[][Pregunta 1    \label{q1}]{\includegraphics{example-image-a}}\hfill
  \subfloat[][Pregunta 2    \label{q2}]{\includegraphics{example-image-b}}\hfill
  \subfloat[][Pregunta 3    \label{q2}]{\includegraphics{example-image-c}}
  
\medskip
  \subfloat[][Pregunta 4    \label{q4}]{\includegraphics{example-image-a}}\hfill
  \subfloat[][Pregunta 5    \label{q5}]{\includegraphics{example-image-b}}\hfill
  \subfloat[][Pregunta 6    \label{q6}]{\includegraphics{example-image-c}}

\medskip
  \subfloat[][Pregunta 7    \label{q7}]{\includegraphics{example-image-duck}}

\caption{Diagramas de barras del test post-experimento}
\label{fig:q}
\end{adjustwidth}
    \end{figure}
\end{document}

在此处输入图片描述

(灰线表示页面布局)

如果您对较窄的图像感到满意,则只需在 ABove MWE 中删除\adjustwidth环境变量并设置Gin较窄的图像即可。例如:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[spanish]{babel}
\usepackage{graphicx}
\usepackage{subfig}

\begin{document}
    \begin{figure}[htbp]
    \setkeys{Gin}{width=0.32\textwidth} % <---
  \centering
\subfloat[][Pregunta 1    \label{q1}]{\includegraphics{example-image-a}}\hfill
\subfloat[][Pregunta 2    \label{q2}]{\includegraphics{example-image-b}}\hfill
\subfloat[][Pregunta 3    \label{q2}]{\includegraphics{example-image-c}}

\medskip
\subfloat[][Pregunta 4    \label{q4}]{\includegraphics{example-image-a}}\hfill
\subfloat[][Pregunta 5    \label{q5}]{\includegraphics{example-image-b}}\hfill
\subfloat[][Pregunta 6    \label{q6}]{\includegraphics{example-image-c}}

\medskip
\subfloat[][Pregunta 7    \label{q7}]{\includegraphics{example-image-duck}}

\caption{Diagramas de barras del test post-experimento}
\label{fig:q}
    \end{figure}
\end{document}

在此处输入图片描述

答案3

由于您正在更改边距,因此将所有内容放入一个迷你页面或\parbox具有正确宽度会有所帮助。我使用了,\parbox因为它不会改变\textwidth。使用\rlap\llap\makebox避免出现过满\hbox警告。

我注意到你在 marginpar 区域前就停了下来,因此我就从那里开始了\llap

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{subcaption}
\usepackage[dvipsnames]{xcolor}
\usepackage[showframe]{geometry}
    \geometry{a4paper}

\begin{document}
    \begin{figure}[htbp]
    \hspace*{\fill}\rlap{\hspace{\dimexpr \marginparsep-4pt}% stop 4pt short of marginpar area
      \llap{\parbox{\dimexpr 1.05\textwidth+4em+1pt}{% set new margins plus 1pt roudoff
        \centering
        \makebox[0.35\textwidth]{\subfloat[Pregunta 1\label{q1}]{%
            \includegraphics[scale=0.45]{example-image-a}
        }}%
        \hspace{2em}%
        \makebox[0.35\textwidth]{\subfloat[Pregunta 2\label{q2}]{
            \includegraphics[scale=0.45]{example-image-a}
        }}%
        \hspace{2em}%
        \makebox[0.35\textwidth]{\subfloat[Pregunta 3\label{q3}]{
            \includegraphics[scale=0.45]{example-image-a}
        }}
        \par\medskip
% I was curious how this would work
        \subfloat[Pregunta 4\label{q4}]{\includegraphics[scale=0.45]{example-image-a}}\hfill
        \subfloat[Pregunta 5\label{q5}]{\includegraphics[scale=0.45]{example-image-a}}\hfill
        \subfloat[Pregunta 6\label{q6}]{\includegraphics[scale=0.45]{example-image-a}}\par\medskip

        \subfloat[Pregunta 7]{
            \label{q7}
            \includegraphics[scale=0.45]{example-image-a}
            }
        \caption{Diagramas de barras del test post-experimento}
    }}}
    \end{figure}
\end{document}

相关内容