我正在为朋友的辅导班写一份试卷。其中一道题是速度-时间图。我希望 4 个图显示在 2x2 网格中,但使用下面的代码,它们显示在 4x1 结构中。我该如何更改此设置?
代码:
\question[1] A car, initially at rest, sets off a 2m/s and travels at this velocity for 2 seconds. It then decelerates a 1m/s$^2$ to 0m/s. It then accelerates at -0.5m/s$^2$ until it reaches a velocity of -1m/s. It stays at this velocity for 2 seconds before coming instantly to rest. Which of the following velocity-time graphs represents the motion of the car?
\begin{figure}[h!]
\centering
\begin{subfigure}[b]{0.2\textwidth}
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,2) -- (2,2) -- (4,0) -- (6,-1) -- (6,0) -- (8,0);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:a}
\end{subfigure}
\begin{subfigure}[b]{0.2\textwidth}
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,0) -- (2,0) -- (4,-2) -- (6,-3) -- (6,-3) -- (8,-3);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:b}
\end{subfigure}
\begin{subfigure}[t]{0.2\textwidth}
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,2) -- (2,2) -- (4,0) -- (6,1) -- (6,0) -- (8,0);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:c}
\end{subfigure}
\begin{subfigure}[b]{0.2\textwidth}
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,-2) -- (2,-2) -- (4,0) -- (6,-1) -- (6,0) -- (8,0);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:d}
\end{subfigure}
\end{figure}
答案1
假设您正在使用 documentclass exam
,我已制作以下 MWE。我已删除第一和第二之间以及第三和最后一之间的空行subfigure
。我还将子图的宽度增加到0.5\textwidth
(确保%
在后面添加\end{subfigure}
添加)。此外,我已将命令添加\centering
到所有subfigure
环境中,并将第三个子图的垂直对齐方式从更改为t
。b
最后,我还引入了siunitx
包,以便更统一地表示(并轻松定制)数值及其单位。
\documentclass{exam}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage[per-mode=symbol]{siunitx}
\begin{document}
\begin{questions}
\question[1] A car, initially at rest, sets off a \SI{2}{\meter\per\second} and travels at this velocity for 2 seconds. It then decelerates a \SI{1}{\meter\per\second\squared} to \SI{0}{\meter\per\second}. It then accelerates at \SI{-0.5}{\meter\per\second\squared} until it reaches a velocity of \SI{-1}{\meter\per\second}. It stays at this velocity for 2 seconds before coming instantly to rest. Which of the following velocity-time graphs represents the motion of the car?
\begin{figure}[h!]
\centering
\begin{subfigure}[b]{0.5\textwidth} \centering
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,2) -- (2,2) -- (4,0) -- (6,-1) -- (6,0) -- (8,0);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:a}
\end{subfigure}%
%
\begin{subfigure}[b]{0.5\textwidth} \centering
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,0) -- (2,0) -- (4,-2) -- (6,-3) -- (6,-3) -- (8,-3);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:b}
\end{subfigure}
\begin{subfigure}[b]{0.5\textwidth} \centering
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,2) -- (2,2) -- (4,0) -- (6,1) -- (6,0) -- (8,0);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:c}
\end{subfigure}%
%
\begin{subfigure}[b]{0.5\textwidth} \centering
\begin{tikzpicture}[scale=0.25]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,-2) -- (2,-2) -- (4,0) -- (6,-1) -- (6,0) -- (8,0);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw (0,2) node [left] {velocity(m/s)};
\draw (8,-1) node [right] {time(s)};
\end{tikzpicture}
\caption{}
\label{fig:d}
\end{subfigure}
\end{figure}
\end{questions}
\end{document}
答案2
作为对 @leandris 答案的补充,一些离题的更改可能对您有用。它们在第一个子图中标记为% <---
...
(红线表示页面布局)
\documentclass{article}
\usepackage{geometry}
\usepackage{subcaption}
\usepackage{tikz}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{figure}[h!]
\centering
\begin{subfigure}{0.4\linewidth} % <---
\begin{tikzpicture}[scale=0.5] % <---
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,2) -- (2,2) -- (4,0) -- (6,-1) -- (6,0) -- (8,0);
\draw[->] (0, 0) -- (8,0) node [right] {time (s)}; % <---
\draw[->] (0,-3) -- node [above,sloped] {velocity (m/s)} (0,3); % <---
\end{tikzpicture}
\caption{}
\label{fig:a}
\end{subfigure}
\hfil % <---
\begin{subfigure}{0.4\linewidth}
\begin{tikzpicture}[scale=0.5]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,0) -- (2,0) -- (4,-2) -- (6,-3) -- (6,-3) -- (8,-3);
\draw[->] (0, 0) -- (8,0) node [right] {time (s)};;
\draw[->] (0,-3) -- node [above,sloped] {velocity (m/s)} (0,3);
\end{tikzpicture}
\caption{}
\label{fig:b}
\end{subfigure}
\bigskip
\begin{subfigure}{0.4\linewidth}
\begin{tikzpicture}[scale=0.5]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,2) -- (2,2) -- (4,0) -- (6,1) -- (6,0) -- (8,0);
\draw[->] (0, 0) -- (8,0) node [right] {time (s)};;
\draw[->] (0,-3) -- node [above,sloped] {velocity (m/s)} (0,3);
\end{tikzpicture}
\caption{}
\label{fig:c}
\end{subfigure}
\hfil
\begin{subfigure}{0.4\linewidth}
\begin{tikzpicture}[scale=0.5]
\draw [gray] (0,-3) grid (8,3);
\draw [red] (0,-2) -- (2,-2) -- (4,0) -- (6,-1) -- (6,0) -- (8,0);
\draw[->] (0,0) -- (8,0);
\draw[->] (0,-3) -- (0,3);
\draw[->] (0, 0) -- (8,0) node [right] {time (s)};;
\draw[->] (0,-3) -- node [above,sloped] {velocity (m/s)} (0,3);
\end{tikzpicture}
\caption{}
\label{fig:d}
\end{subfigure}
\end{figure}
\end{document}
注意:请始终提供 MWE(最小工作示例),一个小但完整的文档,我们可以按原样编译。