[Tex/LaTex] Figures\Subfigures referencing in Latex

cross-referencingsubfloats

I have a figure with 3 subfigures. I used the \ref{fig:2} in my text and I got 2a. Can anyone knows how to make it 2(a) instead of 2a?

Below is an example of my code:

\begin{document}

\begin{figure}

\centering

\begin{subfigure}[b]{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{graph1.jpg}
    \caption{$y=x$}
    \label{fig:1}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{graph2.jpg}
    \caption{$y=sinx$}
    \label{fig:2}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{graph3.jpg}
    \caption{$y=cosx$}
    \label{fig:3}
\end{subfigure}
\caption{Three simple graphs}
\label{fig:three graphs}
\end{figure}

\end{document}

With Thanks

Best Answer

You can just do

\renewcommand{\thesubfigure}{(\alph{subfigure})}
\captionsetup[sub]{labelformat=simple}

Here's a complete example; notice that \hfill should go next to \end{subfigure} and that sinx should be \sin x (similarly for \cos x).

\documentclass[a4paper]{article}
\usepackage{caption,subcaption,graphicx}

\renewcommand\thesubfigure{(\alph{subfigure})}
\captionsetup[sub]{
  labelformat=simple
}

\begin{document}
\begin{figure}

\centering

\begin{subfigure}[b]{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{example-image}
    \caption{$y=x$}
    \label{fig:1}
\end{subfigure}\hfill
\begin{subfigure}[b]{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{example-image}
    \caption{$y=\sin x$}
    \label{fig:2}
\end{subfigure}\hfill
\begin{subfigure}[b]{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{example-image}
    \caption{$y=\cos x$}
    \label{fig:3}
\end{subfigure}
\caption{Three simple graphs}
\label{fig:three-graphs}
\end{figure}

Here are the references: \ref{fig:1}, \ref{fig:2}, \ref{fig:3}
which are subfigures to figure~\ref{fig:three-graphs}.

\end{document}

See Change format of reference to a subfigure for other customizations.

enter image description here