# Subcaption error

Im using TexStudio 2.12.22. When trying to make subfigures I get the following error:

! Illegal unit of measure (pt inserted).

! Missing number, treated as zero.

*
l.157 \begin{subfigure}{0.5*\textwidth}"

\usepackage{subcaption}
\begin{figure}[h]
\begin{subfigure}{0.5*\textwidth}
\includegraphics[width=\textwidth]{example-image-a}
\end{subfigure}
\hfill
\begin{subfigure}{0.5*\textwidth}
\includegraphics[width=\textwidth]{example-image-b}
\end{subfigure}
\end{figure}


Any ideas? Thanks

UPDATE Thanks to Leandris, the * was messing it up. replaced with {0.5\textwidth}

• Tried it with 0.2 and 0.01 but still get the same error. I also get:! Missing number, treated as zero. – potatasbravas Mar 15 at 13:33
• Remove the *. \begin{subfigure}{0.5\textwidth} should work but will result in an overfull box warning. – leandriis Mar 15 at 13:33
• Thanks it was the * that was messing things up. – potatasbravas Mar 15 at 13:37

Remove the *. \begin{subfigure}{0.5\textwidth} instead of \begin{subfigure}{0.5*\textwidth} works perfectly fine.

Placing two subfigures next to each other that both take up half the textwidth will result in an overfull box warning as there will be a small white spce between them. This can be overcome when removing the \hfill and when placing a %sign at the end of the first subfigure environment. Alternatively you could also decrease the width of both subfigures.

(Red lines indicate the start of the margins.)

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\begin{document}
\begin{figure}[h]
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=\textwidth]{example-image-a}
\end{subfigure}
\hfill
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=\textwidth]{example-image-b}
\end{subfigure}
\end{figure}

\begin{figure}[h]
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=\textwidth]{example-image-a}
\end{subfigure}%
%    \hfill
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=\textwidth]{example-image-b}
\end{subfigure}
\end{figure}

\begin{figure}[h]
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{example-image-a}
\end{subfigure}
\hfill
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{example-image-b}
\end{subfigure}
\end{figure}

\end{document}