# Formatting sub figures which use tikz

I'm writing an exam paper for a friend's tutoring business. One of the questions is going to be on velocity-time graphs. I wanted the 4 graphs to show up in a 2x2 grid but using the code below they are showing up in a 4x1 structure. How can I change this?

The code:

\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}

• Welcome to TeX.SE! Remove blank line between firs and second and also between third and fourth sub figures. – Zarko Jun 22 at 19:10
• Replace the empty lines between the first and second, and third and fourth subfigure by \quad? – marmot Jun 22 at 19:10
• that tikzpicture is part of the main document flow, it shouldn't be in a figure environment – David Carlisle Jun 22 at 19:27

## 2 Answers

Assuming that you are using the exam documentclass, I have made the following MWE. I have removed the empty lines between first and second as well as between the third and last subfigure. I have also increased the width of the subfigures to 0.5\textwidth (Make sure to add a % after \end{subfigure}). Additionally, I have added the \centering command into all subfigure environment and changed the vertical alignment of the third subfigure from t to b. Lastly, I have also introduced the siunitx package for a more uniform representation (and easy customization) of the numerical values and their units.

\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}


As supplement to @leandris answer, some off-topic changes which my be useful to you. They are marked (in the first sub figure) by % <--- ...

(red lines indicate page layout)

\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}


Note: please always provide MWE (Minimal Working Example), a small but complete document, which we can compile as it is.