# environment subfigure undefined

I want to place two images side by side and I have seen LaTeX figures side by side but one of the answers give me the Environment subfigure undefined error and I haven't the slightest idea why. The idea is to place two subfigures side-by-side within a main figure.

Here's my code:

\documentclass [10pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\usepackage{hyphenat}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{geometry}
\usepackage{subfig}

\begin{document}

\begin{figure}

\centering
\begin{subfigure}{0.5\textwidth}
\centering
\includegraphics[width=0.4\textwidth]{impedância Ri1.png}
\caption{*caption*}
\label{fig:sub1}
\end{subfigure} %

\begin{subfigure}{0.5\textwidth}
\centering
\includegraphics[width=0.4\textwidth]{impedância ri2.png}
\caption{*caption*}
\label{fig:sub2}
\end{subfigure}

\caption{*caption*}

\end{figure}

\end{document}


Any help?

• Welcome to tex.sx. I'm pretty sure this has been addressed before, but haven't yet found a good question to point to. May 26 at 18:38
• Maybe @barbarabeeton meant this comment by egreg May 26 at 18:45
• @WillieWong -- Yes, that works. Thanks. May 26 at 18:47

You mix two different approaches together. You cannot use both of them at same time. I include both methods to achieve same result:

\usepackage{subcaption}

\documentclass [10pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\usepackage{hyphenat}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{geometry}
\usepackage{subcaption}

\begin{document}
Left is \ref{fig:sub1}. Right is \ref{fig:sub2}
\begin{figure}
\centering
\begin{subfigure}{0.4\textwidth}
\includegraphics[width=\textwidth]{example-image}
\caption{*caption*}
\label{fig:sub1}
\end{subfigure}\hskip1ex
\begin{subfigure}{0.4\textwidth}
\includegraphics[width=\textwidth]{example-image}
\caption{*caption*}
\label{fig:sub2}
\end{subfigure}
\caption{*caption*}
\end{figure}
\end{document}


\usepackage{subfig}

\documentclass [10pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\usepackage{hyphenat}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{geometry}
\usepackage{subfig}

\begin{document}
Left is \ref{fig:sub1}. Right is \ref{fig:sub2}.
\begin{figure}
\centering
\subfloat[*caption*]{\includegraphics[width=0.4\textwidth]{example-image}\label{fig:sub1}}\hskip1ex
\subfloat[*caption*]{\includegraphics[width=0.4\textwidth]{example-image}\label{fig:sub2}}
\caption{*caption*}
\end{figure}
\end{document}


output:

• BTW, the subcaption package also supports \subfloat. May 26 at 21:40